Ask Your Question
0

How to use opencv demosaicing

asked 2020-09-07 20:23:28 -0500

Hello,

I am stuck trying to figure out how to use the opencv demosaicing function. I have OpenCV 4.4.0 installed with CUDA support compiled in, and so far what I think I need to do is:

  1. Read in the raw image data
  2. Load in raw image data to a Mat object
  3. Upload the Mat data to a GpuMat (host to device upload)
  4. Demosaic
  5. Download the GpuMat data (device to host download) to a Mat object
  6. Display or write out the result

Here is a snippet of the code I have.

ifstream ifs("image.raw", ios_base::binary);
ifs.read(buffer, length);
// snip ...buffer contains the entire file...

Mat src_host(6464, 4860, CV_16UC1, buffer);
GpuMat dst, src;
src.upload(src_host);

// Debayer here
cv::cuda::demosaicing(src, dst, COLOR_BayerRG2BGR);

// have a look
Mat result_host;
dst.download(result_host);

namedWindow("Debayered Image", WINDOW_KEEPRATIO);
resizeWindow("Debayered Image", 6464/5, 4860/5);
imshow("Debayered Image", result_host);
waitKey(0);

I have raw frames from cameras that have 12 bits per pixel, RGGB, dimensions 6464 x 4860. I'm uncertain of how to specify this for OpenCV in terms of width and height, what CV_TYPE to give it, whether I am reading in and uploading the data properly for demosaicing, what COLOR_code to give it for demosaicing, and how to download the result for display and saving to file (preferably a high level routine to write a png or similar).

Does anyone know whether I'm on the right track or not?

Thanks! James

edit retag flag offensive close merge delete

Comments

If your raw data is 12-bit pixels, interpreting them as 16-bits is not going to work and may seg fault. You need to first convert your data to 16-bit (without OpenCV). I don't think OpenCV has support for 10 and 12-bit pixels.

Der Luftmensch gravatar imageDer Luftmensch ( 2020-09-08 14:02:30 -0500 )edit

Der I am interested in your answer. Do you (or anyone else) have any suggestions for a gpu based approach to convert 12 bit to 16 bit pixels in order to use the opencv Demosaicing?

Much appreciated.

auimager gravatar imageauimager ( 2020-09-08 15:51:00 -0500 )edit

If you can capture from the camera at 16-bits (even if dynamic range is 12-bits), that might be the simplest and best option.

Der Luftmensch gravatar imageDer Luftmensch ( 2020-09-08 18:49:23 -0500 )edit

Hi Der, thanks for that. The data is not tightly packed; it's 6464 x 4860 of uint16, with each pixel represented in a 16-bit value; the entire raw image is 62830080 bytes. I thought I could treat it as a 16 bit image for debayering but still not sure.

jamesyoung gravatar imagejamesyoung ( 2020-09-08 19:51:19 -0500 )edit

It that case it looks right to me except that you might have rows and cols swapped. Are you seeing a problem? If so, can you share more about the error or the strange output?

Der Luftmensch gravatar imageDer Luftmensch ( 2020-09-09 08:29:23 -0500 )edit

That was it, thanks very much! Somehow I had rows and cols swapped. Works now.

jamesyoung gravatar imagejamesyoung ( 2020-09-09 17:45:10 -0500 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2020-09-09 19:44:29 -0500

Hi Der, much appreciated for your helpful suggestions. Cheers

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-09-07 20:23:28 -0500

Seen: 1,236 times

Last updated: Sep 09 '20