Ask Your Question
1

Why is elemSize 6 and depth 2 of rgb Mat after CV_BayerBG2BGR ?

asked 2020-07-18 03:00:19 -0600

Ciunasbothair gravatar image

When using OpenCV RAW to RGB conversion (CV_BayerBG2BGR), the image is displayed correctly but the image basic types are incorrect (elemSize & depth).

Even writing the converted Mat to file and then loading it (rgb_path in code below), there is a discrepancy between the loaded rgb image and the converted rgb image, although both display fine.

This causes an issue downstream where I convert from Mat to uint8_t* as the buffer size is larger in the converted rgb image.

Is this an issue with the conversion itself or my understanding of the conversion / OpenCV basic data types? I am using OpenCV 341.

int main() {

Mat img = imread(rgb_path);

ifstream ifd(raw_path, ios::binary | ios::ate);
int size = ifd.tellg();
ifd.seekg(0, ios::beg);
vector<char> buffer;
buffer.resize(size);
ifd.read(buffer.data(), size);

Mat rgb_image;
Mat raw_image(600, 800, CV_16UC1, buffer.data());

cvtColor(raw_image, rgb_image, CV_BayerBG2BGR);

cout << "elemSize() orig: " << img.elemSize() << endl;
cout << "elemSize() conv: " << rgb_image.elemSize() << endl;
cout << "channels() conv: " << rgb_image.channels() << endl;
cout << "channels() orig: " << img.channels() << endl;
cout << "depth() conv: " << rgb_image.depth() << endl;
cout << "depth() orig: " << img.depth() << endl;

return 0;

}

Output:
elemSize() orig: 3
elemSize() conv: 6
channels() conv: 3
channels() orig: 3
depth() conv: 2
depth() orig: 0
edit retag flag offensive close merge delete

Comments

1

what is the value of size ?

depth: 2 == CV_16U

can you check your saved file with an external image viewer ? (it's probably still 16bit there)

also check imread(raw_path, IMREAD_ANYDEPTH) (else it will convert to 8bit 3 channel under the hood)

berak gravatar imageberak ( 2020-07-18 03:32:22 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-07-18 06:26:27 -0600

berak gravatar image

updated 2020-07-18 06:36:27 -0600

cvtColor won't do any depth conversion here:

Mat raw_image(600, 800, CV_16UC1, buffer.data());
cvtColor(raw_image, rgb_image, CV_BayerBG2BGR);

so if your raw input image is CV_16U, the resulting bgr image will have exactly the same depth

then, imread(raw_path) will silently convert to 8bit bgr (i guess, the confusion comes from there !),

if you want to keep the original depth (and assuming you saved it with a .png extension), use

imread(raw_path, IMREAD_ANYDEPTH);

if you need the result to be 8bit, you will have to convert it manually:

Mat bgr8;
rgb_image.convertTo(bgr8, CV_8U, 1.0/255);
edit flag offensive delete link more

Comments

Great answer, thank you very much for giving your time @berak.

I had naively (stupidly) expected the CV_BayerBG2BGR to handle the bitness also, and I guess the accommodating nature of the the imread and imshow functions threw me.

Using the convertTo function results in the buffer size I am expecting.

Ciunasbothair gravatar imageCiunasbothair ( 2020-07-18 09:36:15 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-07-18 02:58:13 -0600

Seen: 326 times

Last updated: Jul 18 '20