python opencv设置摄像头分辨率以及各个参数的方法
作者:qq_31077649 时间:2021-03-05 00:23:34
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_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) #设置分辨率
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'
#print cap.isOpened()
class Detect_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)
def head_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")
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)#转成灰度图
#frame=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
# show a frame
cv2.imshow("capture", gray)
faces = face_casade.detectMultiScale(gray,1.2,5) #检测人脸
#print len(faces)
if len(faces)>0:#判断是否检测到人脸
result = ()
max_face = 0
value_x=0
for (x,y,w,h) in faces:
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
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)#转换成舵机值 头部向左转
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'未检测到人脸')
if cv2.waitKey(1) & 0xFF == ord('q'):
break
except Exception,e:
print e
cap.release()
cv2.destroyAllWindows()
来源:https://blog.csdn.net/qq_31077649/article/details/75194719
标签:opencv,摄像头,分辨率,python,参数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在Python运行时动态查看进程内部信息的方法
2021-06-09 09:15:40
Oracle中时间日期转化函数to_date和to_char的具体使用
2023-07-15 20:20:20
IE8 的 JSON 解析 Bug
2009-05-22 12:36:00
ASP函数指针试探-GetRef
2009-10-12 12:39:00
Selenium执行完毕未关闭chromedriver/geckodriver进程的解决办法(java版+python版)
2023-03-28 12:39:51
如何使用SQLServer数据库查询累计值
2009-03-16 14:43:00
详解Python自建logging模块
2023-12-21 02:28:50
网页代码更清晰高效的一些经验
2008-05-19 12:23:00
手把手教你安装Windows版本的Tensorflow
2021-07-12 07:03:15
![](https://img.aspxhome.com/file/2023/6/84066_0s.png)
Chrome和firefox使用比较测评
2010-03-26 12:13:00
![](https://img.aspxhome.com/file/UploadPic/20103/26/2010326124036780s.gif)
35个Python编程小技巧
2023-08-23 21:23:50
闲聊html和body标签
2009-02-21 10:50:00
DB2和 Oracle的并发控制(锁)的比较
2009-02-28 10:29:00
Python+OpenCV实现相机标定的方法详解
2021-03-10 20:02:30
![](https://img.aspxhome.com/file/2023/8/86598_0s.png)
Python使用内置函数setattr设置对象的属性值
2021-10-09 20:32:44
深入浅析python的第三方库pandas
2021-06-05 03:13:03
![](https://img.aspxhome.com/file/2023/0/69010_0s.png)
MySQL数据库中对前端和后台进行系统优化
2009-01-04 13:39:00
Python+OpenCV实现六种常用图像特效
2023-08-02 10:01:43
![](https://img.aspxhome.com/file/2023/5/61965_0s.jpg)
解析ROC曲线绘制(python+sklearn+多分类)
2021-04-06 12:16:16
![](https://img.aspxhome.com/file/2023/4/78564_0s.png)
ASP编程如何执行存储过程?
2010-03-17 20:56:00