我是Python中OpenCV编程的新手。尝试了最基本的相机送料程序,进展顺利。当使用OpenCV Calibration and 3d Reconstruction教程尝试校准针孔摄像机时,imshow()
之后drawChessboardCorners()
函数会出现断言错误。
我使用了大量的打印命令进行调试,看到它成功找到了角点,显示从文件中读取后的图像类型为numpy.ndarray
,这对我来说非常困惑。
imread()
如何返回numpy.ndarray
?
请帮忙。
我的opencv版本是2.4.6,而windows8 64位的python版本是2.7。
代码:
import numpy as np
import cv2
import glob
#termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
#prepare object points, like (0,0,0,), (1,0,0), (2,0,0)......, (6,5,0)
objp = np.zeros((6*7,3), np.float32)
objp[:,:2] = np.mgrid[0:7, 0:6].T.reshape(-1, 2)
#Arrays to store object points and image points from all the images.
objpoints = [] #3d points in real world
imgpoints = [] #2d points in image plane
images = glob.glob('images\*.jpg')
#print images
print ' '*8
for fname in images:
img = cv2.imread(fname)
#size = (int(img.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)), int(img.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
print ' '
print ' '
print 'Type of image after reading', type(img)
#print img.ndim
#print img.size.width
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print 'Processing', fname
#find chessboard corners
ret, corners = cv2.findChessboardCorners(gray, (7, 6), None)
#print 'Corners', corners
#print ret
#If found, add object points, image points (after refining them)
if ret == True:
print 'Corners found in', fname
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
print 'Corners2', corners2
imgpoints.append(corners2)
#Draw and display the corners
img = cv2.drawChessboardCorners(img, (7, 6), corners2, ret)
print 'Type of img after drawChessboardCorners', type(img)
cv2.imshow('img', img)
cv2.waitKey(500)
else:
print 'Corners not found in', fname
cv2.destroyAllWindows()
输出:
Type of image after reading <type 'numpy.ndarray'>
Processing images\left01.jpg
Corners found in images\left01.jpg
Corners2 None
Type of image after drawChessboardCorners <type 'NoneType'>
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in unknown function, file ..\..\..\src\opencv\modules\highgui\src\window.cpp, line 261
Traceback (most recent call last):
File: "PythonCameraCalibration.py", Line 47, in<module>
cv2.imshow('img',img)
cv2.error: ..\..\..\src\opencv\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0
答案 0 :(得分:5)
在我看来,问题在于您正在分配
的输出cv2.drawChessboardCorners(img, (7, 6), corners2, ret)
到img,本教程不做。这个函数调用可能没有返回任何东西。尝试从该行删除作业,看看会发生什么。