<p>In my C/C++ program, I'm using <a href="http://opencvlibrary.sourceforge.net/" rel="noreferrer">OpenCV</a> to capture images from my webcam. The camera (<a href="http://en.wikipedia.org/wiki/Quickcam" rel="noreferrer">Logitech QuickCam IM</a>) can capture at resolutions <strong>320x240</strong>, <strong>640x480</strong> and <strong>1280x960</strong>. But, for some strange reason, OpenCV gives me images of resolution <strong>320x240</strong> only. Calls to change the resolution using <strong>cvSetCaptureProperty()</strong> with other resolution values just don't work. How do I capture images with the other resolutions possible with my webcam?</p>

[{'answer_id': 14290, 'author': 'Ashwin Nanjappa', 'author_id': 1630, 'author_profile': 'https://Stackoverflow.com/users/1630', 'pm_score': 5, 'selected': True, 'text': '<p>There doesn\'t seem to be a solution. The resolution can be increased to <strong>640x480</strong> using <a href="http://tech.groups.yahoo.com/group/OpenCV/message/28735" rel="noreferrer">this hack</a> shared by <em>lifebelt77</em>. Here are the details reproduced:</p>\n\n<p>Add to <strong>highgui.h</strong>:</p>\n\n<pre><code>#define CV_CAP_PROP_DIALOG_DISPLAY 8\n#define CV_CAP_PROP_DIALOG_FORMAT 9\n#define CV_CAP_PROP_DIALOG_SOURCE 10\n#define CV_CAP_PROP_DIALOG_COMPRESSION 11\n#define CV_CAP_PROP_FRAME_WIDTH_HEIGHT 12\n</code></pre>\n\n<p>Add the function <strong>icvSetPropertyCAM_VFW</strong> to <strong>cvcap.cpp</strong>:</p>\n\n<pre><code>static int icvSetPropertyCAM_VFW( CvCaptureCAM_VFW* capture, int property_id, double value )\n{\n    int result = -1;\n    CAPSTATUS capstat;\n    CAPTUREPARMS capparam;\n    BITMAPINFO btmp;\n\n    switch( property_id )\n    {\n        case CV_CAP_PROP_DIALOG_DISPLAY:\n            result = capDlgVideoDisplay(capture-&gt;capWnd);\n            //SendMessage(capture-&gt;capWnd,WM_CAP_DLG_VIDEODISPLAY,0,0);\n            break;\n\n        case CV_CAP_PROP_DIALOG_FORMAT:\n            result = capDlgVideoFormat(capture-&gt;capWnd);\n            //SendMessage(capture-&gt;capWnd,WM_CAP_DLG_VIDEOFORMAT,0,0);\n            break;\n\n        case CV_CAP_PROP_DIALOG_SOURCE:\n            result = capDlgVideoSource(capture-&gt;capWnd);\n            //SendMessage(capture-&gt;capWnd,WM_CAP_DLG_VIDEOSOURCE,0,0);\n            break;\n\n        case CV_CAP_PROP_DIALOG_COMPRESSION:\n            result = capDlgVideoCompression(capture-&gt;capWnd);\n            break;\n\n        case CV_CAP_PROP_FRAME_WIDTH_HEIGHT:\n            capGetVideoFormat(capture-&gt;capWnd, &amp;btmp, sizeof(BITMAPINFO));\n            btmp.bmiHeader.biWidth = floor(value/1000);\n            btmp.bmiHeader.biHeight = value-floor(value/1000)*1000;\n            btmp.bmiHeader.biSizeImage = btmp.bmiHeader.biHeight *\n            btmp.bmiHeader.biWidth * btmp.bmiHeader.biPlanes *\n            btmp.bmiHeader.biBitCount / 8;\n            capSetVideoFormat(capture-&gt;capWnd, &amp;btmp, sizeof(BITMAPINFO));\n            break;\n\n        default:\n            break;\n    }\n\n    return result;\n}\n</code></pre>\n\n<p>and edit <strong>captureCAM_VFW_vtable</strong> as following:</p>\n\n<pre><code>static CvCaptureVTable captureCAM_VFW_vtable =\n{\n6,\n(CvCaptureCloseFunc)icvCloseCAM_VFW,\n(CvCaptureGrabFrameFunc)icvGrabFrameCAM_VFW,\n(CvCaptureRetrieveFrameFunc)icvRetrieveFrameCAM_VFW,\n(CvCaptureGetPropertyFunc)icvGetPropertyCAM_VFW,\n(CvCaptureSetPropertyFunc)icvSetPropertyCAM_VFW, // was NULL\n(CvCaptureGetDescriptionFunc)0\n};\n</code></pre>\n\n<p>Now rebuilt <strong>highgui.dll</strong>.</p>\n'}, {'answer_id': 15422, 'author': 'Louis Brandy', 'author_id': 2089740, 'author_profile': 'https://Stackoverflow.com/users/2089740', 'pm_score': 3, 'selected': False, 'text': "<p>I've done image processing in linux before and skipped OpenCV's built in camera functionality because it's (as you've discovered) incomplete.</p>\n\n<p>Depending on your OS you may have more luck going straight to the hardware through normal channels as opposed to through openCV. If you are using Linux, video4linux or video4linux2 should give you relatively trivial access to USB webcams and you can use libavc1394 for firewire. Depending on the device and the quality of the example code you follow, you should be able to get the device running with the parameters you want in an hour or two. </p>\n\n<p>Edited to add: You are on your own if its Windows. I imagine it's not much more difficult but I've never done it.</p>\n"}, {'answer_id': 38187, 'author': 'martjno', 'author_id': 3373, 'author_profile': 'https://Stackoverflow.com/users/3373', 'pm_score': 3, 'selected': False, 'text': '<p>I strongly suggest using <a href="http://muonics.net/school/spring05/videoInput/" rel="nofollow noreferrer">VideoInput lib</a>, it supports any DirectShow device (even multiple devices at the same time) and is more configurable. You\'ll spend five minutes make it play with OpenCV.</p>\n'}, {'answer_id': 713742, 'author': 'Grifo', 'author_id': 86629, 'author_profile': 'https://Stackoverflow.com/users/86629', 'pm_score': 5, 'selected': False, 'text': "<p>I'm using openCV 1.1pre1 under Windows (videoinput library is used by default by this version of openCv under windows).</p>\n\n<p>With these instructions I can set camera resolution. Note that I call the old cvCreateCameraCapture instead of cvCaptureFromCam.</p>\n\n<pre><code>capture = cvCreateCameraCapture(cameraIndex);\n\ncvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 640 );\n\ncvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 480 );\n\n\nvideoFrame = cvQueryFrame(capture);\n</code></pre>\n\n<p>I've tested it with Logitech, Trust and Philips webcams</p>\n"}, {'answer_id': 3076521, 'author': 'mg72', 'author_id': 371133, 'author_profile': 'https://Stackoverflow.com/users/371133', 'pm_score': -1, 'selected': False, 'text': '<pre><code>cvQueryFrame(capture);\n\ncvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, any_supported_size );\n\ncvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, any_supported_size);\n\ncvQueryFrame(capture);\n</code></pre>\n\n<p>should be just enough!</p>\n'}, {'answer_id': 3867859, 'author': 'fantom1210', 'author_id': 467332, 'author_profile': 'https://Stackoverflow.com/users/467332', 'pm_score': -1, 'selected': False, 'text': '<p>Try this:</p>\n\n<pre><code>capture = cvCreateCameraCapture(-1);\n//set resolution\ncvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, frameWidth);\ncvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, frameHeight);\n</code></pre>\n'}, {'answer_id': 4041979, 'author': 'Yo-L', 'author_id': 310108, 'author_profile': 'https://Stackoverflow.com/users/310108', 'pm_score': 3, 'selected': False, 'text': '<p>Check this ticket out:\n<a href="https://code.ros.org/trac/opencv/ticket/376" rel="noreferrer">https://code.ros.org/trac/opencv/ticket/376</a></p>\n\n<p>"The solution is to use the newer libv4l-based wrapper.</p>\n\n<ol>\n<li><p>install libv4l-dev (this is how it\'s called in Ubuntu)</p></li>\n<li><p>rerun cmake, you will see "V4L/V4L2: Using libv4l"</p></li>\n<li><p>rerun make. now the resolution can be changed. tested with built-in isight on MBP."</p></li>\n</ol>\n\n<p>This fixed it for me using Ubuntu and might aswell work for you.</p>\n'}, {'answer_id': 4157963, 'author': 'Shervin Emami', 'author_id': 199142, 'author_profile': 'https://Stackoverflow.com/users/199142', 'pm_score': 0, 'selected': False, 'text': '<p>I find that in Windows (from Win98 to WinXP SP3), OpenCV will often use Microsoft\'s VFW library for camera access. The problem with this is that it is often very slow (say a max of 15 FPS frame capture) and buggy (hence why cvSetCaptureProperty often doesn\'t work). Luckily, you can usually change the resolution in other software (particularly "AMCAP", which is a demo program that is easily available) and it will effect the resolution that OpenCV will use. For example, you can run AMCAP to set the resolution to 640x480, and then OpenCV will use that by default from that point onwards!</p>\n\n<p>But if you can use a different Windows camera access library such as the "videoInput" library <a href="http://muonics.net/school/spring05/videoInput/" rel="nofollow">http://muonics.net/school/spring05/videoInput/</a> that accesses the camera using very efficient DirectShow (part of DirectX). Or if you have a professional quality camera, then often it will come with a custom API that lets you access the camera, and you could use that for fast access with the ability to change resolution and many other things.</p>\n'}, {'answer_id': 5379910, 'author': 'noonv', 'author_id': 421699, 'author_profile': 'https://Stackoverflow.com/users/421699', 'pm_score': 0, 'selected': False, 'text': '<p>Under Windows try to use VideoInput library:\n<a href="http://robocraft.ru/blog/computervision/420.html" rel="nofollow">http://robocraft.ru/blog/computervision/420.html</a></p>\n'}, {'answer_id': 5816916, 'author': 'evangelos', 'author_id': 729049, 'author_profile': 'https://Stackoverflow.com/users/729049', 'pm_score': 2, 'selected': False, 'text': "<p>I am using debian and ubuntu, i had the same problem, i couldn't change the resolution of video input using <strong>CV_CAP_PROP_FRAME_WIDTH</strong> and <strong>CV_CAP_PROP_FRAME_HEIGHT</strong></p>\n\n<p>I turned out that the reason was a missing library.\nI installed <strong>lib4l-dev</strong> through synaptic, <em>rebuilt</em> OpenCV and the problem is SOLVED! </p>\n"}, {'answer_id': 5849244, 'author': 'CyraX', 'author_id': 727013, 'author_profile': 'https://Stackoverflow.com/users/727013', 'pm_score': 2, 'selected': False, 'text': "<p>I am posting this to ensure that no one else wastes time on this setproperty function. I spent 2 days on this to see that nothing seems to be working. So I dug out the code (I had installed the library the first time around). This is what actually happens - cvSetCaptureProperty, calls setProperty inside CvCapture class and lo behold setProperty does nothing. It just returns false.\nInstead I'll pick up using another library to feed OpenCV a capture video/images. I am using OpenCV 2.2</p>\n"}, {'answer_id': 6250280, 'author': 'William', 'author_id': 785686, 'author_profile': 'https://Stackoverflow.com/users/785686', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n<p>cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, WIDTH );</p>\n<p>cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, HEIGHT);</p>\n<p>cvQueryFrame(capture);</p>\n</blockquote>\n<p>That will not work with OpenCV 2.2, but if you use OpenCV 2.1 it will work fine !</p>\n'}, {'answer_id': 6321334, 'author': 'plan9assembler', 'author_id': 1710672, 'author_profile': 'https://Stackoverflow.com/users/1710672', 'pm_score': 0, 'selected': False, 'text': '<p>If you are on windows platform, try DirectShow (IAMStreamConfig).</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/dd319784%28v=vs.85%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/dd319784%28v=vs.85%29.aspx</a></p>\n'}, {'answer_id': 12253652, 'author': 'Forrest Erickson', 'author_id': 1644581, 'author_profile': 'https://Stackoverflow.com/users/1644581', 'pm_score': 3, 'selected': False, 'text': '<p>Code I finally got working in Python once Aaron Haun pointed out I needed to define the arguments of the set function before using them.</p>\n\n<pre><code>#Camera_Get_Set.py\n#By Forrest L. Erickson of VRX Company Inc. 8-31-12.\n#Opens the camera and reads and reports the settings.\n#Then tries to set for higher resolution.\n#Workes with Logitech C525 for resolutions 960 by 720 and 1600 by 896\n\n\nimport cv2.cv as cv\nimport numpy\n\nCV_CAP_PROP_POS_MSEC = 0\nCV_CAP_PROP_POS_FRAMES = 1\nCV_CAP_PROP_POS_AVI_RATIO = 2\nCV_CAP_PROP_FRAME_WIDTH = 3\nCV_CAP_PROP_FRAME_HEIGHT = 4\nCV_CAP_PROP_FPS = 5\nCV_CAP_PROP_POS_FOURCC = 6\nCV_CAP_PROP_POS_FRAME_COUNT = 7\nCV_CAP_PROP_BRIGHTNESS = 8\nCV_CAP_PROP_CONTRAST = 9\nCV_CAP_PROP_SATURATION = 10\nCV_CAP_PROP_HUE = 11\n\nCV_CAPTURE_PROPERTIES = tuple({\nCV_CAP_PROP_POS_MSEC,\nCV_CAP_PROP_POS_FRAMES,\nCV_CAP_PROP_POS_AVI_RATIO,\nCV_CAP_PROP_FRAME_WIDTH,\nCV_CAP_PROP_FRAME_HEIGHT,\nCV_CAP_PROP_FPS,\nCV_CAP_PROP_POS_FOURCC,\nCV_CAP_PROP_POS_FRAME_COUNT,\nCV_CAP_PROP_BRIGHTNESS,\nCV_CAP_PROP_CONTRAST,\nCV_CAP_PROP_SATURATION,\nCV_CAP_PROP_HUE})\n\nCV_CAPTURE_PROPERTIES_NAMES = [\n"CV_CAP_PROP_POS_MSEC",\n"CV_CAP_PROP_POS_FRAMES",\n"CV_CAP_PROP_POS_AVI_RATIO",\n"CV_CAP_PROP_FRAME_WIDTH",\n"CV_CAP_PROP_FRAME_HEIGHT",\n"CV_CAP_PROP_FPS",\n"CV_CAP_PROP_POS_FOURCC",\n"CV_CAP_PROP_POS_FRAME_COUNT",\n"CV_CAP_PROP_BRIGHTNESS",\n"CV_CAP_PROP_CONTRAST",\n"CV_CAP_PROP_SATURATION",\n"CV_CAP_PROP_HUE"]\n\n\ncapture = cv.CaptureFromCAM(0)\n\nprint ("\\nCamera properties before query of frame.")\nfor i in range(len(CV_CAPTURE_PROPERTIES_NAMES)):\n#    camera_valeus =[CV_CAPTURE_PROPERTIES_NAMES, foo]\n    foo = cv.GetCaptureProperty(capture, CV_CAPTURE_PROPERTIES[i])\n    camera_values =[CV_CAPTURE_PROPERTIES_NAMES[i], foo]\n#    print str(camera_values)\n    print str(CV_CAPTURE_PROPERTIES_NAMES[i]) + ": " + str(foo)\n\n\nprint ("\\nOpen a window for display of image")\ncv.NamedWindow("Camera", 1)\nwhile True:\n    img = cv.QueryFrame(capture)\n    cv.ShowImage("Camera", img)\n    if cv.WaitKey(10) == 27:\n        break\ncv.DestroyWindow("Camera")\n\n\n#cv.SetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 1024)\n#cv.SetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 768)\ncv.SetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 1600)\ncv.SetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 896)\n\n\nprint ("\\nCamera properties after query and display of frame.")\nfor i in range(len(CV_CAPTURE_PROPERTIES_NAMES)):\n#    camera_valeus =[CV_CAPTURE_PROPERTIES_NAMES, foo]\n    foo = cv.GetCaptureProperty(capture, CV_CAPTURE_PROPERTIES[i])\n    camera_values =[CV_CAPTURE_PROPERTIES_NAMES[i], foo]\n#    print str(camera_values)\n    print str(CV_CAPTURE_PROPERTIES_NAMES[i]) + ": " + str(foo)\n\n\nprint ("/nOpen a window for display of image")\ncv.NamedWindow("Camera", 1)\nwhile True:\n    img = cv.QueryFrame(capture)\n    cv.ShowImage("Camera", img)\n    if cv.WaitKey(10) == 27:\n        break\ncv.DestroyWindow("Camera")\n</code></pre>\n'}, {'answer_id': 19108254, 'author': 'user2833455', 'author_id': 2833455, 'author_profile': 'https://Stackoverflow.com/users/2833455', 'pm_score': 0, 'selected': False, 'text': "<p>Just one information that could be valuable for people having difficulties to change the default capture resolution (640 x 480) ! I experimented myself a such problem with opencv 2.4.x and one Logitech camera ... and found one workaround !</p>\n\n<p>The behaviour I detected is that the default format is setup as initial parameters when camera capture is started (cvCreateCameraCapture), and all request to change height or width :</p>\n\n<pre><code>cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, ... \n</code></pre>\n\n<p>or</p>\n\n<pre><code>cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, ...\n</code></pre>\n\n<p>are not possible afterwards ! Effectively, I discovered with adding return error of ioctl functions that V4l2 driver is returning <strong>EBUSY</strong> for thet requests ! \nTherefore, one workaround should be to change the default value directly in highgui/cap_v4l.cpp :</p>\n\n<pre><code>*#define DEFAULT_V4L_WIDTH  1280    // Originally 640* \n\n*#define DEFAULT_V4L_HEIGHT 720     // Originally 480*\n</code></pre>\n\n<p>After that, I just recompiled <strong>opencv</strong> ... and arrived to get 1280 x 720 without any problem ! Of course, a better fix should be to stop the acquisition, change the parameters, and restart stream after, but I'm not enough familiar with opencv for doing that !</p>\n\n<p>Hope it will help.</p>\n\n<p>Michel BEGEY</p>\n"}]
['https://Stackoverflow.com/questions/14287', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1630/']