Ask Your Question

read camera image incorrect

asked 2015-01-30 10:25:27 -0500

elfsingle gravatar image

updated 2015-02-06 02:58:40 -0500

I using the function of videocapture to read camera data

VideoCapture cap(0);

int Key;
Mat src;

        namedWindow("capture", 0);

        Key = waitKey(10);
        if(Key ==27)

The output is likely bayer image with green visionimage description And the image size is not correct(640X480), it should be 2592X1944 in raw 12 bits.

Is some parameters for function videocapture need I set? or have anything I miss for opencv videocapture?


I got it!

The problem is causing by a function which is translating raw data to bayer pattern.

The camera input is raw in 12 bits format, as reading the camera data that one element is separated to two bytes (low byte and high byte)

We need to merge this two byte to a int format(0~2^8 -> 0~2^16).

OpenCV has a mistake in the data merging, so I got the wrong image first.

I coding that translating raw data to bayer pattern and getting the correct result without white balance.

image description

edit retag flag offensive close merge delete


Hi, I'm having the same problem. I've used cvtColor but, my image is still not displaying correctly. I've pasted my code below and have included a link to the resulting image. Can you see what I'm doing wrong? cv::VideoCapture (cam1);

cv::Mat changed;
cv::Mat bayer16(data.rows, data.cols, CV_16UC1,,data.step);
cv::Mat bayer8 = bayer16.clone();
bayer8.convertTo(bayer8, CV_8UC3, 0.0625);

cv::cvtColor(bayer8, changed, CV_BayerGB2BGR);
imshow("", changed);
Resulting image

jnj11 gravatar imagejnj11 ( 2015-07-24 12:36:46 -0500 )edit

I have an PointGrey BlackFly that reads as three 8-bit channels in Python's Numpy array of shape (rows, cols, 3). However, each cell (pixel) has the same value, so I end up with a gray-scale image-like by default. I tried several conversions of color, but Bayer cannot go from 3 channels to anything else. Does anyone know which property to set for this VideoCapture object or any other suggestion? I'm not sure which Video Data Output it's using, perhaps it's 24-bit digital data it's giving 24-bits per pixel as (8,8,8), but maybe not. How to find out the video format within OpenCV? If I ask for get(CAP_PVAPI_PIXELFORMAT_RGB24) it give 15.0, get(CAP_PVAPI_PIXELFORMAT_BAYER16) gives 480.0, and get(CAP_PVAPI_PIXELFORMAT_BAYER8) gives 640.0. Any other PIXELFORMAT gives -1. I don't understand it.

opencvslave gravatar imageopencvslave ( 2015-10-31 00:33:13 -0500 )edit

@elfsingle Can you share your final code that works, because I did not understand your explanation. I am a newbie and I rhave the same problem.

alidemir gravatar imagealidemir ( 2016-10-12 07:26:46 -0500 )edit

3 answers

Sort by ยป oldest newest most voted

answered 2015-01-30 14:53:32 -0500

updated 2015-01-30 14:57:46 -0500

After reading in the data apply either

  • cvtColor(cap, cap, CV_BayerBG2BGR)
  • cvtColor(cap, cap, CV_BayerGB2BGR)
  • cvtColor(cap, cap, CV_BayerGR2BGR)
  • cvtColor(cap, cap, CV_BayerRG2BGR)

according to the bayer pattern of your camera. If you do not know how the pattern is, just go for trial and error.

edit flag offensive delete link more


it show assertion failed (scn==1 && dcn==3) in cv:cvtcolor

elfsingle gravatar imageelfsingle ( 2015-02-01 21:57:11 -0500 )edit

that means you do not have a single channel image and thus your camera doesn't output a single channel bayer pattern image. Can you please check which format is returned by your camera?

StevenPuttemans gravatar imageStevenPuttemans ( 2015-02-02 03:33:16 -0500 )edit



src.channels() is three.... oh...I don't set anything just using so the function of videocapture is change the input format automatically? or the function isn't adapt this camera......

elfsingle gravatar imageelfsingle ( 2015-02-02 08:24:08 -0500 )edit

If this one worked then you are on the right track:

Mat bayer(1944, 2592, CV_16UC1,;              
cvtColor(bayer ,rgb, CV_BayerRG2BGR);

Now just try the different formats. It is important to know what type of bayer pattern you have, others simply won't work.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-02-03 02:06:47 -0500 )edit

All type of bayer pattern I have tried that got the same incorrect output. I think that reading unknown input which have 3 channels by and using Mat function to read input data that losing information by unknown format translate to 16UC1.

elfsingle gravatar imageelfsingle ( 2015-02-03 06:05:08 -0500 )edit

Can you please just try to manually create the matrix first. Do the following.

cap >> src; 
imshow("capture", src); 
StevenPuttemans gravatar imageStevenPuttemans ( 2015-02-03 08:15:06 -0500 )edit

it show assertion failed (scn==1 && dcn==3) in cv:cvtcolor

elfsingle gravatar imageelfsingle ( 2015-02-03 08:29:47 -0500 )edit

CAN you please debug and check which capture interface is being used? How is your camera connected to your system?

StevenPuttemans gravatar imageStevenPuttemans ( 2015-02-03 08:32:52 -0500 )edit

Camera is a usb3.0 device and I connected it in usb3.0 port

elfsingle gravatar imageelfsingle ( 2015-02-03 20:18:03 -0500 )edit

Okay now start a debug session and tell me what cap interface is been used by OpenCV

StevenPuttemans gravatar imageStevenPuttemans ( 2015-02-04 01:10:00 -0500 )edit

answered 2019-04-29 00:04:30 -0500

@elfsingle I am trying to convert a similar image to RGB but end up getting the following as the output. Can you help me with this?C:\fakepath\rawtorgb.png

edit flag offensive delete link more

answered 2015-01-30 14:58:02 -0500

mgb gravatar image

Cap set isn't very reliable because a lot of simpel camera drivers don't properly respond under windows. If it is a simple webcam you might be limited to 640x480.

Working out what data format is returned is always a bit of a challenge. Try the various BAYERxxxx options in cvtColor()

edit flag offensive delete link more


it show assertion failed (scn==1 && dcn==3) in cv:cvtcolor

elfsingle gravatar imageelfsingle ( 2015-02-01 21:57:43 -0500 )edit

Can you post more details (and code) on the solution to this problem?

lefsky gravatar imagelefsky ( 2015-07-29 13:29:49 -0500 )edit
Login/Signup to Answer

Question Tools



Asked: 2015-01-30 10:25:27 -0500

Seen: 3,005 times

Last updated: Jul 29 '15