0

I'm trying to initialize a Mat using a camera buffer that is holding a 32 bit ARGB frame. These are the steps I have taken till now:

cv::Mat src = cv::Mat(cv::Size(img_height, img_width),CV_8UC4);
memcpy(src.ptr(), (void*) img_buffer,img_height * img_width * 4);

cv::Mat dest= src.clone();

cv::cvtColor(src,dest,COLOR_BGRA2BGR);

This leads to a segfault. Still occurs even if dest is initialized as

cv::Mat dest=cv::Mat(src.size(),src.type());

Would appreciate any help on this.

UPDATE

So I'm trying to untangle the order manually, like this:

int rgb_temp[4];
for(int y=0; y < (int)img_height; y++) {
    for(int x=0; x < (int)img_width; x++) {
        rgb_temp[0] = (unsigned int)img_buffer[(int)img_stride * y + x + 0]; // A
        rgb_temp[1] = (unsigned int)img_buffer[(int)img_stride * y + x + 1]; // R
        rgb_temp[2] = (unsigned int)img_buffer[(int)img_stride * y + x + 2]; // G
        rgb_temp[3] = (unsigned int)img_buffer[(int)img_stride * y + x + 3]; // B
        src.data[ (y + x)  + 0] = rgb_temp[3]; // B
        src.data[ (y + x)  + 1] = rgb_temp[2]; // G
        src.data[ (y + x)  + 2] = rgb_temp[1]; // R
        src.data[ (y + x)  + 3] = rgb_temp[0]; // A
    }
}

But to no avail. I am able to read the ARGB values from the img_buffer but am unable to write to the src.data. Is this a right way to take?

hasan
  • 315
  • 2
  • 14

1 Answers1

1

You could use the following construction:

Mat::Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP)

which maps your data into the OpenCV format in your case this is:

cv::Mat src(img_height, img_width ,CV_8UC4, img_buffer)
cv::Mat dst;
src.copyTo(dst); 

but be carefull the first line is not copying the data.

Tobias Senst
  • 2,665
  • 17
  • 38
  • The reason why I am not initializing with img_buffer is because it will not copy the data and the data contained in it is not guaranteed to be available. – hasan Feb 22 '13 at 13:15