基于python opencv实现广角相机标定和图像畸变矫正
目的:实现相机标定,得到相机的内参以及畸变旋转参数等尝试矫正由相机产生的图像畸变代码:import cv2 as cvimport numpy as npimport globimport os#循环中断criteria=(cv.TERM_CRITERIA_EPS+cv.TERM_CRITERIA_MAX_ITER,30,0.001)#标定板交叉点的个数row=6column=4objp=np.z
·
目的:
- 实现相机标定,得到相机的内参以及畸变旋转参数等
- 尝试矫正由相机产生的图像畸变
代码:
import cv2 as cv
import numpy as np
import glob
import os
#循环中断
criteria=(cv.TERM_CRITERIA_EPS+cv.TERM_CRITERIA_MAX_ITER,30,0.001)
#标定板交叉点的个数
row=6
column=4
objp=np.zeros((row*column,3),np.float32)
objp[:,:2]=np.mgrid[0:row,0:column].T.reshape(-1,2)
objpoints=[] #实际空间3D点
imgpoints=[] #图像中2D点
#批量读取图片
images=glob.glob('D:\\python\\photos\\chess\\*.jpg')
for fname in images:
img=cv.imread(fname)
gray=cv.cvtColor(img,cv.COLOR_BGR2GRAY)
#找标定板角点
ret,corners=cv.findChessboardCorners(gray,(row,column),None)
if ret==True:
objpoints.append(objp)
corners2=cv.cornerSubPix(gray, corners, (11,11), (-1,-1), criteria)
imgpoints.append(corners2)
#标定相机
ret,Matrix,dist,rvecs,tvecs=cv.calibrateCamera(objpoints,imgpoints,gray.shape[::-1],None,None)
datadir="D:\\python\\photos\\chess\\"
path=os.path.join(datadir)
img_list=os.listdir(path)
for i in img_list:
img=cv.imread(os.path.join(path,i))
h,w=img.shape[:2]
newMatrix, roi = cv.getOptimalNewCameraMatrix(Matrix, dist, (w,h), 1, (w,h)) #矫正图像
dst = cv.undistort(img, Matrix, dist, None, newMatrix)
cv.imwrite('D:\\python\\practice\\photos\\calibrated\\'+i,dst)
#计算重投影误差
tot_error = 0
for i in range(len(objpoints)):
imgpoints2, _ = cv.projectPoints(objpoints[i], rvecs[i], tvecs[i], Matrix, dist)
error = cv.norm(imgpoints[i],imgpoints2, cv.NORM_L2)/len(imgpoints2)
tot_error += error
#输出参数
print('ret:\n',ret)
print('mtx:\n',Matrix)
print('dist:\n',dist)
print('rvecs:\n',rvecs)
print('tvecs:\n',tvecs)
print ("total error: ", tot_error/len(objpoints))
欢迎关注公众号:算法工程师的学习日志
更多推荐
已为社区贡献3条内容
所有评论(0)