基于python opencv单目相机标定的示例代码

来自:网络
时间:2022-01-08
阅读:

相机固定不动,通过标定版改动不同方位的位姿进行抓拍

import cv2
camera=cv2.VideoCapture(1)
i = 0
while 1:
    (grabbed, img) = camera.read()
    cv2.imshow('img',img)
    if cv2.waitKey(1) & 0xFF == ord('j'):  # 按j保存一张图片
        i += 1
        u = str(i)
        firename=str('./img'+u+'.jpg')
        cv2.imwrite(firename, img)
        print('写入:',firename)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

将抓拍好的图片存放程序的同一级目录下 运行标定代码如下:

# 相机标定
import cv2
# 修改目录
# 首先读取图像并转为灰度图
img = cv2.imread('c1.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# cv2.imshow("img",img)
# cv2.imshow("gray",gray)
# 使用OpenCV的cv2.findChessboardCorners()函数找出棋盘图中的对角(即图片中黑白相对的点的坐标),
# 同时使用cv2.drawChessboardCorners()将之画出来
# cv2.findChessboardCorners参数patternSize取(9,5)--棋盘图中每行和每列交点的个数
# 其原因在于导入的图片./camera_cal/calibration1.jpg数一下交点的数目,一行有9个,一列有5个
# Adam博客当中取(9,6)原因在于他的图和我的图不一样,认真数一下可以发现他的图确实是一行9个一列6个角点
# 事实证明,可以取任何只要在size小于图片中的交点数即可
# 函数解析参见官网https://docs.opencv.org/3.3.0/dc/dbb/tutorial_py_calibration.html
# It returns the corner points and retval which will be True if pattern is obtained.
# These corners will be placed in an order (from left-to-right, top-to-bottom)
ret, corners = cv2.findChessboardCorners(gray, (9, 5),None)
print(ret)
print(corners)  # 交点坐标
if ret == True:
    img = cv2.drawChessboardCorners(img, (9, 5), corners, ret)
cv2.imshow("final",img)
cv2.waitKey()
cv2.destroyAllWindows()
返回顶部
顶部