python opencv设置摄像头分辨率以及各个参数的方法_python


当前第2页 返回上一页

你想要设置成的新值。例如,我可以使用 cap.get(3) 和 cap.get(4) 来查看每一帧的宽和高。默认情况下得到的值是 640X480。但是我可以使用 ret=cap.set(3,320)和 ret=cap.set(4,240) 来把宽和高改成 320X240。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

#!/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()

相关推荐:

python opencv 图像尺寸变换方法

Python-OpenCV基本操作方法详解_python

python使用opencv读取图片的实例详解



以上就是python opencv设置摄像头分辨率以及各个参数的方法_python的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

Python中顺序表算法复杂度的相关知识介绍

Python运算符优先级顺序是什么?

Python 操作 excel 系列之:数据清洗

Python字典怎么排序

Python如何无视大小写

Python常用类型转换实现

Python中flask的session设置的方法介绍

Python与c互相调用的详细介绍

Python内置的pickle库的对象序列化与反序列化

Python中range函数怎么用

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...