在我的论文中,我需要确定相机的K-失真参数。我决定用python和opencv来做is。所以你可以看到我是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))
运行此脚本时,收到的错误信息如下:
^{pr2}$如您所见,我使用了以下标准代码:https://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_calib3d/py_calibration/py_calibration.html#calibration。 唯一的区别是计数器(我以前用过它,所以我可以很容易地确定某个透视图是否足够清晰)、正方形的大小(=7cm)和棋盘的形状(=8hor和6vert squares)。在
我的第一个猜测是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 professional N 32位
Python
数字1.9.2
开放式CV 2.4.11
所以我的猜测是,我有一部分软件需要运行这个脚本没有安装,但我不知道它是什么。在
我已经对这个话题做了一些研究,但是我在这个论坛上找不到适合我问题的解决方案。这似乎是一个常见的错误,但我不知道是什么导致了这个错误。在
有人能帮我吗?我会非常感激的!在
错误源于这一行:
根据docs,}。所以不要重新绑定
cv2.drawChessboardCorners
返回{img
。在drawChessboardCorners
的第一个参数是目标图像(同样,来自文档),它将呈现检测到的棋盘角点。在您链接到的教程适用于opencvversion3.x,如this github issue中所述,作者在其中遇到了与您相同的问题。 工作代码(用opencv2.3.1.7测试,其中提到它的})在this SO answer中提供。在
__version__
是{相关问题 更多 >
编程相关推荐