以下のエラーの改善がわかりません。
UnboundLocalError: local variable 'gray' referenced before assignment
test2というフォルダにチェスボードを撮影した画像を10枚いれています。
最近始めたばかりでわからないことが多いのでご教授いただきたいです。よろしくお願いします。
以下のサイトは見ましたがよくわかりませんでした。
[https://teratail.com/questions/149453
]
python
1import numpy as np 2import cv2 3import glob 4 5def Calibration(path): 6 # termination criteria 7 criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) 8 9 # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0) 10 objp = np.zeros((6 * 8, 3), np.float32) 11 objp[:, :2] = np.mgrid[0:8, 0:6].T.reshape(-1, 2) 12 13 # Arrays to store object points and image points from all the images. 14 objpoints = [] # 3d point in real world space 15 imgpoints = [] # 2d points in image plane. 16 17 images = sorted(glob.glob(path + '*.jpg')) 18 19 i = 1 20 for fname in images: 21 img = cv2.imread(fname) 22 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 23 24 # Find the chess board corners 25 ret, corners = cv2.findChessboardCorners(gray, (8, 6), flags=cv2.CALIB_CB_FAST_CHECK) 26 print(ret) 27 # If found, add object points, image points (after refining them) 28 if ret == True: 29 objpoints.append(objp) 30 31 corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria) 32 imgpoints.append(corners2) 33 34 # Draw and display the corners 35 img = cv2.drawChessboardCorners(img, (8, 6), corners2, ret) 36 height = img.shape[0] 37 width = img.shape[1] 38 # cv2.imshow('img',img) 39 40 cv2.waitKey(500) 41 i = i + 1 42 cv2.destroyAllWindows() 43 44 ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None) 45 i = 0 46 for fname in images: 47 img = cv2.imread(fname) 48 h, w = img.shape[:2] 49 newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, (w, h)) 50 # undistort 51 52 dst = cv2.undistort(img, mtx, dist, None, newcameramtx) 53 # crop the image 54 x, y, w, h = roi 55 dst = dst[y:y + h, x:x + w] 56 cv2.imwrite('calibration_result/calibresult' + str(i)+ '.jpg', dst) 57 i = i + 1 58 print("A=\n{0}".format(newcameramtx)) 59 print("R=\n{0}".format(rvecs[0])) 60 print("t=\n{0}".format(tvecs[0])) 61 R_t = np.hstack((rvecs, tvecs)) 62 print("[R|t]={0}".format(R_t)) 63 64 65 np.save('camera_data/camera_matrix', newcameramtx) 66 np.save('camera_data/distCoeffs', dist) 67 np.save('camera_data/roi',roi) 68 69 return newcameramtx 70 71def video_split(path): 72 i = 0 73 cap = cv2.VideoCapture(path) 74 while (cap.isOpened()): 75 flag, frame = cap.read() # Capture frame-by-frame 76 if flag == False: # Is a frame left? 77 break 78 if i % 50 ==0: 79 cv2.imwrite("calb_test/test2/" + str(i) + ".jpg", frame) # Save a frame 80 print("test" + str(i) + ".jpg") 81 i += 1 82 83 cap.release() # When everything done, release the capture 84# 85if __name__ == '__main__': 86 # video_split('calb_test/test.mov') 87 A = Calibration('calb_test/test2/') 88 print(A)
Traceback (most recent call last): File "C:/Users/tampa/PycharmProjects/camera-calib/Camera_Calibration.py", line 134, in <module> A = Calibration('calb_test/test2/') File "C:/Users/tampa/PycharmProjects/camera-calib/Camera_Calibration.py", line 91, in Calibration ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None) UnboundLocalError: local variable 'gray' referenced before assignment
回答1件
あなたの回答
tips
プレビュー