繁体   English   中英

使用openCV进行摄像机校准-cv2.getOptimalNewCameraMatrix的投资回报率为零

[英]Camera Calibration with openCV - cv2.getOptimalNewCameraMatrix results in zero roi

这是我在stackoverflow上的第一篇文章。 如果他们不知所措,请为我的英语和我的编程知识感到抱歉。

好吧,我正在尝试在Windows 8.1操作系统中使用opencv 2.4.9进行相机校准(ubuntu操作系统无法解决问题。)

问题:我正在使用下面的代码校准相机,但是,如果我的示例图像(带有检查板图案)的数量大于2,则newcameramtx的投资回报率为roi = cv2.getOptimalNewCameraMatrix(mtx,dist ,((w,h),1,(w,h))得出[0,0,0,0]。 与该结果相关的样本数量如何? (之前,在对该代码进行一些更改之前,最大样本数为12)。

所谓最大样本数,是指从照相机获取的具有棋盘图案的图像,如果数量超过最大数目,则roi效果不佳。

拐角检测效果很好。 您可以在这里找到我的示例图像。

# -*- coding: utf-8 -*-
"""
Created on Fri May 16 15:23:00 2014

@author: kakarot
"""

import numpy as np
import cv2
#import os
#import time
from matplotlib import pyplot as plt
LeftorRight = 'L'

numer = 12
chx = 6
chy = 9
chd = 25
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, numer, 0.001)

# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((chy*chx,3), np.float32)
objp[:,:2] = np.mgrid[0:chy,0:chx].T.reshape(-1,2)

# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space, (x25mm)
imgpoints = [] # 2d points in image plane.

enum = 1

while(enum<=numer):
    img=cv2.imread('1280x720p/BestAsPerMatlab/calib_'+str(LeftorRight)+str(enum)+'.jpg')

    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    # Find the chess board corners
    ret, corners = cv2.findChessboardCorners(gray, (chy,chx),None)
    #cv2.imshow('Calibration',img)

    # If found, add object points, image points (after refining them)
    if  ret == True and enum <= numer:

        objpoints.append(objp*chd)


        cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria)
        imgpoints.append(corners)


        # Draw and display the corners
        cv2.drawChessboardCorners(img, (chy,chx),corners,ret)
        cv2.imshow('Calibration',img)
        cv2.imwrite('1280x720p/Chessboard/calibrated_L{0}.jpg'.format(enum),img)
        print enum

        #time.sleep(2)
        if enum == numer:
            ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)

            img = cv2.imread('1280x720p/BestAsPerMatlab/calib_'+str(LeftorRight)+'7.jpg')
            gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
            h,  w = img.shape[:2]           #a (1 to see the whole picture)
            newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))
            if (np.size(roi) == 4 and np.mean(roi) != 0):
                # undistort
                mapx,mapy = cv2.initUndistortRectifyMap(mtx,dist,None,newcameramtx,(w,h),5)
                dst = cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)

                # crop the image
                x,y,w,h = roi
                dst = dst[y:y+h, x:x+w]
                dst = cv2.cvtColor(dst,cv2.COLOR_RGB2BGR)
                plt.imshow(dst)
                #cv2.imwrite('result.jpg',dst)
                #np.savetxt('mtxL.txt',mtx)
                #np.savetxt('distL.txt',dist)
            else:
                np.disp('Something Went Wrong')
        enum += 1
'''
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break
'''
cv2.destroyAllWindows()

编辑:我正在使用两个廉价的USB相机。 我发现其中一台相机的样本集还可以,我可以毫无问题地使用19个以上的样本。 但是,当使用另一台摄像机的校准样本时,样本图像的最大数量为2。(如果我制作另一组样本,则数量将有所不同)。 总之,似乎产生的校准矩阵正在发生某些事情。 但这很奇怪。

最终,我使用了鱼眼镜头的摄像头,相信在每次捕获的末尾切出足够多的像素后,我会模拟普通的摄像头……也许这就是给我造成麻烦的原因!

您应该将dist更改为

dist = np.array([-0.13615181, 0.53005398, 0, 0, 0]) # no translation

然后打电话给

newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))

它为我工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM