Skip to content

Commit d0c155f

Browse files
committed
add single channel grayscale parsing for color inputs
1 parent 1b3a4fd commit d0c155f

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

hydra_ros/src/input/image_receiver.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,28 @@ ColorSubscriber::Filter& ColorSubscriber::getFilter() const {
5656
}
5757

5858
void ColorSubscriber::fillInput(const Image& img, ImageInputPacket& packet) const {
59-
try {
60-
packet.color = cv_bridge::toCvCopy(img, sensor_msgs::image_encodings::RGB8)->image;
61-
} catch (const cv_bridge::Exception& e) {
62-
LOG(ERROR) << "Failed to convert color image: " << e.what();
59+
// Allow also mono images to be converted to grayscale.
60+
if (sensor_msgs::image_encodings::isColor(img.encoding)) {
61+
try {
62+
packet.color =
63+
cv_bridge::toCvCopy(img, sensor_msgs::image_encodings::RGB8)->image;
64+
return;
65+
} catch (const cv_bridge::Exception& e) {
66+
LOG(ERROR) << "Failed to convert mono image as color input: " << e.what();
67+
return;
68+
}
69+
} else if (sensor_msgs::image_encodings::isMono(img.encoding)) {
70+
try {
71+
cv::Mat mono =
72+
cv_bridge::toCvCopy(img, sensor_msgs::image_encodings::MONO8)->image;
73+
cv::cvtColor(mono, packet.color, cv::COLOR_GRAY2RGB);
74+
return;
75+
} catch (const cv_bridge::Exception& e) {
76+
LOG(ERROR) << "Failed to convert color image: " << e.what();
77+
return;
78+
}
6379
}
80+
LOG(ERROR) << "Failed to convert color image: unsupported encoding: " << img.encoding;
6481
}
6582

6683
DepthSubscriber::DepthSubscriber() = default;

0 commit comments

Comments
 (0)