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?

4

1 回答 1

1

您可以使用以下结构:

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

在您的情况下,它将您的数据映射到 OpenCV 格式,这是:

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

但要小心第一行不是复制数据。

于 2013-02-22T13:05:37.043 回答