import cv2 import numpy as np chessboard_size = (9,6) # 棋盘格行列数 objp = np.zeros((np.prod(chessboard_size), 3), np.float32) # 内部点的坐标 objp[:, :2] = np.mgrid[0:chessboard_size[0], 0:chessboard_size[1]].T.reshape(-1, 2) img = cv2.imread('C:/yingxiang/biaoding.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, corners = cv2.findChessboardCorners(gray, chessboard_size, None) if ret == True: cv2.drawChessboardCorners(img, chessboard_size, corners, ret) ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera([objp], [corners], gray.shape[::-1], None, None) # 打印相机内部参数和畸变系数 print("相机内部参数:") np.set_printoptions(precision=4, suppress=True) print(mtx) print("畸变系数:") np.set_printoptions(precision=4, suppress=True) print(dist) # 打印外部参数 print("旋转向量:") np.set_printoptions(precision=4, suppress=True) print(rvecs) print("平移向量:") np.set_printoptions(precision=4, suppress=True) print(tvecs)这段代码如何将旋转向量和平移向量改为旋转矩阵和平移矩阵
时间: 2024-02-27 12:53:20 浏览: 147
python 3.74 运行import numpy as np 报错lib\site-packages\numpy\__init__.py
在 OpenCV 中,可以使用 `cv2.Rodrigues()` 函数将旋转向量转换为旋转矩阵,使用 `np.array()` 函数将平移向量转换为平移矩阵。修改代码如下:
```
import cv2
import numpy as np
chessboard_size = (9,6) # 棋盘格行列数
objp = np.zeros((np.prod(chessboard_size), 3), np.float32) # 内部点的坐标
objp[:, :2] = np.mgrid[0:chessboard_size[0], 0:chessboard_size[1]].T.reshape(-1, 2)
img = cv2.imread('C:/yingxiang/biaoding.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, chessboard_size, None)
if ret == True:
cv2.drawChessboardCorners(img, chessboard_size, corners, ret)
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera([objp], [corners], gray.shape[::-1], None, None)
# 打印相机内部参数和畸变系数
print("相机内部参数:")
np.set_printoptions(precision=4, suppress=True)
print(mtx)
print("畸变系数:")
np.set_printoptions(precision=4, suppress=True)
print(dist)
# 将旋转向量转换为旋转矩阵
R, _ = cv2.Rodrigues(rvecs[0])
# 打印旋转矩阵和平移矩阵
print("旋转矩阵:")
np.set_printoptions(precision=4, suppress=True)
print(R)
print("平移矩阵:")
np.set_printoptions(precision=4, suppress=True)
print(np.array(tvecs[0]).reshape(3,1))
```
这样,就将旋转向量和平移向量转换为了旋转矩阵和平移矩阵。
阅读全文