Skip to content

Commit c1e0d63

Browse files
committed
Fix visible/color frame conversion
It's RGB8, not MONO8. It's also possible to do a shallow copy instead of a deep copy, so the conversion is faster.
1 parent 2cec3fd commit c1e0d63

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/sc.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@
2121
#define LOG_PERIOD_S 10
2222
#define NODE_NAME "sc"
2323

24+
bool isMono(const ST::ColorFrame &visFrame)
25+
{
26+
return visFrame.width() * visFrame.height() == visFrame.rgbSize();
27+
}
28+
29+
std::string getEncoding(const ST::ColorFrame &visFrame)
30+
{
31+
return isMono(visFrame) ? sensor_msgs::image_encodings::MONO8 : sensor_msgs::image_encodings::RGB8;
32+
}
33+
2434
struct SessionDelegate : ST::CaptureSessionDelegate {
2535
private:
2636
std::mutex lock;
@@ -198,25 +208,16 @@ struct SessionDelegate : ST::CaptureSessionDelegate {
198208
return;
199209
}
200210

201-
cv::Mat img(visFrame.height(), visFrame.width(), CV_8UC1);
211+
const std::string encoding = getEncoding(visFrame);
202212

203-
const uint8_t* buf = visFrame.rgbData();
204-
205-
for(int y = 0; y < visFrame.height(); y++)
206-
{
207-
for(int x = 0; x < visFrame.width(); x++)
208-
{
209-
std::size_t pixelOffset = (y * visFrame.width()) + x;
210-
img.at<uchar>(y, x) = buf[pixelOffset];
211-
}
212-
}
213+
cv::Mat img(visFrame.height(), visFrame.width(), cv_bridge::getCvType(encoding), static_cast<void*>(const_cast<uint8_t*>(visFrame.rgbData())));
213214

214215
std_msgs::Header header;
215216
header.frame_id = frame_id_;
216217
// TODO: convert timstamp to ROS time
217218
// header.stamp = infraredFrame.timestamp();
218219

219-
sensor_msgs::ImagePtr msg = cv_bridge::CvImage(header, "mono8", img).toImageMsg();
220+
sensor_msgs::ImagePtr msg = cv_bridge::CvImage(header, encoding, img).toImageMsg();
220221
vis_pub_.publish(msg);
221222

222223
sensor_msgs::CameraInfo cam_info;

0 commit comments

Comments
 (0)