您的位置:首页 > 编程语言 > Python开发

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

2022-01-08 04:07 756 查看

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

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()

到此这篇关于基于python opencv单目相机标定的文章就介绍到这了,更多相关python opencv相机标定内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python opencv 相机标定