前言:
如今小伙伴们对“出售鱼眼矫正算法”大体比较关注,各位老铁们都想要了解一些“出售鱼眼矫正算法”的相关文章。那么小编也在网上搜集了一些有关“出售鱼眼矫正算法””的相关文章,希望我们能喜欢,各位老铁们快快来了解一下吧!01.简介
当我们使用的鱼眼镜头视角大于160°时,OpenCV中用于校准镜头“经典”方法的效果可能就不是和理想了。即使我们仔细遵循OpenCV文档中的步骤,也可能会得到下面这个奇奇怪怪的照片:
如果小伙伴也遇到了类似情况,那么这篇文章可能会对大家有一定的帮助。
从3.0版开始,OpenCV包含了cv2.fisheye可以很好地处理鱼眼镜头校准的软件包。但是,该模块没有针对读者的相关的教程。
02.相机参数获取
校准镜头其实只需要下面2个步骤。
利用OpenCV计算镜头的2个固有参数。OpenCV称它们为K和D,我们只需要知道它们是numpy数组外即可。通过K和D对图像进行去畸变矫正。
计算K和D
下载棋盘格图案并将其打印在纸上(字母或A4尺寸)。大家要尽量将这张纸粘在坚硬且平坦的物体表面,例如一块硬纸板上。因为这里的关键是直线必须是直线。将图案放在相机前面拍摄一些图像,图案要取在不同的位置和角度。这里的关键是图案需要以不同的方式出现失真(以便OpenCV尽可能多地了解镜头相关参数)。我们先将这些图片保存在JPG文件夹中。现在我们只需要将此Python脚本片段复制到calibrate.py先前保存这些图像的文件夹中的文件中,就可以对其进行命名。
import cv2assert cv2.__version__[0] == '3', 'The fisheye module requires opencv version >= 3.0.0'import numpy as npimport osimport globCHECKERBOARD = (6,9)subpix_criteria = (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1)calibration_flags = cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC+cv2.fisheye.CALIB_CHECK_COND+cv2.fisheye.CALIB_FIX_SKEWobjp = np.zeros((1, CHECKERBOARD[0]*CHECKERBOARD[1], 3), np.float32)objp[0,:,:2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)_img_shape = Noneobjpoints = [] # 3d point in real world spaceimgpoints = [] # 2d points in image plane.images = glob.glob('*.jpg')for fname in images: img = cv2.imread(fname) if _img_shape == None: _img_shape = img.shape[:2] else: assert _img_shape == img.shape[:2], "All images must share the same size." gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # Find the chess board corners ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, cv2.CALIB_CB_ADAPTIVE_THRESH+cv2.CALIB_CB_FAST_CHECK+cv2.CALIB_CB_NORMALIZE_IMAGE) # If found, add object points, image points (after refining them) if ret == True: objpoints.append(objp) cv2.cornerSubPix(gray,corners,(3,3),(-1,-1),subpix_criteria) imgpoints.append(corners)N_OK = len(objpoints)K = np.zeros((3, 3))D = np.zeros((4, 1))rvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_OK)]tvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_OK)]rms, _, _, _, _ = \ cv2.fisheye.calibrate( objpoints, imgpoints, gray.shape[::-1], K, D, rvecs, tvecs, calibration_flags, (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-6) )print("Found " + str(N_OK) + " valid images for calibration")print("DIM=" + str(_img_shape[::-1]))print("K=np.array(" + str(K.tolist()) + ")")print("D=np.array(" + str(D.tolist()) + ")")
运行python calibrate.py。如果一切顺利,脚本将输出如下内容:
import cv2assert cv2.__version__[0] == '3', 'The fisheye module requires opencv version >= 3.0.0'import numpy as npimport osimport globCHECKERBOARD = (6,9)subpix_criteria = (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1)calibration_flags = cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC+cv2.fisheye.CALIB_CHECK_COND+cv2.fisheye.CALIB_FIX_SKEWobjp = np.zeros((1, CHECKERBOARD[0]*CHECKERBOARD[1], 3), np.float32)objp[0,:,:2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)_img_shape = Noneobjpoints = [] # 3d point in real world spaceimgpoints = [] # 2d points in image plane.images = glob.glob('*.jpg')for fname in images: img = cv2.imread(fname) if _img_shape == None: _img_shape = img.shape[:2] else: assert _img_shape == img.shape[:2], "All images must share the same size." gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # Find the chess board corners ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, cv2.CALIB_CB_ADAPTIVE_THRESH+cv2.CALIB_CB_FAST_CHECK+cv2.CALIB_CB_NORMALIZE_IMAGE) # If found, add object points, image points (after refining them) if ret == True: objpoints.append(objp) cv2.cornerSubPix(gray,corners,(3,3),(-1,-1),subpix_criteria) imgpoints.append(corners)N_OK = len(objpoints)K = np.zeros((3, 3))D = np.zeros((4, 1))rvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_OK)]tvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_OK)]rms, _, _, _, _ = \ cv2.fisheye.calibrate( objpoints, imgpoints, gray.shape[::-1], K, D, rvecs, tvecs, calibration_flags, (cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-6) )print("Found " + str(N_OK) + " valid images for calibration")print("DIM=" + str(_img_shape[::-1]))print("K=np.array(" + str(K.tolist()) + ")")print("D=np.array(" + str(D.tolist()) + ")")
03.图像畸变矫正
获得K和D后,我们可以对以下情况获得的图像进行失真矫正:我们需要取消失真的图像与校准期间捕获的图像具有相同的尺寸。也可以将边缘周围的某些区域裁剪掉,来保证使未失真图像的整洁。通过undistort.py使用以下python代码创建文件:
# You should replace these 3 lines with the output in calibration stepDIM=XXXK=np.array(YYY)D=np.array(ZZZ)def undistort(img_path): img = cv2.imread(img_path) h,w = img.shape[:2] map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, D, np.eye(3), K, DIM, cv2.CV_16SC2) undistorted_img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT) cv2.imshow("undistorted", undistorted_img) cv2.waitKey(0) cv2.destroyAllWindows()if __name__ == '__main__': for p in sys.argv[1:]: undistort(p)
现在运行python undistort.py file_to_undistort.jpg。
矫正前
矫正后
如果大家仔细观察,可能会注意到一个问题:原始图像中的大部分会在此过程中被裁剪掉。例如,图像左侧的橙色RC汽车只有一半的车轮保持在未变形的图像中。实际上,原始图像中约有30%的像素丢失了。小伙伴们可以思考思考如果我们想找回丢失的像素该这么办呢?
欢迎大家点赞关注哦,今后会为大家每天更新计算机视觉的小知识!
标签: #出售鱼眼矫正算法 #鱼眼图像矫正 #鱼眼图像校正 #鱼眼图像校正软件下载 #鱼眼图像校正软件下载免费