Home  >  Article  >  Backend Development  >  python opencv method to set camera resolution and various parameters_python

python opencv method to set camera resolution and various parameters_python

不言
不言Original
2018-05-18 14:40:309447browse

Below I will share with you an article on how to set the camera resolution and various parameters in python opencv. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together

1. In order to get the video, you should create a VideoCapture object. His parameter can be the index number of the device, or a video file. The device index number specifies the camera to be used. Most laptops have built-in cameras. So the parameter is 0. You can select another camera by setting it to 1 or something else. After that, you can capture the video frame by frame. But finally, don't forget to stop capturing video. Use the ls /dev/video* command to view the camera device

2, cap.read() returns a Boolean value (True/False). If the frame is read correctly, it is True. So finally you can check whether the video file has reached the end by checking its return value. Sometimes cap may not successfully initialize the camera device. In this case, the above code will report an error. You can use cap.isOpened() to check whether initialization was successful. If the return value is True, there is no problem. Otherwise use the function cap.open(). You can use the function cap.get(propId) to get some parameter information of the video. Here propId can be any integer between 0 and 18. Each number represents an attribute of the video. See the table. Some of the values ​​can be modified using cap.set(propId,value). The value is the new value you want to set. For example, I can use cap.get(3) and cap.get(4) to see the width and height of each frame. By default the resulting value is 640X480. But I can use ret=cap.set(3,320) and ret=cap.set(4,240) to change the width and height to 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(&#39;%s&#39;%motor_value)
x=[]
else:
if i==fps:
face.neck_puber.publish(&#39;1500&#39;)
i=1
else:
i +=1
print (U&#39;未检测到人脸&#39;)
if cv2.waitKey(1) & 0xFF == ord(&#39;q&#39;):
break
except Exception,e:
print e
cap.release()
cv2.destroyAllWindows()

Related recommendations:

python opencv image size conversion method

##Detailed explanation of the basic operation method of Python-OpenCV_python

Detailed example of using python to read images using opencv



##

The above is the detailed content of python opencv method to set camera resolution and various parameters_python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn