Ask Your Question
0

Webcam image size

asked 2013-08-19 09:15:20 -0600

se6OH43Q gravatar image

Hello, I've downloaded OpenCV and made a simple program which takes a photo of you using the webcam. But the size of photos is only 160x120 so I was wondering if I can somehow scale it. I tried to use cvResize but it didn't help. Can anybody help me? Here is the code:

int main() {
    CvCapture *webcam = cvCreateCameraCapture(0); 
    if (!webcam)
        return 1;
    while (1) {
        IplImage *camImg = cvQueryFrame(webcam),*camImgSize = cvCreateImage(cvSize(640,480),camImg->depth,camImg->nChannels);
        if (camImg) {
            cvResize(camImg,camImgSize,CV_INTER_LINEAR);
            cvSaveImage("photo.jpg",camImg,0);
            cvShowImage("Photo",camImg);
            if (cvWaitKey(3000) == 0x1B) break;
        }
    }
    return 0;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-08-19 09:29:15 -0600

updated 2013-08-19 09:35:22 -0600

You can use :

cvSetCaptureProperty( webcam, CV_CAP_PROP_FRAME_WIDTH, 640 );
cvSetCaptureProperty( webcam, CV_CAP_PROP_FRAME_HEIGHT, 480 );

or resize the image (not recommended):

resize(img,img,Size(640,480));

In your case:

int main() {
    CvCapture *webcam = cvCreateCameraCapture(0); 
    cvSetCaptureProperty( webcam, CV_CAP_PROP_FRAME_WIDTH, 640 );
    cvSetCaptureProperty( webcam, CV_CAP_PROP_FRAME_HEIGHT, 480 );

    if (!webcam)return -1;
    while (true) {
        Mat img = cvQueryFrame(webcam);
        if (img.data) {
            imwrite("photo.jpg",img);
            imshow("Photo",img);
            if (waitKey(3000) == 0x1B) break;
        }
    }
    return 0;
}
edit flag offensive delete link more

Comments

Spas, thanks for your answer but unfortunately neither cvSetCaptureProperty nor resize are working. In fact, I cannot use resize at all because it says that it is an undefined function. //UPD Thanks for adding. How can I change your program in C? Now I cannot run it because of Mat.

se6OH43Q gravatar imagese6OH43Q ( 2013-08-19 09:39:03 -0600 )edit

try to add :

include "opencv2/opencv.hpp" and using namespace cv;

Spas Hristov gravatar imageSpas Hristov ( 2013-08-19 09:43:29 -0600 )edit

Question Tools

Stats

Asked: 2013-08-19 09:15:20 -0600

Seen: 6,098 times

Last updated: Apr 23 '14