python opencv设置摄像头分辨率以及各个参数的方法
1,为了获取视频,你应该创建一个VideoCapture对象。他的参数可以是设备的索引号,或者是一个视频文件。设备索引号就是在指定要使用的摄像头。一般的笔记本电脑都有内置摄像头。所以参数就是0。你可以通过设置成1或者其他的来选择别的摄像头。之后,你就可以一帧一帧的捕获视频了。但是最后,别忘了停止捕获视频。使用ls/dev/video*命令可以查看摄像头设备
2,cap.read()返回一个布尔值(True/False)。如果帧读取的是正确的,就是True。所以最后你可以通过检查他的返回值来查看视频文件是否已经到了结尾。有时cap可能不能成功的初始化摄像头设备。这种情况下上面的代码会报错。你可以使用cap.isOpened(),来检查是否成功初始化了。如果返回值是True,那就没有问题。否则就要使用函数cap.open()。你可以使用函数cap.get(propId)来获得视频的一些参数信息。这里propId可以是0到18之间的任何整数。每一个数代表视频的一个属性,见表其中的一些值可以使用cap.set(propId,value)来修改,value就是
你想要设置成的新值。例如,我可以使用cap.get(3)和cap.get(4)来查看每一帧的宽和高。默认情况下得到的值是640X480。但是我可以使用ret=cap.set(3,320)和ret=cap.set(4,240)来把宽和高改成320X240。
CV_CAP_PROP_POS_MSECCurrentpositionofthevideofileinmilliseconds. •CV_CAP_PROP_POS_FRAMES0-basedindexoftheframetobedecoded/capturednext. •CV_CAP_PROP_POS_AVI_RATIORelativepositionofthevideofile:0-startofthefilm,1-endofthefilm. •CV_CAP_PROP_FRAME_WIDTHWidthoftheframesinthevideostream. •CV_CAP_PROP_FRAME_HEIGHTHeightoftheframesinthevideostream. •CV_CAP_PROP_FPSFramerate. •CV_CAP_PROP_FOURCC4-charactercodeofcodec. •CV_CAP_PROP_FRAME_COUNTNumberofframesinthevideofile. •CV_CAP_PROP_FORMATFormatoftheMatobjectsreturnedbyretrieve(). •CV_CAP_PROP_MODEBackend-specificvalueindicatingthecurrentcapturemode. •CV_CAP_PROP_BRIGHTNESSBrightnessoftheimage(onlyforcameras). •CV_CAP_PROP_CONTRASTContrastoftheimage(onlyforcameras). •CV_CAP_PROP_SATURATIONSaturationoftheimage(onlyforcameras). •CV_CAP_PROP_HUEHueoftheimage(onlyforcameras). •CV_CAP_PROP_GAINGainoftheimage(onlyforcameras). •CV_CAP_PROP_EXPOSUREExposure(onlyforcameras). •CV_CAP_PROP_CONVERT_RGBBooleanflagswhetherimagesshouldbeconvertedtoRGB.indicating •CV_CAP_PROP_WHITE_BALANCECurrentlyunsupported •CV_CAP_PROP_RECTIFICATIONRectificationflagforstereocameras(note:onlysupportedbyDC1394v2.xbackendcur-rently)
#!/usr/bin/envpython
#-*-coding:utf-8-*-
importcv2
importnumpy
fromhlf_moduleimporthlf_define
fromstd_msgs.msgimportString
importmatplotlib.pyplotasplot
importxml.dom.minidom
importpylab
importrospy
importtime
cap=cv2.VideoCapture(0)
cap.set(3,640)#设置分辨率
cap.set(4,480)
fps=cap.get(cv2.CAP_PROP_FPS)#获取视频帧数
face_casade=cv2.CascadeClassifier('/opt/ros/kinetic/share/OpenCV-3.2.0-dev/haarcascades/haarcascade_frontalface_default.xml')
Node_name='neck'
#printcap.isOpened()
classDetect_face():
def__init__(self):
'''定义节点Node_name(全局变量,而非具体名称)'''
self.err_pub=hlf_define.err_publisher()#错误消息发布者
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)
defhead_motor_value(self):#解析xml文件获取舵机的范围值
dom=xml.dom.minidom.parse('/home/sb/catkin_ws/src/hlf_robot/scripts/hlf_action/head_value.xml')
#得到文档元素对象
root=dom.documentElement
itemlist=root.getElementsByTagName('login')
item=itemlist[0]
max_value=item.getAttribute("max")
min_value=item.getAttribute("min")
returnmax_value,min_value
defdetect_face(self):
#getaframe
#frame=cv2.imread('/home/sb/桌面/timg.jpeg')
ret,frame=cap.read()
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)#转成灰度图
#frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
#showaframe
cv2.imshow("capture",gray)
faces=face_casade.detectMultiScale(gray,1.2,5)#检测人脸
#printlen(faces)
iflen(faces)>0:#判断是否检测到人脸
result=()
max_face=0
value_x=0
for(x,y,w,h)infaces:
if(w*h>max_face):#检测最大人脸
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
returnz
else:
return1
if__name__=='__main__':
face=Detect_face()
motor_max,motor_min=face.head_motor_value()
x=[]
i=1
whileTrue:
try:
z=face.detect_face()
ifz!=1:
x.append(z)
iflen(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)#转换成舵机值头部向左转
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:
ifi==fps:
face.neck_puber.publish('1500')
i=1
else:
i+=1
print(U'未检测到人脸')
ifcv2.waitKey(1)&0xFF==ord('q'):
break
exceptException,e:
printe
cap.release()
cv2.destroyAllWindows()
以上这篇pythonopencv设置摄像头分辨率以及各个参数的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。