halcon_image.cpp
Go to the documentation of this file.
1 
20 #include <boost/make_shared.hpp>
21 
22 namespace halcon_bridge {
23 
24  const char* INVALID = "invalid";
25  const char* RGB = "rgb";
26  const char* BGR = "bgr";
27 
28  int getHalconTypeSize(const std::string& type) {
29 
30  if ((type == "byte") || (type == "int1")) return 1;
31  if ((type == "uint2") || (type == "int2")) return 2;
32  if ((type == "int4") || (type == "real")) return 4;
33  if (type == "int8") return 8;
34 
35  return -1;
36  }
37 
38  const char* getHalconEncoding(const std::string& encoding) {
39  // 3/4-channel encodings
40  if (encoding == sensor_msgs::image_encodings::BGR8) return "bgr";
41  if (encoding == sensor_msgs::image_encodings::RGB8) return "rgb";
42  if (encoding == sensor_msgs::image_encodings::BGRA8) return "bgrx";
43  if (encoding == sensor_msgs::image_encodings::RGBA8) return "rgbx";
44 
45  // 1-channel encoding
46  if (encoding == sensor_msgs::image_encodings::MONO8) return "mono";
47 
48  // Other formats are not supported
49  return INVALID;
50  }
51 
52 
53 
54  const char* getHalconChannelLength(const std::string& encoding) {
55 
59  return "byte";
60  }
64  return "uint2";
65  }
66 
67  return INVALID;
68  }
69 
70 
71 
72  const char* getColorChannelOrder(const std::string& encoding) {
75  return BGR;
76  }
79  return RGB;
80  }
81  return INVALID;
82  }
83 
84 
85 
86 
88  delete image;
89  }
90 
91  sensor_msgs::ImagePtr HalconImage::toImageMsg() const {
92  sensor_msgs::ImagePtr ptr = boost::make_shared<sensor_msgs::Image>();
93  toImageMsg(*ptr);
94  return ptr;
95  }
96 
97 
98 
99  void HalconImage::toImageMsg(sensor_msgs::Image& ros_image) const {
100  long width, height;
101  width = image->Width();
102  height = image->Height();
103 
104  int channel_count = image->CountChannels();
105 
106  ros_image.height = height;
107  ros_image.width = width;
108  ros_image.encoding = encoding;
109  ros_image.is_bigendian = false;
110  ros_image.step = channel_count * width;
111 
112 
113  HalconCpp::HString typeReturn;
114  Hlong widthReturn;
115  Hlong heightReturn;
116  HalconCpp::HString type = image->GetImageType();
117 
118  if (channel_count > 1) {
119 
120  //build interleaved image from 3-channel image
121 
122  HalconCpp::HImage *interleavedImage = new HalconCpp::HImage(type, width * 3, height);
123 
124  HalconCpp::HHomMat2D homMat;
125  homMat = homMat.HomMat2dScale(1, 3, 0, 0);
126 
127  HalconCpp::HImage transImage = image->AffineTransImageSize(homMat, "constant", width * 3, height);
128 
129  HalconCpp::HImage imageRed;
130  HalconCpp::HImage imageGreen;
131  HalconCpp::HImage imageBlue;
132  imageRed = transImage.Decompose3(&imageGreen, &imageBlue);
133 
134 
135  HalconCpp::HRegion regionGrid;
136  regionGrid.GenGridRegion(2 * height, 3, "lines", width * 3, height + 1);
137  HalconCpp::HRegion movedRegion = regionGrid.MoveRegion(-1, 0);
138  HalconCpp::HRegion clippedRegion = movedRegion.ClipRegion(0, 0, height - 1, (3 * width) - 1);
139 
140  if (getColorChannelOrder(encoding) == RGB) {
141  imageRed = imageRed.ReduceDomain(clippedRegion);
142  } else {
143  imageBlue = imageBlue.ReduceDomain(clippedRegion);
144  }
145  movedRegion = regionGrid.MoveRegion(-1, 1);
146  clippedRegion = movedRegion.ClipRegion(0, 0, height - 1, (3 * width) - 1);
147  imageGreen = imageGreen.ReduceDomain(clippedRegion);
148  movedRegion = regionGrid.MoveRegion(-1, 2);
149  clippedRegion = movedRegion.ClipRegion(0, 0, height - 1, (3 * width) - 1);
150  if (getColorChannelOrder(encoding) == RGB) {
151  imageBlue = imageBlue.ReduceDomain(clippedRegion);
152  } else {
153  imageRed = imageRed.ReduceDomain(clippedRegion);
154  }
155 
156  interleavedImage->OverpaintGray(imageRed);
157  interleavedImage->OverpaintGray(imageGreen);
158  interleavedImage->OverpaintGray(imageBlue);
159 
160 
161 
162  // copy data of interleaved image into sensor_msg
163  unsigned char* colorData = (unsigned char*)interleavedImage->GetImagePointer1(&typeReturn, &widthReturn, &heightReturn);
164  int interleavedHeight = interleavedImage->Height();
165  int interleavedWidth = interleavedImage->Width();
166  size_t size = interleavedHeight * interleavedWidth * getHalconTypeSize((std::string)typeReturn);
167  ros_image.data.resize(size);
168  memcpy((unsigned char*)(&ros_image.data[0]), colorData, size);
169 
170  interleavedImage->Clear();
171  imageRed.Clear();
172  imageGreen.Clear();
173  imageBlue.Clear();
174  homMat.Clear();
175  regionGrid.Clear();
176  clippedRegion.Clear();
177  movedRegion.Clear();
178 
179 
180  } else {
181 
182  // 1-channel image: copy data of original image
183  unsigned char* colorData = (unsigned char*)image->GetImagePointer1(&typeReturn, &widthReturn, &heightReturn);
184  size_t size = height * width * getHalconTypeSize((std::string)typeReturn);
185  ros_image.data.resize(size);
186  memcpy((unsigned char*)(&ros_image.data[0]), colorData, size);
187  }
188 
189  }
190 
191 
192 
193  HalconImagePtr toHalconCopy(const sensor_msgs::ImageConstPtr& source) {
194  return toHalconCopy(*source);
195  }
196 
197  HalconImagePtr toHalconCopy(const sensor_msgs::Image& source) {
198  HalconImagePtr ptr = boost::make_shared<HalconImage>();
199  ptr->header = source.header;
200  ptr->encoding = source.encoding;
201 
202  if (getHalconEncoding(ptr->encoding) == INVALID) {
203  throw Exception("Encoding " + ptr->encoding + " not supported");
204  }
205 
206  long* pixeldata = (long*)const_cast<unsigned char*>(&source.data[0]);
207  HalconCpp::HImage *img = new HalconCpp::HImage();
208  if ((std::string)getHalconEncoding(source.encoding) == "mono") {
209  img->GenImage1(getHalconChannelLength(source.encoding), source.width, source.height, pixeldata);
210  } else {
211  img->GenImageInterleaved(pixeldata, getHalconEncoding(source.encoding), source.width, source.height, 0,
212  getHalconChannelLength(source.encoding), source.width, source.height, 0, 0, -1, 0);
213  }
214  ptr->image = img;
215 
216  return ptr;
217  }
218 
219 }
220 
221 
sensor_msgs::ImagePtr toImageMsg() const
Convert this message to a ROS sensor_msgs::Image message.
const std::string BGRA16
HalconImagePtr toHalconCopy(const sensor_msgs::ImageConstPtr &source)
Convert a sensor_msgs::Image message to a Halcon-compatible HImage, copying the image data...
const char * getColorChannelOrder(const std::string &encoding)
const char * INVALID
const std::string RGBA16
int getHalconTypeSize(const std::string &type)
const char * getHalconEncoding(const std::string &encoding)
const std::string MONO16
const char * BGR
HalconCpp::HImage * image
Definition: halcon_image.h:45
const char * RGB
const char * getHalconChannelLength(const std::string &encoding)


asr_halcon_bridge
Author(s): Allgeyer Tobias
autogenerated on Fri Jun 19 2020 03:54:32