media media - 16 days ago 4x
C++ Question

Image is not retrieved correctly

I want to capture an image and use it as a gray level image.

I have the following code:

CvCapture *p = cvCreateCameraCapture(0);
cvSetCaptureProperty(p, CV_CAP_PROP_FRAME_WIDTH, 1024);
cvSetCaptureProperty(p, CV_CAP_PROP_FRAME_HEIGHT, 1024);
IplImage* frame;
for (int i = 0; i < 25; i++)
frame = cvQueryFrame(p);
cvSaveImage("test.jpg", frame);

Mat r = imread("test.jpg", 1);
Mat inputImage;
cvtColor(r, inputImage, COLOR_RGB2GRAY);

In my code
is a three channel image (three dimensions). when I read the saved image with r it has two channels.

I have two questions:

  1. why this happens?

  2. how can I have one dimensional image which is gray level?


For your first question: You should check which camera/hardware you are using? Also, confirm if frame has 2 channels by running:

 cout << img->nChannels << endl;

For second part:

To read image as gray channel, change:

Mat r = imread("test.jpg", 1);


Mat r = imread("test.jpg", 0);

See this: docs