CvBridgeError: [8SC1] is not a color format. but [mono8] is. The conversion does not make sense [closed]
I'm trying to convert a ROS message of class 'sensor_msgs.msg._Image.Image' to an OpenCV image using Python and cv_bridge. The ROS message encoding is "8SC1", which is a single-channel 8-bit (grayscale) format. However, when I go to use cv_bridge's "imgmsg_to_cv2", I receive the following CvBridgeError:
[8SC1] is not a color format. but [mono8] is. The conversion does not make sense
First of all, this is wrong because mono8 is NOT a color format. I set desired_encoding to "mono8" as this is my desired encoding.
I also tried having desired encoding = "passthrough", but since this gave negative pixel values, I am attempting the mono8 conversion. It should be known that my ROS message is raw disparity values in pixels. I have used imgmsg_to_cv2 on another ROS (color) image message with no problems.
Relevant Python code is below:
import numpy as np
import cv2
import rospy
import std_msgs
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
cv_bridge_object = CvBridge()
def ROS2OpenCV_disp_converter(input):
output = cv_bridge_object.imgmsg_to_cv2(input, desired_encoding = "mono8")
cv2.imshow('Disparity', output)
cv2.waitKey(1)
def stereoCameraSubscriber():
rospy.init_node("Stereo_Camera_Subscriber")
rospy.Subscriber("/occam/disparity_image0", Image, ROS2OpenCV_disp_converter)
rospy.spin()
if __name__ == '__main__':
try:
stereoCameraSubscriber()
except rospy.ROSInterruptException:
pass
I had the same issue and fixed it by specifying the desired output format of the conversion to ROS format:
cv_bridge.cv2_to_imgmsg(image, 'bgr8')