20 #include <boost/make_shared.hpp> 25 const char*
RGB =
"rgb";
26 const char*
BGR =
"bgr";
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;
92 sensor_msgs::ImagePtr ptr = boost::make_shared<sensor_msgs::Image>();
101 width =
image->Width();
102 height =
image->Height();
104 int channel_count =
image->CountChannels();
106 ros_image.height = height;
107 ros_image.width = width;
109 ros_image.is_bigendian =
false;
110 ros_image.step = channel_count * width;
113 HalconCpp::HString typeReturn;
116 HalconCpp::HString type =
image->GetImageType();
118 if (channel_count > 1) {
122 HalconCpp::HImage *interleavedImage =
new HalconCpp::HImage(type, width * 3, height);
124 HalconCpp::HHomMat2D homMat;
125 homMat = homMat.HomMat2dScale(1, 3, 0, 0);
127 HalconCpp::HImage transImage =
image->AffineTransImageSize(homMat,
"constant", width * 3, height);
129 HalconCpp::HImage imageRed;
130 HalconCpp::HImage imageGreen;
131 HalconCpp::HImage imageBlue;
132 imageRed = transImage.Decompose3(&imageGreen, &imageBlue);
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);
141 imageRed = imageRed.ReduceDomain(clippedRegion);
143 imageBlue = imageBlue.ReduceDomain(clippedRegion);
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);
151 imageBlue = imageBlue.ReduceDomain(clippedRegion);
153 imageRed = imageRed.ReduceDomain(clippedRegion);
156 interleavedImage->OverpaintGray(imageRed);
157 interleavedImage->OverpaintGray(imageGreen);
158 interleavedImage->OverpaintGray(imageBlue);
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);
170 interleavedImage->Clear();
176 clippedRegion.Clear();
183 unsigned char* colorData = (
unsigned char*)
image->GetImagePointer1(&typeReturn, &widthReturn, &heightReturn);
185 ros_image.data.resize(size);
186 memcpy((
unsigned char*)(&ros_image.data[0]), colorData, size);
199 ptr->header = source.header;
200 ptr->encoding = source.encoding;
203 throw Exception(
"Encoding " + ptr->encoding +
" not supported");
206 long* pixeldata = (
long*)const_cast<unsigned char*>(&source.data[0]);
207 HalconCpp::HImage *img =
new HalconCpp::HImage();
211 img->GenImageInterleaved(pixeldata,
getHalconEncoding(source.encoding), source.width, source.height, 0,
sensor_msgs::ImagePtr toImageMsg() const
Convert this message to a ROS sensor_msgs::Image message.
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)
int getHalconTypeSize(const std::string &type)
const char * getHalconEncoding(const std::string &encoding)
HalconCpp::HImage * image
const char * getHalconChannelLength(const std::string &encoding)