gpt4 book ai didi

python - 使用 OpenCV 进行 RaspiCam 鱼眼校准

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:35 25 4
gpt4 key购买 nike

我正在尝试使用 OpenCV 校准 RaspiCam 鱼眼镜头相机。我正在使用 Python 示例代码,棋盘行号和列号也是正确的,但不知何故我无法获得成功的结果。我已经用下面的很多照片进行了测试,您可以看到它们。我的源代码:https://github.com/jagracar/OpenCV-python-tests/blob/master/OpenCV-tutorials/cameraCalibration/cameraCalibration.py

我的棋盘行和列:行 = 9,列 = 6

images

但没有得到成功的结果

images

编辑:我的解决方案

https://gist.github.com/mesutpiskin/0412c44bae399adf1f48007f22bdd22d

最佳答案

从 opencv 3 开始,引入了 fisheye 模块,它可以很好地管理鱼眼类型镜头的校准。 (至少对于那些不熟悉校准过程背后的数学的人来说是这样。)

# Checkboard dimensions
CHECKERBOARD = (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_SKEW
objp = 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)

objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.

### read images and for each image:
img = cv2.imread(fname)
img_shape = img.shape[:2]

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)
###

# calculate K & D
N_imm = # number of calibration images
K = np.zeros((3, 3))
D = np.zeros((4, 1))
rvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_imm)]
tvecs = [np.zeros((1, 1, 3), dtype=np.float64) for i in range(N_imm)]
retval, K, D, rvecs, tvecs = 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))

现在您有了 KD,您可以取消扭曲:

img = # your image to undistort
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)

这应该有效!

更新 enter image description here

如果你想看到图像的隐藏部分(例如上图中黄色框外的部分),校准后,你需要这样:

img = cv2.imread(img_path)
img_dim = img.shape[:2][::-1]

DIM = # dimension of the images used for calibration

scaled_K = K * img_dim[0] / DIM[0]
scaled_K[2][2] = 1.0
new_K = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(scaled_K, D,
img_dim, np.eye(3), balance=balance)
map1, map2 = cv2.fisheye.initUndistortRectifyMap(scaled_K, D, np.eye(3),
new_K, img_dim, cv2.CV_16SC2)
undist_image = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR,
borderMode=cv2.BORDER_CONSTANT)

现在,通过改变 balance 值,您应该减小或增大最终图像的尺寸(与上图相比,实际上是黄色矩形)。

来自 OpenCV API:balance:在最小焦距和最大焦距之间的范围内设置新的焦距。余额在 [0, 1] 范围内。

关于python - 使用 OpenCV 进行 RaspiCam 鱼眼校准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50857278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com