Ask Your Question

DepthImage to Mat (OpenCV) [closed]

asked 2014-09-25 09:29:58 -0600

updated 2014-09-26 12:03:42 -0600

I am working with a 3d camera that provides a depthimage which is encoded in 32FC1 I need the image in OpenCV format (Mat) in order to access each component as a matrix

The problem is that when I have done the transformation, I try to access the Matrix data with Mat::at but I can not see real values. I don't know where the problem is.


I have discovered that the problem is the ros encoding of the image. I have simulated a depth camera and I don't know the equivalence between each pixel value and the distance it represents. My camera code is the following:

  <gazebo reference="kinect_depth_frame">
    <sensor type="depth" name="fotonic">
      <camera name="fotonic">
        <horizontal_fov>1.221730476</horizontal_fov>  <!-- 70 deg -->
          <width>800</width>  <!-- 160 -->
          <height>600</height> <!-- 120 -->

      <plugin name="kinect_depth_optical_frame_controller" filename="">
edit retag flag offensive reopen merge delete

Closed for the following reason too subjective or argumentative by arenillas
close date 2014-09-30 04:50:43.611820


Can you please add your code for retreiving your cv::Mat from the image message and the cv::Mat::at statement?

Wolf gravatar imageWolf ( 2014-09-30 04:23:26 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2014-09-30 04:18:04 -0600

The depth image does not follow OpenCV standards. The value of each pixel represents the distance in meters,

edit flag offensive delete link more


There is no standard that specifies a cv::Mat may not contain depth values as distances in meters. OpenCV allows floating point matrices.

Wolf gravatar imageWolf ( 2014-09-30 04:26:16 -0600 )edit

Of course, image processing function that assume color images will not work with such image; but cv::Mat::at (which the user mentioned) should work if templates are selected correctly!

Wolf gravatar imageWolf ( 2014-09-30 04:26:41 -0600 )edit

Yeah, but I tried to represent the image with OpenCV and, because of I had it in 32float, it expects the image to have values between 0 to 1. So, when I tried to represent the image It was not the original I had in ROS and I was becoming crazy.

arenillas gravatar imagearenillas ( 2014-09-30 04:48:35 -0600 )edit

Still, this should work; if you have a cv::Mat your_mat of type CV_32FC1 using<float>( i,j ) you are able to set it to any float value and retrieve any float value out of it; not just those between 0 and 1

Wolf gravatar imageWolf ( 2014-09-30 05:39:23 -0600 )edit

answered 2014-09-26 05:04:19 -0600

Mago Nick gravatar image

updated 2014-09-26 05:06:02 -0600

Hi, I am not really sure but apparently you are doing the conversion in the wrong way. Try to use the encription parameter of toCvCopy. Try to use 16UC1 as encription.

EDIT: I hav read that you already know the encription, try to use that one

Some sources:

  1. Tutorial: tutorial
  2. Discussion: first

  3. Discussion: second

edit flag offensive delete link more


I have updated the question with my new discoveries. Thank you for your help.

arenillas gravatar imagearenillas ( 2014-09-26 11:58:41 -0600 )edit

Question Tools

1 follower


Asked: 2014-09-25 09:29:58 -0600

Seen: 390 times

Last updated: Sep 30 '14