# -*- coding: utf-8 -*- ''' @Time : 2022/10/05 19:05 @Author : jianbin @Email : [email protected] @FileName: get_image.py @Software: PyCharm ''' import cv2 import numpy as np import os class StereoCamera(object): """采集双目标定图片,按键盘【c】或【s】保存图片""" #def __init__(self, chess_width, chess_height, detect=False): def __init__(self, chess_width, chess_height, detect=True): """ :param chess_width: chessboard width size,即棋盘格宽方向黑白格子相交点个数, :param chess_height: chessboard height size,即棋盘格长方向黑白格子相交点个数 :param detect: 是否实时检测棋盘格,方便采集数据 """ self.chess_width = chess_width self.chess_height = chess_height self.detect = detect #设置寻找亚像素角点的参数,采用的停止准则是最大循环次数30和最大误差容限0.001 self.criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)#阈值 def detect_chessboard(self, image): """检测棋盘格""" gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ret, corners = cv2.findChessboardCorners(gray, (self.chess_width, self.chess_height), None) if ret: # 角点精检测 corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), self.criteria) # Draw and display the corners image = cv2.drawChessboardCorners(image, (self.chess_width, self.chess_height), corners2, ret) return image # 类的实例化 stereo = StereoCamera(8, 11, detect=True) cap = cv2.VideoCapture(0) # 设置摄像头分辨率 cap.set(cv2.CAP_PROP_FRAME_WIDTH, 2560) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) i = 0 while cap.isOpened(): ret, frame = cap.read() left_img = frame[:, 0:1280, :] right_img = frame[:, 1280:2560, :] if ret: if stereo.detect: l = stereo.detect_chessboard(left_img.copy()) r = stereo.detect_chessboard( right_img.copy()) else: l = left_img.copy() r = right_img.copy() # 显示两幅图片合成的图片 # cv2.imshow('img', frame) # 显示左摄像头视图 cv2.imshow('left', l) # 显示右摄像头视图 cv2.imshow('right', r) key = cv2.waitKey(delay=2) if key == ord("q") or key == 27: break # 按”c“或”s“保存图像 elif key == ord('c') or key == ord('s'): print("save image:{:0=3d}".format(i)) cv2.imwrite(os.path.join("data/camera", "left_{:0=3d}.png".format(i)), left_img) cv2.imwrite(os.path.join("data/camera", "right_{:0=3d}.png".format(i)), right_img) i += 1 cap.release() cv2.destroyAllWindows()
标签:detect,img,双目,self,cv2,标定,height,chess,摄像头 From: https://www.cnblogs.com/xiejb2430/p/16757659.html