双目测距代码 python opencv 利用双目摄像头拍照,测距

环境:ubuntu14.04
软件:opencv3.3 +pycharm
  好久没有写博客了,手都感觉好生疏,最近一直在研究工控机,水下机器人等,好不容易闲下来,没办法呀,继续前行吧,前面一直说双目测距,但没有高速大家如何获得这个双目标注得图像,我在这把这代码贴一下吧,获得这些双目拍摄的图片就可以利用matlab和opencv进行标注了。

小编查了网上好多代码,在利用双目摄像头i实现拍照得时候总是出线黑框,无法获取图片,经过修改之后,我将代码改成了自动拍照或者按键S进行拍照
不多说,看看效果吧

双目测距代码 python opencv 利用双目摄像头拍照,测距_第1张图片

双目测距代码 python opencv 利用双目摄像头拍照,测距_第2张图片

#!/usr/bin/python
# -*- coding: utf-8 -*-
import cv2
import time

AUTO = True  # 自动拍照,或手动按s键拍照
INTERVAL = 2 # 自动拍照间隔

cv2.namedWindow("left")
cv2.namedWindow("right")
cv2.moveWindow("left", 0, 0)
cv2.moveWindow("right", 400, 0)
left_camera = cv2.VideoCapture(0)
#cv2.waitKey(50)
#left_camera.set(cv2.CV_CAP_PROP_FRAME_WIDTH,320)
#left_camera.set(cv2.CV_CAP_PROP_FRAME_HEIGHT,240)
right_camera = cv2.VideoCapture(1)
#right_camera.set(cv2.CV_CAP_PROP_FRAME_WIDTH,320)
#right_camera.set(cv2.CV_CAP_PROP_FRAME_HEIGHT,240)

counter = 0
utc = time.time()
pattern = (12, 8) # 棋盘格尺寸
folder = "./snapshot/" # 拍照文件目录

def shot(pos, frame):
    global counter
    path = folder + pos + "_" + str(counter) + ".jpg"

    cv2.imwrite(path, frame)
    print("snapshot saved into: " + path)

while True:
    ret, left_frame = left_camera.read()
    ret, right_frame = right_camera.read()

    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

left_camera.release()
right_camera.release()
cv2.destroyWindow("left")
cv2.destroyWindow("right")

好啦,到此结束,大家可结合自己自身情况修改或者标定吧

你可能感兴趣的:(python-opencv,c++与opencv做图像处理,图像处理,机器学习)