【发布时间】:2015-04-07 17:43:27
【问题描述】:
对于我的论文项目,我需要确定相机的 K 失真参数。我决定用python和opencv做的就是。所以你可以看到我是 Python 和 Opencv 的初学者。我使用这个标准代码:
import numpy as np
import cv2
import glob
#termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 70, 0.001)
#prepare object points, like (0,0,0), (1,0,0) ...., (6, 4, 0)
objp = np.zeros((7*5, 3), np.float32)
objp[:,:2] = np.mgrid[0:5,0:7].T.reshape(-1,2)
#Arrays to store objects points and real image points from all the images.
objpoints = [] #3D point in real world space
imgpoints = [] #2D points in image plane
images = glob.glob('C:/tmp/pixelgrootte/*.jpg')
counter = int(x=1)
for fname in images:
img = cv2.imread(fname)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (5,7),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)
# Draw and display the corners
img = cv2.drawChessboardCorners(img, (5,7), corners2,ret)
cv2.imshow('img',img)
cv2.waitKey(500)
counter += 1
else:
print("No corners found on Picture " + str(counter))
counter += 1
cv2.destroyAllWindows()
#Calibration
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None)
print("Camera Matrix: " + str(mtx))
print("Distortion Factors: " + str(dist))
当我运行这个脚本时,这是我收到的错误:
No corners found on Picture 1
Traceback (most recent call last):
File "C:\Users\Lode\Documents\School\2014-2015\Thesis\Camera\Camera Calibration.py", line 37, in <module>
cv2.imshow('img',img)
error: ..\..\..\..\opencv\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0 in function cv::imshow
如您所见,我使用了这个标准代码:https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_calib3d/py_calibration/py_calibration.html#calibration。 唯一的区别是计数器(我以前用过,所以我可以很容易地确定某个图形是否足够清晰)、正方形的大小(=7cm)和棋盘的形状(= 8hor 和 6vert 正方形)。
我的第一个猜测是 imshow 功能不起作用,但事实并非如此。当我运行这个脚本时,一切正常:
import numpy as np
import cv2
import glob
images = glob.glob('C:/tmp/pixelgrootte/*.jpg')
for fname in images:
img = cv2.imread(fname)
cv2.imshow('img',img)
cv2.waitKey(500)
cv2.destroyAllWindows()
于是我决定运行调试器。调试器显示 img, imgpoints en corners2 是空的。所以也许功能cornerSubPix在我的情况下不起作用?但我真的不知道为什么这不起作用。
这段代码的奇怪之处在于它在两周前运行良好,但从那以后我重新安装了我的电脑,因为它运行缓慢。现在,我已经安装了当前版本:
Windows 7 专业版 N 32 位
Python 2.7.9
Numpy 1.9.2
Opencv 2.4.11
所以我的猜测是我没有安装运行此脚本所需的软件的一部分,但我不知道它是什么。
我已经对此主题进行了一些研究,但在此论坛上找不到适合我的问题的任何解决方案。这似乎是一个常见错误,但我无法弄清楚导致此错误的原因。
有没有人可以帮助我解决这个问题?我将不胜感激!
【问题讨论】:
标签: python opencv camera-calibration