[ACCEPTED]-Increasing camera capture resolution in OpenCV-resolutions

Accepted answer
Score: 43

I'm using openCV 1.1pre1 under Windows (videoinput 6 library is used by default by this version 5 of openCv under windows).

With these instructions 4 I can set camera resolution. Note that I 3 call the old cvCreateCameraCapture instead 2 of cvCaptureFromCam.

capture = cvCreateCameraCapture(cameraIndex);

cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 640 );

cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 480 );


videoFrame = cvQueryFrame(capture);

I've tested it with 1 Logitech, Trust and Philips webcams

Score: 17

There doesn't seem to be a solution. The 4 resolution can be increased to 640x480 using this hack shared 3 by lifebelt77. Here are the details reproduced:

Add 2 to highgui.h:

#define CV_CAP_PROP_DIALOG_DISPLAY 8
#define CV_CAP_PROP_DIALOG_FORMAT 9
#define CV_CAP_PROP_DIALOG_SOURCE 10
#define CV_CAP_PROP_DIALOG_COMPRESSION 11
#define CV_CAP_PROP_FRAME_WIDTH_HEIGHT 12

Add the function icvSetPropertyCAM_VFW to cvcap.cpp:

static int icvSetPropertyCAM_VFW( CvCaptureCAM_VFW* capture, int property_id, double value )
{
    int result = -1;
    CAPSTATUS capstat;
    CAPTUREPARMS capparam;
    BITMAPINFO btmp;

    switch( property_id )
    {
        case CV_CAP_PROP_DIALOG_DISPLAY:
            result = capDlgVideoDisplay(capture->capWnd);
            //SendMessage(capture->capWnd,WM_CAP_DLG_VIDEODISPLAY,0,0);
            break;

        case CV_CAP_PROP_DIALOG_FORMAT:
            result = capDlgVideoFormat(capture->capWnd);
            //SendMessage(capture->capWnd,WM_CAP_DLG_VIDEOFORMAT,0,0);
            break;

        case CV_CAP_PROP_DIALOG_SOURCE:
            result = capDlgVideoSource(capture->capWnd);
            //SendMessage(capture->capWnd,WM_CAP_DLG_VIDEOSOURCE,0,0);
            break;

        case CV_CAP_PROP_DIALOG_COMPRESSION:
            result = capDlgVideoCompression(capture->capWnd);
            break;

        case CV_CAP_PROP_FRAME_WIDTH_HEIGHT:
            capGetVideoFormat(capture->capWnd, &btmp, sizeof(BITMAPINFO));
            btmp.bmiHeader.biWidth = floor(value/1000);
            btmp.bmiHeader.biHeight = value-floor(value/1000)*1000;
            btmp.bmiHeader.biSizeImage = btmp.bmiHeader.biHeight *
            btmp.bmiHeader.biWidth * btmp.bmiHeader.biPlanes *
            btmp.bmiHeader.biBitCount / 8;
            capSetVideoFormat(capture->capWnd, &btmp, sizeof(BITMAPINFO));
            break;

        default:
            break;
    }

    return result;
}

and edit captureCAM_VFW_vtable as following:

static CvCaptureVTable captureCAM_VFW_vtable =
{
6,
(CvCaptureCloseFunc)icvCloseCAM_VFW,
(CvCaptureGrabFrameFunc)icvGrabFrameCAM_VFW,
(CvCaptureRetrieveFrameFunc)icvRetrieveFrameCAM_VFW,
(CvCaptureGetPropertyFunc)icvGetPropertyCAM_VFW,
(CvCaptureSetPropertyFunc)icvSetPropertyCAM_VFW, // was NULL
(CvCaptureGetDescriptionFunc)0
};

Now 1 rebuilt highgui.dll.

Score: 5

I've done image processing in linux before 15 and skipped OpenCV's built in camera functionality 14 because it's (as you've discovered) incomplete.

Depending 13 on your OS you may have more luck going 12 straight to the hardware through normal 11 channels as opposed to through openCV. If 10 you are using Linux, video4linux or video4linux2 9 should give you relatively trivial access 8 to USB webcams and you can use libavc1394 7 for firewire. Depending on the device and 6 the quality of the example code you follow, you 5 should be able to get the device running 4 with the parameters you want in an hour 3 or two.

Edited to add: You are on your own 2 if its Windows. I imagine it's not much 1 more difficult but I've never done it.

Score: 5

I strongly suggest using VideoInput lib, it supports any 3 DirectShow device (even multiple devices 2 at the same time) and is more configurable. You'll 1 spend five minutes make it play with OpenCV.

Score: 5

Check this ticket out: https://code.ros.org/trac/opencv/ticket/376

"The solution 7 is to use the newer libv4l-based wrapper.

  1. install 6 libv4l-dev (this is how it's called in Ubuntu)

  2. rerun 5 cmake, you will see "V4L/V4L2: Using 4 libv4l"

  3. rerun make. now the resolution 3 can be changed. tested with built-in isight 2 on MBP."

This fixed it for me using 1 Ubuntu and might aswell work for you.

Score: 5

Code I finally got working in Python once 3 Aaron Haun pointed out I needed to define 2 the arguments of the set function before 1 using them.

#Camera_Get_Set.py
#By Forrest L. Erickson of VRX Company Inc. 8-31-12.
#Opens the camera and reads and reports the settings.
#Then tries to set for higher resolution.
#Workes with Logitech C525 for resolutions 960 by 720 and 1600 by 896


import cv2.cv as cv
import numpy

CV_CAP_PROP_POS_MSEC = 0
CV_CAP_PROP_POS_FRAMES = 1
CV_CAP_PROP_POS_AVI_RATIO = 2
CV_CAP_PROP_FRAME_WIDTH = 3
CV_CAP_PROP_FRAME_HEIGHT = 4
CV_CAP_PROP_FPS = 5
CV_CAP_PROP_POS_FOURCC = 6
CV_CAP_PROP_POS_FRAME_COUNT = 7
CV_CAP_PROP_BRIGHTNESS = 8
CV_CAP_PROP_CONTRAST = 9
CV_CAP_PROP_SATURATION = 10
CV_CAP_PROP_HUE = 11

CV_CAPTURE_PROPERTIES = tuple({
CV_CAP_PROP_POS_MSEC,
CV_CAP_PROP_POS_FRAMES,
CV_CAP_PROP_POS_AVI_RATIO,
CV_CAP_PROP_FRAME_WIDTH,
CV_CAP_PROP_FRAME_HEIGHT,
CV_CAP_PROP_FPS,
CV_CAP_PROP_POS_FOURCC,
CV_CAP_PROP_POS_FRAME_COUNT,
CV_CAP_PROP_BRIGHTNESS,
CV_CAP_PROP_CONTRAST,
CV_CAP_PROP_SATURATION,
CV_CAP_PROP_HUE})

CV_CAPTURE_PROPERTIES_NAMES = [
"CV_CAP_PROP_POS_MSEC",
"CV_CAP_PROP_POS_FRAMES",
"CV_CAP_PROP_POS_AVI_RATIO",
"CV_CAP_PROP_FRAME_WIDTH",
"CV_CAP_PROP_FRAME_HEIGHT",
"CV_CAP_PROP_FPS",
"CV_CAP_PROP_POS_FOURCC",
"CV_CAP_PROP_POS_FRAME_COUNT",
"CV_CAP_PROP_BRIGHTNESS",
"CV_CAP_PROP_CONTRAST",
"CV_CAP_PROP_SATURATION",
"CV_CAP_PROP_HUE"]


capture = cv.CaptureFromCAM(0)

print ("\nCamera properties before query of frame.")
for i in range(len(CV_CAPTURE_PROPERTIES_NAMES)):
#    camera_valeus =[CV_CAPTURE_PROPERTIES_NAMES, foo]
    foo = cv.GetCaptureProperty(capture, CV_CAPTURE_PROPERTIES[i])
    camera_values =[CV_CAPTURE_PROPERTIES_NAMES[i], foo]
#    print str(camera_values)
    print str(CV_CAPTURE_PROPERTIES_NAMES[i]) + ": " + str(foo)


print ("\nOpen a window for display of image")
cv.NamedWindow("Camera", 1)
while True:
    img = cv.QueryFrame(capture)
    cv.ShowImage("Camera", img)
    if cv.WaitKey(10) == 27:
        break
cv.DestroyWindow("Camera")


#cv.SetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 1024)
#cv.SetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 768)
cv.SetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 1600)
cv.SetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 896)


print ("\nCamera properties after query and display of frame.")
for i in range(len(CV_CAPTURE_PROPERTIES_NAMES)):
#    camera_valeus =[CV_CAPTURE_PROPERTIES_NAMES, foo]
    foo = cv.GetCaptureProperty(capture, CV_CAPTURE_PROPERTIES[i])
    camera_values =[CV_CAPTURE_PROPERTIES_NAMES[i], foo]
#    print str(camera_values)
    print str(CV_CAPTURE_PROPERTIES_NAMES[i]) + ": " + str(foo)


print ("/nOpen a window for display of image")
cv.NamedWindow("Camera", 1)
while True:
    img = cv.QueryFrame(capture)
    cv.ShowImage("Camera", img)
    if cv.WaitKey(10) == 27:
        break
cv.DestroyWindow("Camera")
Score: 4

I am using debian and ubuntu, i had the 5 same problem, i couldn't change the resolution 4 of video input using CV_CAP_PROP_FRAME_WIDTH and CV_CAP_PROP_FRAME_HEIGHT

I turned out that 3 the reason was a missing library. I installed 2 lib4l-dev through synaptic, rebuilt OpenCV and the problem 1 is SOLVED!

Score: 2

I am posting this to ensure that no one 10 else wastes time on this setproperty function. I 9 spent 2 days on this to see that nothing 8 seems to be working. So I dug out the code 7 (I had installed the library the first time 6 around). This is what actually happens - cvSetCaptureProperty, calls 5 setProperty inside CvCapture class and lo 4 behold setProperty does nothing. It just 3 returns false. Instead I'll pick up using 2 another library to feed OpenCV a capture 1 video/images. I am using OpenCV 2.2

Score: 0

I find that in Windows (from Win98 to WinXP 23 SP3), OpenCV will often use Microsoft's 22 VFW library for camera access. The problem 21 with this is that it is often very slow 20 (say a max of 15 FPS frame capture) and 19 buggy (hence why cvSetCaptureProperty often 18 doesn't work). Luckily, you can usually 17 change the resolution in other software 16 (particularly "AMCAP", which is 15 a demo program that is easily available) and 14 it will effect the resolution that OpenCV 13 will use. For example, you can run AMCAP 12 to set the resolution to 640x480, and then 11 OpenCV will use that by default from that 10 point onwards!

But if you can use a different 9 Windows camera access library such as the 8 "videoInput" library http://muonics.net/school/spring05/videoInput/ that accesses 7 the camera using very efficient DirectShow 6 (part of DirectX). Or if you have a professional 5 quality camera, then often it will come 4 with a custom API that lets you access the 3 camera, and you could use that for fast 2 access with the ability to change resolution 1 and many other things.

Score: 0

Under Windows try to use VideoInput library: http://robocraft.ru/blog/computervision/420.html

0

Score: 0

cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, WIDTH 3 );

cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, HEIGHT);

cvQueryFrame(capture);

That 2 will not work with OpenCV 2.2, but if you 1 use OpenCV 2.1 it will work fine !

Score: 0

If you are on windows platform, try DirectShow 1 (IAMStreamConfig).

http://msdn.microsoft.com/en-us/library/dd319784%28v=vs.85%29.aspx

Score: 0

Just one information that could be valuable 21 for people having difficulties to change 20 the default capture resolution (640 x 480) ! I 19 experimented myself a such problem with 18 opencv 2.4.x and one Logitech camera ... and 17 found one workaround !

The behaviour I detected 16 is that the default format is setup as initial 15 parameters when camera capture is started 14 (cvCreateCameraCapture), and all request 13 to change height or width :

cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, ... 

or

cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, ...

are not possible 12 afterwards ! Effectively, I discovered with 11 adding return error of ioctl functions that 10 V4l2 driver is returning EBUSY for thet requests 9 ! Therefore, one workaround should be to 8 change the default value directly in highgui/cap_v4l.cpp 7 :

*#define DEFAULT_V4L_WIDTH  1280    // Originally 640* 

*#define DEFAULT_V4L_HEIGHT 720     // Originally 480*

After that, I just recompiled opencv ... and 6 arrived to get 1280 x 720 without any problem 5 ! Of course, a better fix should be to stop 4 the acquisition, change the parameters, and 3 restart stream after, but I'm not enough 2 familiar with opencv for doing that !

Hope 1 it will help.

Michel BEGEY

More Related questions