如何使用 opencv.omnidir 模块去扭曲鱼眼图像
Posted
技术标签:
【中文标题】如何使用 opencv.omnidir 模块去扭曲鱼眼图像【英文标题】:How to use opencv.omnidir module for dewarping fisheye images 【发布时间】:2017-06-13 19:42:44 【问题描述】:我正在尝试使用 omnidirectional module 在 Python 中对鱼眼图像进行去扭曲。我正在尝试在 Python 中调整这个C++ tutorial,但遇到了问题。这是我的代码:
import numpy as np
import cv2
import glob
nx = 9
ny = 6
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
chessboard_model = np.zeros((1, nx*ny, 3), np.float32)
chessboard_model[0, :, :2] = np.mgrid[0:nx, 0:ny].T.reshape(-1, 2)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((nx*ny, 3), np.float32)
objp[:,:2] = np.mgrid[0:nx, 0:ny].T.reshape(-1,2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
filenames = glob.glob('/home/work/Downloads/fisheye_calibration/*.jpg')
images = []
i = 0
for fname in filenames:
i += 1
print("Processing image: ".format(i))
img = cv2.imread(fname)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
images.append(img)
# Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (nx,ny),None)
# If found, add object points, image points (after refining them)
if ret == True:
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
#imgpoints.append(corners2)
imgpoints.append(corners2.reshape(1, -1, 2))
# Draw and display the corners
img = cv2.drawChessboardCorners(img, (nx,ny), corners2,ret)
cv2.imshow('img',img)
cv2.waitKey(500)
num_points = len(imgpoints)
K = np.zeros((3, 3))
xi = np.array([])
idx = np.array([])
D = np.zeros((4, 1))
h, w = images[0].shape[:2]
calibration_flags = cv2.omnidir.CALIB_USE_GUESS | cv2.omnidir.CALIB_FIX_SKEW
obj_points_arr = np.array([chessboard_model]*num_points)
img_points_arr = np.array(imgpoints).squeeze(axis=1)
rms = cv2.omnidir.calibrate(obj_points_arr, img_points_arr, (w,h), K, xi, D, calibration_flags, criteria)
我收到以下错误:
cv2.error:/home/work/opencv_contrib/modules/ccalib/src/omnidir.cpp:1065: error: (-215) !patternPoints.empty() && !imagePoints.empty() && patternPoints.total() == imagePoints.total() in function calibrate
这里的obj_points_arr
和img_points_arr
分别是num_points x 1 x (nx * ny) x 3
和num_points x 1 x (nx * ny) x 2
。如果我使用
obj_points_arr
和 img_points_arr
重塑为 num_points x (nx * ny) x 3
和 num_points x (nx * ny) x 2
obj_points_arr = np.array([chessboard_model]*num_points).squeeze(axis=1)
img_points_arr = np.array(imgpoints).squeeze(axis=1)
然后我得到以下错误
cv2.error: /home/work/opencv/modules/core/src/matrix.cpp:469: error: (-215) 0 <= _rowRange.start && _rowRange.start <= _rowRange.end && _rowRange.end <= m.rows in function Mat
这些数组的形状应该是什么?有人可以分享他们用于去扭曲鱼眼图像的 python 代码吗?
【问题讨论】:
【参考方案1】:首先你没有得到图像角落,最好写到else并打印出来。
else:
print "not found: "+fname
您可以使用 pyfisheye 的模块来完成您的任务。 https://bitbucket.org/amitibo/pyfisheye
【讨论】:
我相信这只有在您的 FOV 小于 180 时才有效以上是关于如何使用 opencv.omnidir 模块去扭曲鱼眼图像的主要内容,如果未能解决你的问题,请参考以下文章
我可以/如何通过 websocket 连接到扭曲的应用程序访问客户端 cookie?
如何使用 cv2.fisheye.undistortPoints 将扭曲空间中的点转换为未扭曲空间中的点?