打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
opencv(九):相机标定
import cv2import numpy as npdef draw(img, corners, imgpts): corner = tuple(corners[0].ravel()) img = cv2.line(img, corner, tuple(imgpts[0].ravel()), (255, 0, 0), 5) img = cv2.line(img, corner, tuple(imgpts[1].ravel()), (0, 255, 0), 5) img = cv2.line(img, corner, tuple(imgpts[2].ravel()), (0, 0, 255), 5) return img# 标定图像def calibration_photo(photo_path): # 设置要标定的角点个数 x_nums = 11 # x方向上的角点个数 y_nums = 8 # 设置(生成)标定图在世界坐标中的坐标 world_point = np.zeros((x_nums * y_nums, 3), np.float32) # 生成x_nums*y_nums个坐标,每个坐标包含x,y,z三个元素 world_point[:, :2] = 15 * np.mgrid[:x_nums, :y_nums].T.reshape(-1, 2) # mgrid[]生成包含两个二维矩阵的矩阵,每个矩阵都有x_nums列,y_nums行 print('world point:',world_point) # .T矩阵的转置 # reshape()重新规划矩阵,但不改变矩阵元素 # 设置世界坐标的坐标 axis = 15* np.float32([[3, 0, 0], [0, 3, 0], [0, 0, -3]]).reshape(-1, 3) # 设置角点查找限制 criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) image = cv2.imread(photo_path) gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # 查找角点 ok, corners = cv2.findChessboardCorners(gray, (x_nums, y_nums), ) # print(ok) if ok: # 获取更精确的角点位置 exact_corners = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria) # 获取外参 _, rvec, tvec, inliers = cv2.solvePnPRansac(world_point, exact_corners, mtx, dist) #获得的旋转矩阵是向量,是3×1的矩阵,想要还原回3×3的矩阵,需要罗德里格斯变换Rodrigues, rotation_m, _ = cv2.Rodrigues(rvec)#罗德里格斯变换 # print(rotation_m) # print('旋转矩阵是:\n', rvec) # print('平移矩阵是:\n', tvec) rotation_t = np.hstack([rotation_m,tvec]) rotation_t_Homogeneous_matrix = np.vstack([rotation_t,np.array([[0, 0, 0, 1]])]) print(rotation_t_Homogeneous_matrix) imgpts, jac = cv2.projectPoints(axis, rvec, tvec, mtx, dist) # 可视化角点 img = draw(image, corners, imgpts) cv2.imshow('img', img) return rotation_t_Homogeneous_matrix # 返回旋转矩阵和平移矩阵组成的其次矩阵if __name__ == '__main__': # 读取相机内参 with np.load('D:\\ML\\Project_python\\my_code\\video_and_img\\checkerboard.npz') as X: mtx, dist = [X[i] for i in ('mtx', 'dist')] print(mtx, '\n', dist) photo_path = "D:\\ML\\Project_python\\my_code\\video_and_img\\checkerboard\\WIN_20191123_11_54_24_Pro.jpg" # 标定图像保存路径 calibration_photo(photo_path) cv2.waitKey() cv2.destroyAllWindows()
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
基于Opencv的图像单应性转换实战
立体图像的三维重建-对极几何
OpenCV-Python 图像的几何变换 | 十四
Python
机器学习实战:用 SVD 压缩图像(已上线)
python+opencv图像处理(十)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服