# Depth image/disparity

**URL:** <https://forum.opencv.org/t/depth-image-disparity/11014>\
**Category:** Python\
**Tags:** ros\
**Created:** [November 17, 2022, 4:01pm UTC](https://forum.opencv.org/t/depth-image-disparity/11014 "2022-11-17T16:01:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aristow](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@aristow](https://forum.opencv.org/u/aristow)\
**Post date:** [November 17, 2022, 4:01pm UTC](https://forum.opencv.org/t/depth-image-disparity/11014/1 "2022-11-17T16:01:19Z")

</div>

Hi, i need a small help with some matter that i cant wrap my head arround.  
Im trying to publish a disparity map of a stereo camera, i wrote this node here and i keep getting this error when after starting the node :  
`could not convert image from '16SC1' to 'rgb8' ([16SC1] is not a color format. but [rgb8] is. The conversion does not make sense)`  
this is my node :

```auto
import rospy
import cv2 as cv
import numpy as np
from cv_bridge import CvBridge, CvBridgeError
from sensor_msgs.msg import Image
import message_filters

def read_cameras():
    imageL = message_filters.Subscriber("/camera_left/image_raw", Image)
    imageR = message_filters.Subscriber("/camera_Right/image_raw", Image)
    

    # Synchronize images
    ts = message_filters.ApproximateTimeSynchronizer([imageL, imageR], queue_size=10, slop=0.5)
    ts.registerCallback(image_callback)
    rospy.spin()

def image_callback(imageL, imageR):
    br = CvBridge()
    rospy.loginfo("receiving Image")

    imageLeft = br.imgmsg_to_cv2(imageL, "rgb8")
    imageRight = br.imgmsg_to_cv2(imageR, "rgb8")

    imageL_new=cv.cvtColor(imageLeft, cv.COLOR_BGR2GRAY)

    imageR_new=cv.cvtColor(imageRight, cv.COLOR_BGR2GRAY)

   

    stereo = cv.StereoBM_create(numDisparities=16, blockSize=15)
    disparity = stereo.compute(imageR_new,imageL_new)

    rospy.loginfo("showing depth image")
    # Process images...

    pub = rospy.Publisher('/left_camera', Image, queue_size=1)
    cv.imshow('disparity', disparity)
    msg = br.cv2_to_imgmsg(disparity, encoding='16SC1')
    pub2 = rospy.Publisher('/Right_camera', Image, queue_size=1)
    msg2 = br.cv2_to_imgmsg(disparity, encoding='16SC1')

    
    pub.publish(msg)
    pub2.publish(msg2)
     

    

if __name__ == ' __main__':
    rospy.init_node('my_node')
    try:
        read_cameras()
        
    except rospy.ROSInterruptException:
        pass

```

is this an encoding Problem ? can someone tell me what im doing wrong exactly? i tried multiple approaches as to change the encodings but that didnt change.  
Anyhelp would be appreciated. thanks

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [November 17, 2022, 9:13pm UTC](https://forum.opencv.org/t/depth-image-disparity/11014/2 "2022-11-17T21:13:46Z")

</div>

ROS+numpy issue.

your array has `dtype == np.int16` (which is `CV_16SC1`)

---

<div class="post-metadata">

**Author:** ![aristow](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@aristow](https://forum.opencv.org/u/aristow)\
**Post date:** [November 17, 2022, 10:24pm UTC](https://forum.opencv.org/t/depth-image-disparity/11014/3 "2022-11-17T22:24:54Z")

</div>

the color mode of the image that is being read from the camera is `"rgb8"` and basically converting it to GRAY `imageL_new=cv.cvtColor(imageLeft, cv.COLOR_BGR2GRAY)` and inputting into `stereo.compute()`. i Dont understand what to change here exactly.  
Thank you for your response.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [November 17, 2022, 10:59pm UTC](https://forum.opencv.org/t/depth-image-disparity/11014/4 "2022-11-17T22:59:11Z")

</div>

> [@aristow](#):
>
> the color mode of the image that is being read from the camera is `"rgb8"` and basically converting it to GRAY `imageL_new=cv.cvtColor(imageLeft, cv.COLOR_BGR2GRAY)`

irrelevant.

> [@aristow](#):
>
> inputting into `stereo.compute()`

and what you get **out** of that has a `dtype` of `np.int16`. do you see?

---

<div class="post-metadata">

**Author:** ![aristow](https://avatars.discourse-cdn.com/v4/letter/a/5e9695/32.png) [@aristow](https://forum.opencv.org/u/aristow)\
**Post date:** [November 18, 2022, 1:04pm UTC](https://forum.opencv.org/t/depth-image-disparity/11014/5 "2022-11-18T13:04:33Z")

</div>

What do i need to change in my code for the conversion ?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [November 18, 2022, 4:50pm UTC](https://forum.opencv.org/t/depth-image-disparity/11014/6 "2022-11-18T16:50:42Z")

</div>

again, we don’t support `cv_bridge`. that’s not part of OpenCV. you need to talk to ROS people about this.

`cv2_to_imgmsg()` appears to support a “passthrough” encoding argument. maybe try that and see if it works.
