Ask Your Question

Convert BayerBG12Packed (12-Bit packed) into RGB

asked 2013-09-15 12:08:13 -0600

syberarall gravatar image

updated 2013-09-15 12:36:32 -0600


can anyone tell me, what code inside cvtColor has to be used, when converting 12 bit packed BayerBG12Packed into RGB. The following code does not work:

cv::Mat Mat16Bit(SizeY, SizeX, CV_16UC1, ImageData);
cv::Mat Mat8Bit = Mat16Bit.clone();
Mat8Bit.convertTo(Mat8Bit, CV_8UC3, 0.0625);
cv::Mat MatRgb(SizeY, SizeX, CV_8UC3);
cv::cvtColor(Mat8Bit, MatRgb, CV_BayerBG2RGB);

image MyImage

BayerBG12 works fine, so maybe the image sizes do not match - any help is welcome.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-09-16 08:33:58 -0600

Maik Angermann gravatar image


I am facing kind of the same problem. I found a sample converting PV Image from ebus sdk (which uses several bayer Patterns) to IplImage. But if you use the Switch case structure like in the sample, a BG8 Color Image will be changed into a monochrome Image. If one uses nchannels=3 the program crashes. I Need to read out the buffer as PVImage the to convert it into an IplImage, after this I could use the cvConvert function. I know that the depth of a Bayer BG8 is 8 bit unsigned but I dont know how many channels to use for the IPLImage then. Setting channel to 1 seems the only possibility but also changes the colored Image into a Grey one. cheers Maik

edit flag offensive delete link more

answered 2014-01-09 05:45:43 -0600

Maik Angermann gravatar image

after struggling a lot I found out something. Using an 12bit Bayern Pattern Image will be too dark though. So if one converts the Bayern Pattern into a 16 Bit IplImage the colors will be displayed with a wrong value. What needs to be done after the cvconvert (Bayer2BGR) is to scale the color value (which is still based on the 12 Bit Image) to the 16 Bit Image. The IplImage now will be displayed with the correct Image colors

Cheers Maik

edit flag offensive delete link more


I am having a similar problem. What do you mean by "scale the color value"?

jnj11 gravatar imagejnj11 ( 2015-07-24 10:23:51 -0600 )edit

12 bit max = 4095, 16 bit max = 65535, right? So if you have a value that is 2048 in 12bit (4095max) (approx. 50%), it is 32767 (approx. 50%) in the 16 bit range. Or:

12bitVal / 12bitMax * 16bitMax

2048 / 4095 * 65535

mikegibson_dajac gravatar imagemikegibson_dajac ( 2015-09-24 12:12:25 -0600 )edit

I feel I should add that this is the same as multiplying the 12bit value by 16. And for speed, you could actually just bit shift it to the left by 4:

16bitVal = (12bitVal << 4);

mikegibson_dajac gravatar imagemikegibson_dajac ( 2015-09-24 12:25:02 -0600 )edit

Question Tools


Asked: 2013-09-15 12:08:13 -0600

Seen: 6,450 times

Last updated: Jan 09 '14