Demosaicing c++

asked 2020-11-23 02:43:10 -0500

genzy9 gravatar image

updated 2020-11-23 02:49:36 -0500


I have some issue using opencv 4.5 for demosaicing my image.

My CFA starts with GR pixels then second line BG as follows:

G R G R...

B G B G...

I get it from my camera. Now I try to debayer to display the RGB picture only using opencv.

// Get Picture, each composant is 8bit, stored in uint8_t buffer
frame.rightPicture().getPixels8bits(bufSize, imgBuf);
// Store it in Mat object
cv::Mat inputBufMat(h, w, CV_8U, cv::Scalar::all(0));
inputBufMat = cv::Mat(h, w, CV_8U, imgBuf);

// Should I convert it to BGR first ? 
cv::Mat outputBufMat(h, w, CV_8U, cv::Scalar::all(0));
cv::cvtColor(inputBufMat, outputBufMat, cv::COLOR_RGB2BGR); // ????

cv::Mat imgDebayer(h, w, CV_8U, cv::Scalar::all(0));
cv::demosaicing(outputBufMat, imgDebayer, cv::COLOR_BayerGR2BGR_EA); // ERROR

demosaicing function triggers exception. What I have done wrong? Is it the correct way to do it?

edit retag flag offensive close merge delete


first, please do not preallocate those result Mat's. they will be overwriten anyway, and you got the type/sizes quite often wrong, thus fooling you into thinking it is something it isn't

// Should I convert it to BGR first ?

definitely not. this is wrong, input is still bayer, not bgr. you also only have 1 channel, so far (so it's a no-op)

what is the exact exception you get ?

berak gravatar imageberak ( 2020-11-23 03:37:09 -0500 )edit

Thank you for your quick feedback. The error was due to typo, one mat was using CV_8UC3 instead of CV_8U. Ok so I only convert RGB -> BGR before using cv::imshow ?

genzy9 gravatar imagegenzy9 ( 2020-11-23 04:20:45 -0500 )edit

only IF your image IS RGB

berak gravatar imageberak ( 2020-11-23 04:33:46 -0500 )edit