How to convert color frame to OpenCV mat

Hello I’m the beginner of orbbec astra camera. I finally found out how to get color frame and listen the data. However, I’m struggled to convert the frame to opencv mat. Is there anyone who can help me?

Thanks in advance

2 Likes

Have you figured anything out? This is something that I’m also struggling with and would like to know if there’s a solution.

Check ‘Loop for reading frames’ at:

I’ve looked into that but I don’t have openni installed, do you know where I can get it for the conversion?

Are you using Astra SDK instead of OpenNI?

You can download OpenNI here:

1 Like

That helped, thank you. In the stackoverflow they do rc = openni::OpenNI::initialize(); but what is rc’s identifier? I’m not sure I understand how they got to this line.

rc is a status code indicating success or failure for an operation e.g.:
openni::Status rc = openni::STATUS_OK;

  • STATUS_OK = 0
  • STATUS_ERROR = 1
  • STATUS_NOT_IMPLEMENTED = 2
  • STATUS_NOT_SUPPORTED = 3
  • STATUS_BAD_PARAMETER = 4
  • STATUS_OUT_OF_FLOW = 5
  • STATUS_NO_DEVICE = 6
  • STATUS_TIME_OUT = 102
1 Like

Hi,

Will it be possible to use Astra SDK instead of OpenNI?

I guess astra::RgbPixel* == openni::RGB888Pixel*, so the while loop behind the stackoverflow link would be then:

const astra::ColorFrame colorFrame = frame.get<astra::ColorFrame>();
const astra::RgbPixel* imageBuffer = colorFrame.data();

frame.create(colorFrame.height(), colorFrame.width(), CV_8UC3);
memcpy( frame.data, imageBuffer, 3*colorFrame.height()*colorFrame.width()*sizeof(uint8_t) );

cv::cvtColor(frame,frame,CV_BGR2RGB); //this will put colors right

I haven’t tried if this works though. :slight_smile:

1 Like

Thanks! Managed to get it to work!

You’re welcome. Did you have to modify to the or did it work as it is?

I have problem with this line. I got an error that the “create” method doesn’t exists. Not too sure if I made a mistake somewhere. I created a matrix instead:

Mat my_frame = Mat(colorFrame.height(), colorFrame.width(), CV_8UC3);
memcpy(my_frame, color, 3 * colorFrame.height() * colorFrame.width() * sizeof(uint8_t));

What version of OpenCV are you using?

I am using 3.4.5 (Ubuntu 16.04).

Then the method should exist. Did you forget to introduce Mat my_frame before allocating it inside the while loop?
...
Mat my_frame;
while (true)
{
...
my_frame.create(colorFrame.getHeight(), colorFrame.getWidth(), CV_8UC3);
...

Oh, yes, that must be it. Haha.

Thanks for your help!

1 Like