在我的C/C++程序中,我使用OpenCV从我的网络摄像头捕获图像.相机(罗技快IM)可以在分辨率捕捉320×240,640×480和1280×960.但是,由于一些奇怪的原因,OpenCV只给我分辨率为320x240的图像.使用带有其他分辨率值的cvSetCaptureProperty()更改分辨率的调用不起作用.如何使用我的网络摄像头以其他可能的分辨率捕获图像?
我在Windows下使用openCV 1.1pre1(Windows下这个版本的openCv默认使用videoinput库).
通过这些说明,我可以设置相机分辨率.请注意,我调用旧的cvCreateCameraCapture而不是cvCaptureFromCam.
capture = cvCreateCameraCapture(cameraIndex); cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 640 ); cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 480 ); videoFrame = cvQueryFrame(capture);
我用罗技,Trust和飞利浦网络摄像头对它进行了测试
似乎没有解决方案.分辨率可提高到640×480使用这个技巧的共享lifebelt77.以下是转载的细节:
添加到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
将函数icvSetPropertyCAM_VFW添加到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; }
并编辑captureCAM_VFW_vtable,如下所示:
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 };
现在重建了highgui.dll.
我之前在linux中完成了图像处理,并跳过了OpenCV的内置摄像头功能,因为它(如你所发现的)不完整.
根据您的操作系统,您可以通过正常渠道直接访问硬件,而不是通过openCV.如果您使用的是Linux,那么video4linux或video4linux2应该可以让您轻松访问USB网络摄像头,并且可以使用libavc1394进行火线.根据设备和您遵循的示例代码的质量,您应该能够在一两个小时内使用所需的参数运行设备.
编辑添加:如果是Windows,你就是自己的.我想这并不困难,但我从来没有这样做过.
我强烈建议使用VideoInput lib,它支持任何DirectShow设备(甚至同时支持多个设备),并且更易于配置.你将花费五分钟来玩OpenCV.
检查此票:https: //code.ros.org/trac/opencv/ticket/376
"解决方案是使用较新的基于libv4l的包装器.
安装libv4l-dev(这是在Ubuntu中调用的方式)
重新运行cmake,你会看到"V4L/V4L2:使用libv4l"
重新运行.现在分辨率可以改变.在MBP上进行了内置测试."
这使用Ubuntu为我修复它,并且可能也适合你.
代码我终于在Python中工作了一次Aaron Haun指出我需要在使用之前定义set函数的参数.
#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")