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?