首页 > 编程知识 正文

python图像处理opencv,Python调用摄像头

时间:2023-05-03 10:50:15 阅读:141551 作者:3990

为了获得良好的图像效果,需要设定照相机的参数。

当照相机流程是cap时,设置参数是cap.set (参数号,参数) )

获取参数值的函数是cap.get (参数编号)

让我们看一下照相机的参数设定读取的例子。 在代码中设置三个参数,然后读取这三个参数。

选择import cv2#相机编号,并通常从0开始cap=cv2.videocapture(0),然后选择参数cap.set (3,1280 ) cap.set (4,1024 ) cap.set (0) 0.1 ) print(width={}(.format ) cap.get(3) ) (print ) height={ } (.format ).cap.get ) ) ) )

width=1280.0

height=720.0

exposure=-1.0

宽度、高度、设定有效,但曝光量的设定恢复为无效。

相机可以设定什么样的参数? 参数号码的对应关系如何? 请看下面的列表。 0.cv _ cap _ prop _ pos _ mseccurrentpositionofthevideofileinmilliseconds.1.cv _ cap _ prop _ pos _ frames0- bames 0 tionofthevideofile3. cv _ cap _ prop _ frame _ wid de cap _ prop _ frame _ heightheightoftheframesinthevideostream.5.5. _ countnumberofframesinthevideofile.8.cv _ cap _ prop _ formatformatofthematobjectsreturnedbyretrieve (.9.cv _ cap ) rrentcapturemode.10.cv _ cap _ prop _ brightness brode.11.cv _ cap _ prop _ contrastcontrastoftheimage (only for cor ) op _ huehueoftheimage (only for cameras ).14.cv _ cap _ prop _ gaingainoftheimage (onlyforcamerage )。 15.cv _ cap _ prop _ exposure exposure (only for cameras ) )。 16.cv _ cap _ prop _ convert _ rgbbooleanflagsindicatingwhetherimagesshouldbeconvertedtorgb.17.cv _ cap _ prop _ prop ocameras (note : onlysupportedbydc 1394 v2.xbackendcurrently )此处

一般来说,读取返回-1的代码会无效。 让我们看看检测有效无效的代码。 上面的代码更改了

选择import cv2#相机号,通常从0开始先设定cap=cv2.videocapture(0),然后设定参数(forIinrange ) 47 ) (:print ) ) no.={ } parararint 读取'的cap.get(I ) ) while True: ret,img=cap.read ) cv2.imshow ),img )按ESC键退出key=c

v2.waitKey(10) if key == 27: breakcv2.destroyAllWindows() cv2.VideoCapture(0).release()

运行结果是:

0=0.0
1=0.0
2=-1.0
3=1280.0
4=720.0
5=30.0
6=842094158.0
7=-1.0
8=-1.0
9=0.0
10=0.0
11=0.0
12=64.0
13=0.0
14=0.0
15=-1.0
16=1.0
17=-1.0
18=-1.0
19=-1.0
20=2.0
21=-1.0
22=100.0
23=4600.0
24=-1.0
25=-1.0
26=-1.0
27=-1.0
28=-1.0
29=-1.0
30=-1.0
31=-1.0
32=3.0
33=-1.0
34=-1.0
35=-1.0
36=-1.0
37=-1.0
38=-1.0
39=-1.0
40=1.0
41=1.0
42=1400.0
43=-1.0
44=-1.0
45=-1.0
46=-1.0

可以看到我的摄像头哪些参数可以设置,不为-1的。

在程序中编号很容易弄错,可以用如下方式比较好看一点,也不容易出错。

cap.set(cv2.CAP_PROP_FRAME_WIDTH,1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,1024)
cap.set(cv2.CAP_PROP_EXPOSURE, 0.1)

上面表不完整,但基本都包括了我们看看现在opencv 里面怎么说的:

https://github.com/opencv/opencv/blob/master/modules/videoio/include/opencv2/videoio.hpp

enum VideoCaptureProperties { CAP_PROP_POS_MSEC =0, //!< Current position of the video file in milliseconds. CAP_PROP_POS_FRAMES =1, //!< 0-based index of the frame to be decoded/captured next. CAP_PROP_POS_AVI_RATIO =2, //!< Relative position of the video file: 0=start of the film, 1=end of the film. CAP_PROP_FRAME_WIDTH =3, //!< Width of the frames in the video stream. CAP_PROP_FRAME_HEIGHT =4, //!< Height of the frames in the video stream. CAP_PROP_FPS =5, //!< Frame rate. CAP_PROP_FOURCC =6, //!< 4-character code of codec. see VideoWriter::fourcc . CAP_PROP_FRAME_COUNT =7, //!< Number of frames in the video file. CAP_PROP_FORMAT =8, //!< Format of the %Mat objects returned by VideoCapture::retrieve(). CAP_PROP_MODE =9, //!< Backend-specific value indicating the current capture mode. CAP_PROP_BRIGHTNESS =10, //!< Brightness of the image (only for those cameras that support). CAP_PROP_CONTRAST =11, //!< Contrast of the image (only for cameras). CAP_PROP_SATURATION =12, //!< Saturation of the image (only for cameras). CAP_PROP_HUE =13, //!< Hue of the image (only for cameras). CAP_PROP_GAIN =14, //!< Gain of the image (only for those cameras that support). CAP_PROP_EXPOSURE =15, //!< Exposure (only for those cameras that support). CAP_PROP_CONVERT_RGB =16, //!< Boolean flags indicating whether images should be converted to RGB. CAP_PROP_WHITE_BALANCE_BLUE_U =17, //!< Currently unsupported. CAP_PROP_RECTIFICATION =18, //!< Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently). CAP_PROP_MONOCHROME =19, CAP_PROP_SHARPNESS =20, CAP_PROP_AUTO_EXPOSURE =21, //!< DC1394: exposure control done by camera, user can adjust reference level using this feature. CAP_PROP_GAMMA =22, CAP_PROP_TEMPERATURE =23, CAP_PROP_TRIGGER =24, CAP_PROP_TRIGGER_DELAY =25, CAP_PROP_WHITE_BALANCE_RED_V =26, CAP_PROP_csdl =27, CAP_PROP_FOCUS =28, CAP_PROP_GUID =29, CAP_PROP_ISO_SPEED =30, CAP_PROP_BACKLIGHT =32, CAP_PROP_PAN =33, CAP_PROP_TILT =34, CAP_PROP_ROLL =35, CAP_PROP_IRIS =36, CAP_PROP_SETTINGS =37, //!< Pop up video/camera filter dialog (note: only supported by DSHOW backend currently. The property value is ignored) CAP_PROP_BUFFERSIZE =38, CAP_PROP_AUTOFOCUS =39, CAP_PROP_SAR_NUM =40, //!< Sample aspect ratio: num/den (num) CAP_PROP_SAR_DEN =41, //!< Sample aspect ratio: num/den (den) CAP_PROP_BACKEND =42, //!< Current backend (enum VideoCaptureAPIs). Read-only property CAP_PROP_CHANNEL =43, //!< Video input or Channel Number (only for those cameras that support) CAP_PROP_AUTO_WB =44, //!< enable/ disable auto white-balance CAP_PROP_WB_TEMPERATURE=45, //!< white-balance color temperature#ifndef CV_DOXYGEN CV__CAP_PROP_LATEST#endif };

其实参数取值范围很重要,看来我还是要查找资料。下面是别人的一个例子。

capture.set(CV_CAP_PROP_FPS, 30);//帧率 帧/秒

capture.set(CV_CAP_PROP_BRIGHTNESS, 1);//亮度 

capture.set(CV_CAP_PROP_CONTRAST,40);//对比度 40

capture.set(CV_CAP_PROP_SATURATION, 50);//饱和度 50

capture.set(CV_CAP_PROP_HUE, 50);//色调 50

capture.set(CV_CAP_PROP_EXPOSURE, 50);//曝光 50 获取摄像头参数

要核实参数范围,我们用一个现成的软件,比如windows 的相机,或者AMcap,linux下的guvcview,cheese。

在设置好参数后,我们的python 程序获取他们的参数,是个比较好的办法。

然后用python运行获取所有参数:

for i in range(49): if abs(cap.get(i)+1)>0.001: print("{}.parameter={}".format(i,cap.get(i)))

我的运行结果如下:

ipdb> 0.parameter=0.0
1.parameter=0.0
3.parameter=1280.0
4.parameter=720.0
5.parameter=30.0
6.parameter=842094158.0
9.parameter=0.0
10.parameter=0.0
11.parameter=0.0
12.parameter=64.0
13.parameter=0.0
14.parameter=0.0
16.parameter=1.0
20.parameter=2.0
22.parameter=100.0
23.parameter=4600.0
32.parameter=3.0
40.parameter=1.0
41.parameter=1.0
42.parameter=1400.0

在不知道参数范围的情况下调整参数比较盲目。

下面看我用Amp 的参数调整对话框,大致可以看出参数调整的范围。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。