python opencv設(shè)置攝像頭分辨率以及各個(gè)參數(shù)的方法
1,為了獲取視頻,你應(yīng)該創(chuàng)建一個(gè) VideoCapture 對(duì)象。他的參數(shù)可以是設(shè)備的索引號(hào),或者是一個(gè)視頻文件。設(shè)備索引號(hào)就是在指定要使用的攝像頭。一般的筆記本電腦都有內(nèi)置攝像頭。所以參數(shù)就是 0。你可以通過(guò)設(shè)置成 1 或者其他的來(lái)選擇別的攝像頭。之后,你就可以一幀一幀的捕獲視頻了。但是最后,別忘了停止捕獲視頻。使用 ls /dev/video*命令可以查看攝像頭設(shè)備
2,cap.read() 返回一個(gè)布爾值(True/False)。如果幀讀取的是正確的,就是 True。所以最后你可以通過(guò)檢查他的返回值來(lái)查看視頻文件是否已經(jīng)到了結(jié)尾。有時(shí) cap 可能不能成功的初始化攝像頭設(shè)備。這種情況下上面的代碼會(huì)報(bào)錯(cuò)。你可以使用 cap.isOpened(),來(lái)檢查是否成功初始化了。如果返回值是True,那就沒(méi)有問(wèn)題。否則就要使用函數(shù) cap.open()。你可以使用函數(shù) cap.get(propId) 來(lái)獲得視頻的一些參數(shù)信息。這里propId 可以是 0 到 18 之間的任何整數(shù)。每一個(gè)數(shù)代表視頻的一個(gè)屬性,見(jiàn)表其中的一些值可以使用cap.set(propId,value) 來(lái)修改,value 就是
你想要設(shè)置成的新值。例如,我可以使用 cap.get(3) 和 cap.get(4) 來(lái)查看每一幀的寬和高。默認(rèn)情況下得到的值是 640X480。但是我可以使用 ret=cap.set(3,320)和 ret=cap.set(4,240) 來(lái)把寬和高改成 320X240。
CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds. • CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next. • CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film. • CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream. • CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream. • CV_CAP_PROP_FPS Frame rate. • CV_CAP_PROP_FOURCC 4-character code of codec. • CV_CAP_PROP_FRAME_COUNT Number of frames in the video file. • CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() . • CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode. • CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras). • CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras). • CV_CAP_PROP_SATURATION Saturation of the image (only for cameras). • CV_CAP_PROP_HUE Hue of the image (only for cameras). • CV_CAP_PROP_GAIN Gain of the image (only for cameras). • CV_CAP_PROP_EXPOSURE Exposure (only for cameras). • CV_CAP_PROP_CONVERT_RGB Boolean flags whether images should be converted to RGB. indicating • CV_CAP_PROP_WHITE_BALANCE Currently unsupported • CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend cur-rently)
#!/usr/bin/env python # -*- coding: utf-8 -*- import cv2 import numpy from hlf_module import hlf_define from std_msgs.msg import String import matplotlib.pyplot as plot import xml.dom.minidom import pylab import rospy import time cap = cv2.VideoCapture(0) cap.set(3,640) #設(shè)置分辨率 cap.set(4,480) fps =cap.get(cv2.CAP_PROP_FPS) #獲取視頻幀數(shù) face_casade = cv2.CascadeClassifier('/opt/ros/kinetic/share/OpenCV-3.2.0-dev/haarcascades/haarcascade_frontalface_default.xml') Node_name='neck' #print cap.isOpened() class Detect_face(): def __init__(self): '''定義節(jié)點(diǎn)Node_name(全局變量,而非具體名稱)''' self.err_pub=hlf_define.err_publisher()#錯(cuò)誤消息發(fā)布者 rospy.init_node(Node_name,anonymous=True) self.neck_puber=rospy.Publisher(hlf_define.TOPIC_ACTION_NECK,String,queue_size=10) time.sleep(0.5) def head_motor_value(self):#解析xml文件 獲取舵機(jī)的范圍值 dom = xml.dom.minidom.parse('/home/sb/catkin_ws/src/hlf_robot/scripts/hlf_action/head_value.xml') #得到文檔元素對(duì)象 root = dom.documentElement itemlist = root.getElementsByTagName('login') item = itemlist[0] max_value=item.getAttribute("max") min_value=item.getAttribute("min") return max_value,min_value def detect_face(self): # get a frame #frame=cv2.imread('/home/sb/桌面/timg.jpeg') ret, frame = cap.read() gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)#轉(zhuǎn)成灰度圖 #frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) # show a frame cv2.imshow("capture", gray) faces = face_casade.detectMultiScale(gray,1.2,5) #檢測(cè)人臉 #print len(faces) if len(faces)>0:#判斷是否檢測(cè)到人臉 result = () max_face = 0 value_x=0 for (x,y,w,h) in faces: if (w*h > max_face): #檢測(cè)最大人臉 max_face = w*h result = (x,y,w,h) # max_face.append(width*height) x=result[0] w=result[2] z=value_x=value_x+x+w/2 return z else: return 1 if __name__=='__main__': face=Detect_face() motor_max,motor_min= face.head_motor_value() x=[] i=1 while True: try: z=face.detect_face() if z != 1: x.append(z) if len(x)>(fps-1): true_x = int(sum(x)/30) if(true_x>319): motor_value=int(1500+(int(motor_max)-1500)*(true_x-319)/320)#轉(zhuǎn)換成舵機(jī)值 頭部向左轉(zhuǎn) face.neck_puber.publish('%s'%motor_value) elif (true_x<319): motor_value=int(1500-(1500-int(motor_min))*(319-true_x)/320) face.neck_puber.publish('%s'%motor_value) x=[] else: if i==fps: face.neck_puber.publish('1500') i=1 else: i +=1 print (U'未檢測(cè)到人臉') if cv2.waitKey(1) & 0xFF == ord('q'): break except Exception,e: print e cap.release() cv2.destroyAllWindows()
以上這篇python opencv設(shè)置攝像頭分辨率以及各個(gè)參數(shù)的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python的格式化輸出(format,%)實(shí)例詳解
Python中格式化字符串目前有兩種陣營(yíng):%和format,哪一種比較適合我們使用呢?下面腳本之家小編給大家介紹下python的格式化輸出(format,%)實(shí)例詳解,感興趣的朋友一起看看吧2018-06-06Gradio機(jī)器學(xué)習(xí)模型快速部署工具應(yīng)用分享
這篇文章主要為大家介紹了Gradio機(jī)器學(xué)習(xí)模型快速部署工具應(yīng)用分享原文翻譯,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Python實(shí)現(xiàn)數(shù)據(jù)庫(kù)編程方法詳解
這篇文章主要介紹了Python實(shí)現(xiàn)數(shù)據(jù)庫(kù)編程方法,較為詳細(xì)的總結(jié)了Python數(shù)據(jù)庫(kù)編程涉及的各種常用技巧與相關(guān)組件,需要的朋友可以參考下2015-06-06Python?IDLE?Subprocess?Connection?Error的簡(jiǎn)單解決方法
最近用要Python處理一點(diǎn)事,就打開(kāi)Python IDLE,結(jié)果出現(xiàn)錯(cuò)誤,下面這篇文章主要給大家介紹了關(guān)于Python?IDLE?Subprocess?Connection?Error的簡(jiǎn)單解決方法,需要的朋友可以參考下2023-01-01