2017-09-20 62 views
-1

我使用的源代码示例从Open CV成为Python文档如下:的Python + OpenCV的名称没有定义

import numpy as np 
import cv2 
import glob 

# termination criteria in this, 30 max number of iterations, 0.001 minimum accuracy 
# CV_TERMCRIT_ITER or CV_TERMCRIT_EPS, tells the algorithm that we want to terminate either after some number of iterations or when the convergence metric reaches some small value (respectively). 
# The next two arguments set the values at which one, the other, or both of these criteria should terminate the algorithm. 
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*9,3), np.float32) 
objp[:,:2] = np.mgrid[0:9,0:6].T.reshape(-1,2) 

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

images = glob.glob('*.jpg') 

# fname= 'C:\\Users\\Bender\\Desktop\\fotospayloads\\' 

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, (9,6), 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, (9,6), corners2, ret) 
     cv2.imshow('img', img) 
     cv2.waitKey(500) 

cv2.destroyAllWindows() 

rms, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None) 

不幸的是,当我运行的源代码,我得到了以下错误: “NameError:名称'灰色'未定义“(第50行)。

任何帮助将非常感激。

感谢

艾萨克

+1

“图像”实际上包含什么? 它可能为空,然后未定义或初始化“灰色”。 尝试添加一个打印(甚至更好,使用pdb)以查看“图像”包含的内容。 – Idan

+1

由于您的代码没有50行,因此您无法获得有关第50行的此类消息。请查看[如何创建最小,完整和可验证示例](http://stackoverflow.com/帮助/ mcve),然后更新问题。 – John1024

回答

0

有在文件夹中没有图像在您的脚本所在,这就是为什么glob.glob(“ JPG”)不返回任何文件和灰色对象未创建。

相关问题