2017-09-20 104 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」)不返回任何文件和灰色對象未創建。

相關問題