Camera Calibration opencv python imshow error: (-215) size.width>0 && size.height>0
Camera Calibration opencv python imshow error: (-215) size.width>0 && size.height>0
对于我的论文项目,我需要确定相机的 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\School14-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 函数在我的情况下不起作用?但我真的不知道为什么这行不通。
这段代码的奇怪之处在于它在两周前运行良好,但从那以后我重新安装了我的计算机,因为它 运行 速度很慢。现在,我安装了当前版本:
Windows7专业N 32bit
Python 2.7.9
麻木 1.9.2
Opencv 2.4.11
所以我猜我有一部分软件需要 运行 这个脚本没有安装,但我不知道它是什么。
我已经对这个主题做了一些研究,但是我在这个论坛上找不到适合我的问题的任何解决方案。这似乎是一个常见错误,但我无法弄清楚导致此错误的原因。
有人可以帮我解决这个问题吗?我将不胜感激!
错误源于这一行:
img = cv2.drawChessboardCorners(img, (5,7), corners2,ret)
根据 docs、cv2.drawChessboardCorners
returns None
。所以不要重新绑定 img
.
drawChessboardCorners
的第一个参数是目标图像(再次来自文档),它将渲染检测到的棋盘角。
您link的教程适用于opencv 版本3.x,如this github issue中所述,作者遇到相同的情况问题和你一样。
this SO answer.
中提供了工作代码(使用 opencv 2.3.1.7 测试,其中提到其 __version__
是 $Rev: 4557 $
)
对于我的论文项目,我需要确定相机的 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\School14-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 函数在我的情况下不起作用?但我真的不知道为什么这行不通。
这段代码的奇怪之处在于它在两周前运行良好,但从那以后我重新安装了我的计算机,因为它 运行 速度很慢。现在,我安装了当前版本:
Windows7专业N 32bit
Python 2.7.9
麻木 1.9.2
Opencv 2.4.11
所以我猜我有一部分软件需要 运行 这个脚本没有安装,但我不知道它是什么。
我已经对这个主题做了一些研究,但是我在这个论坛上找不到适合我的问题的任何解决方案。这似乎是一个常见错误,但我无法弄清楚导致此错误的原因。
有人可以帮我解决这个问题吗?我将不胜感激!
错误源于这一行:
img = cv2.drawChessboardCorners(img, (5,7), corners2,ret)
根据 docs、cv2.drawChessboardCorners
returns None
。所以不要重新绑定 img
.
drawChessboardCorners
的第一个参数是目标图像(再次来自文档),它将渲染检测到的棋盘角。
您link的教程适用于opencv 版本3.x,如this github issue中所述,作者遇到相同的情况问题和你一样。 this SO answer.
中提供了工作代码(使用 opencv 2.3.1.7 测试,其中提到其__version__
是 $Rev: 4557 $
)