OpenCV 打开双目摄像头(python版)
前言
本文主要介绍在OpenCV用使用双目摄像头,包括:打开单目摄像头、设置摄像头参数、拍照、录制视频。
环境:编程语言:Python3 主要依赖库:OpenCV3.x 或 OpenCV4.x
双目摄像头
双目同步摄像头,两个镜头共用一个设备ID,左右摄像机同一频率。这款摄像头分辨率支持2560*960或以上。
思路流程
1、由于两个镜头共用一个设备ID,打开摄像头时使用cv2.VideoCapture()函数,只需打开一次。区别有的双目摄像头是左右镜头各用一个设备ID,需要打开两次cv2.VideoCapture(0),cv2.VideoCapture(1)。
2、双目摄像头的总分辨率是由左右镜头组成的,比如:左右摄像机总分辨率1280x480;分割为左相机640x480、右相机640x480
为了方便理解画了张草图;图中的“原点”是图像像素坐标系的原点。
3、分割后,左相机的分辨率:高度 0:480、宽度 0:640
右相机的分辨率:高度 0:480、宽度 640:1280
4、转换为代码后
-
# 读取摄像头数据
-
ret, frame = camera.read()
-
#裁剪坐标为[y0:y1, x0:x1] HEIGHT * WIDTH
-
left_frame = frame[0:480, 0:640]
-
right_frame = frame[0:480, 640:1280]
-
-
cv2.imshow("left", left_frame)
-
cv2.imshow("right", right_frame)
1)分辨率1280x480
源代码:
-
# -*- coding: utf-8 -*-
-
import cv2
-
import time
-
-
-
AUTO = False # 自动拍照,或手动按s键拍照
-
INTERVAL = 2 # 自动拍照间隔
-
-
cv2.namedWindow("left")
-
cv2.namedWindow("right")
-
camera = cv2.VideoCapture(0)
-
-
# 设置分辨率 左右摄像机同一频率,同一设备ID;左右摄像机总分辨率1280x480;分割为两个640x480、640x480
-
camera.set(cv2.CAP_PROP_FRAME_WIDTH,1280)
-
camera.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
-
-
counter = 0
-
utc = time.time()
-
folder = "./SaveImage/" # 拍照文件目录
-
-
def shot(pos, frame):
-
global counter
-
path = folder + pos + "_" + str(counter) + ".jpg"
-
-
cv2.imwrite(path, frame)
-
print("snapshot saved into: " + path)
-
-
while True:
-
ret, frame = camera.read()
-
# 裁剪坐标为[y0:y1, x0:x1] HEIGHT*WIDTH
-
left_frame = frame[0:480, 0:640]
-
right_frame = frame[0:480, 640:1280]
-
-
cv2.imshow("left", left_frame)
-
cv2.imshow("right", right_frame)
-
-
now = time.time()
-
if AUTO and now - utc >= INTERVAL:
-
shot("left", left_frame)
-
shot("right", right_frame)
-
counter += 1
-
utc = now
-
-
key = cv2.waitKey(1)
-
if key == ord("q"):
-
break
-
elif key == ord("s"):
-
shot("left", left_frame)
-
shot("right", right_frame)
-
counter += 1
-
camera.release()
-
cv2.destroyWindow("left")
-
cv2.destroyWindow("right")
补充理解
OpenCV有VideoCapture()函数,能用来定义“摄像头”对象,0表示第一个摄像头(一般是电脑内置的摄像头);如果有两个摄像头,第二个摄像头则对应VideoCapture(1)。
在while循环中使用“摄像头对象”的read()函数一帧一帧地读取摄像头画面数据。
imshow函数是显示摄像头的某帧画面;cv2.waitKey(1)是等待1ms,如果期间检测到了键盘输入q,则退出while循环。
效果
2)分辨率2560x720
源代码:
-
# -*- coding: utf-8 -*-
-
import cv2
-
import time
-
-
-
AUTO = False # 自动拍照,或手动按s键拍照
-
INTERVAL = 2 # 自动拍照间隔
-
-
cv2.namedWindow("left")
-
cv2.namedWindow("right")
-
camera = cv2.VideoCapture(0)
-
-
# 设置分辨率左右摄像机同一频率,同一设备ID;左右摄像机总分辨率2560x720;分割为两个1280x720
-
camera.set(cv2.CAP_PROP_FRAME_WIDTH,2560)
-
camera.set(cv2.CAP_PROP_FRAME_HEIGHT,720)
-
-
counter = 0
-
utc = time.time()
-
folder = "./SaveImage/" # 拍照文件目录
-
-
def shot(pos, frame):
-
global counter
-
path = folder + pos + "_" + str(counter) + ".jpg"
-
-
cv2.imwrite(path, frame)
-
print("snapshot saved into: " + path)
-
-
while True:
-
ret, frame = camera.read()
-
print("ret:",ret)
-
# 裁剪坐标为[y0:y1, x0:x1] HEIGHT * WIDTH
-
left_frame = frame[0:720, 0:1280]
-
right_frame = frame[0:720, 1280:2560]
-
-
cv2.imshow("left", left_frame)
-
cv2.imshow("right", right_frame)
-
-
now = time.time()
-
if AUTO and now - utc >= INTERVAL:
-
shot("left", left_frame)
-
shot("right", right_frame)
-
counter += 1
-
utc = now
-
-
key = cv2.waitKey(1)
-
if key == ord("q"):
-
break
-
elif key == ord("s"):
-
shot("left", left_frame)
-
shot("right", right_frame)
-
counter += 1
-
camera.release()
-
cv2.destroyWindow("left")
-
cv2.destroyWindow("right")
其他版本:OpenCV 双目摄像头拍照 C++
文章来源: guo-pu.blog.csdn.net,作者:一颗小树x,版权归原作者所有,如需转载,请联系作者。
原文链接:guo-pu.blog.csdn.net/article/details/113090604
- 点赞
- 收藏
- 关注作者
评论(0)