opencv进行标定并输出矫正后照片的代码
时间: 2024-02-07 08:03:16 浏览: 159
可以提供以下 OpenCV 标定代码:
```python
import numpy as np
import cv2
# 准备棋盘格的仿真数据
objp = np.zeros((6*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7,0:6].T.reshape(-1,2)
# 存储棋盘格角点的数组
objpoints = [] # 3d points in real world space
imgpoints = [] # 2d points in image plane.
# 枚举所有图片进行棋盘格角点的检测
images = glob.glob('*.jpg')
for fname in images:
img = cv2.imread(fname)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# 检测棋盘格角点
ret, corners = cv2.findChessboardCorners(gray, (7,6),None)
# 如果找到,则保存角点坐标
if ret == True:
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
imgpoints.append(corners)
# 可视化检测结果
img = cv2.drawChessboardCorners(img, (7,6), corners2,ret)
cv2.imshow('img',img)
cv2.waitKey(500)
cv2.destroyAllWindows()
# 标定相机
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
# 对棋盘格进行验证
img = cv2.imread('left12.jpg')
h, w = img.shape[:2]
newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))
# 矫正后的图片
dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
# 裁剪矫正后的图片
x,y,w,h = roi
dst = dst[y:y+h, x:x+w]
cv2.imwrite('calibresult.png',dst)
```
这个代码可以根据特定的棋盘格图案进行摄像头标定,并输出矫正后的图片。但是请注意,该代码并不适用所有情况,具体使用时需要根据实际情况进行修改和调整。
阅读全文