0

I'm using a source code example from Open CV for Python documentation as follows:

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)

Unfortunately when I run the source code I get the following error: "NameError: name 'gray' is not defined" (line 50).

Any help would be very much appreciated.

Thanks

Isaac

2
  • 1
    What does "images" actually contain? It might be empty and then the "gray" is not defined or initialized. Try to add a print (or even better, use pdb) in order to see what "images" contains. Commented Sep 20, 2017 at 21:13
  • 1
    Since your code does not have 50 lines, it is not possible for you to have gotten such a message about line 50. Please review How to create a Minimal, Complete, and Verifiable example and then update the question. Commented Sep 20, 2017 at 21:15

1 Answer 1

1

There are no images in folder where your script is located and that is why glob.glob('.jpg') does not return any files and grey object is not created.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.