如何使用opencv.omnidir模块使鱼眼图像变形 [英] How to use opencv.omnidir module for dewarping fisheye images

查看:217
本文介绍了如何使用opencv.omnidir模块使鱼眼图像变形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用全向模块使鱼眼图像变形在 Python 中.我正在尝试在Python中改编 C ++教程问题.这是我的代码:

I am trying to use omnidirectional module for dewarping fisheye images in Python. I am trying to adapt this C++ tutorial in Python but running into issues. Here is my code:

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_arrimg_points_arr分别为num_points x 1 x (nx * ny) x 3num_points x 1 x (nx * ny) x 2.如果我使用

Here obj_points_arr and img_points_arr were num_points x 1 x (nx * ny) x 3 and num_points x 1 x (nx * ny) x 2 respectively. If I reshape the obj_points_arr and img_points_arr to num_points x (nx * ny) x 3 and num_points x (nx * ny) x 2 using

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代码以使鱼眼图像变形吗?

What should be the shapes of these arrays ? Can someone share their python code for dewarping fisheye images ?

推荐答案

首先,您无需弄乱图像,最好将其写入并打印.

First you didn't get the images corners, better to write to else and print out.

else: print "not found: "+fname

else: print "not found: "+fname

您可以使用pyfisheye模块完成您的任务. https://bitbucket.org/amitibo/pyfisheye

You can use the module of pyfisheye to complete your task. https://bitbucket.org/amitibo/pyfisheye

这篇关于如何使用opencv.omnidir模块使鱼眼图像变形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆