PngConverter.cpp
Go to the documentation of this file.
1 #include "PngConverter.h"
2 
3 // ArmarX
4 #include <opencv2/imgcodecs.hpp>
5 #include <opencv2/imgproc.hpp>
6 #include <opencv2/imgproc/types_c.h>
7 #include <opencv2/opencv.hpp>
8 
10 
12 {
13  void
14  PngConverter::configure(const nlohmann::json& json)
15  {
16  }
17 
20  {
22 
24  std::vector<unsigned char> buffer;
25 
26 
27  auto shape = data->getShape(); // we know from the extraction that the shape has 3 elements
28  ARMARX_CHECK_EQUAL(shape.size(), 3);
29 
30  if (shape[2] == 3) // its probably a rgb image
31  {
32  cv::cvtColor(img, img, CV_RGB2BGR);
33  cv::imencode(suffix, img, buffer);
34  return {buffer, ".rgb"};
35  }
36 
37  if (shape[2] == 1) // its probably a grayscale image
38  {
39  cv::imencode(suffix, img, buffer);
40  return {buffer, ".gs"};
41  }
42 
43  // try to export without conversion
44  cv::imencode(suffix, img, buffer);
45  return {buffer, ""};
46  }
47 
50  {
51  if (data.suffix == ".rgb")
52  {
53  cv::Mat img = cv::imdecode(data.data, cv::IMREAD_COLOR);
54  cv::cvtColor(img, img, CV_BGR2RGB);
56  }
57 
58  if (data.suffix == ".gs")
59  {
60  cv::Mat img = cv::imdecode(data.data, cv::IMREAD_GRAYSCALE);
62  }
63 
64  // try to load without conversion
65  cv::Mat img = cv::imdecode(data.data, cv::IMREAD_ANYCOLOR);
67  }
68 } // namespace armarx::armem::server::ltm::processor::converter::data::image
armarx::armem::server::ltm::processor::converter::data::image::PngConverter::configure
void configure(const nlohmann::json &json) override
Definition: PngConverter.cpp:14
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::aron::data::NDArrayPtr
std::shared_ptr< NDArray > NDArrayPtr
Definition: NDArray.h:46
armarx::armem::server::ltm::processor::converter::data::image::PngConverter::_convert
ConversionResult _convert(const aron::data::NDArrayPtr &data) final
Definition: PngConverter.cpp:19
armarx::aron::Path
The Path class.
Definition: Path.h:35
armarx::aron::data::converter::AronOpenCVConverter::ConvertFromMat
static data::NDArrayPtr ConvertFromMat(const cv::Mat &, const armarx::aron::Path &={})
Definition: OpenCVConverter.cpp:52
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
OpenCVConverter.h
armarx::armem::server::ltm::processor::DataConverter::suffix
const std::string suffix
Definition: Converter.h:51
armarx::aron::data::converter::AronOpenCVConverter::ConvertToMat
static cv::Mat ConvertToMat(const data::NDArrayPtr &)
Definition: OpenCVConverter.cpp:33
armarx::armem::server::ltm::processor::converter::data::image
Definition: ExrConverter.cpp:10
armarx::armem::server::ltm::processor::DataConverter::ConversionResult
Definition: Converter.h:25
PngConverter.h
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130