11import numpy as np
22import cv2 as cv
33
4- def select_img_from_video (input_file , board_pattern , select_all = False , wait_msec = 10 ):
4+ def select_img_from_video (video_file , board_pattern , select_all = False , wait_msec = 10 , wnd_name = 'Camera Calibration' ):
55 # Open a video
6- video = cv .VideoCapture (input_file )
7- assert video .isOpened (), 'Cannot read the given input, ' + input_file
6+ video = cv .VideoCapture (video_file )
7+ assert video .isOpened ()
88
99 # Select images
1010 img_select = []
@@ -20,21 +20,19 @@ def select_img_from_video(input_file, board_pattern, select_all=False, wait_msec
2020 # Show the image
2121 display = img .copy ()
2222 cv .putText (display , f'NSelect: { len (img_select )} ' , (10 , 25 ), cv .FONT_HERSHEY_DUPLEX , 0.6 , (0 , 255 , 0 ))
23- cv .imshow ('Camera Calibration' , display )
23+ cv .imshow (wnd_name , display )
2424
2525 # Process the key event
2626 key = cv .waitKey (wait_msec )
27- if key == 27 : # 'ESC' key: Exit (Complete image selection)
28- break
29- elif key == ord (' ' ): # 'Space' key: Pause and show corners
27+ if key == ord (' ' ): # Space: Pause and show corners
3028 complete , pts = cv .findChessboardCorners (img , board_pattern )
3129 cv .drawChessboardCorners (display , board_pattern , pts , complete )
32- cv .imshow ('Camera Calibration' , display )
30+ cv .imshow (wnd_name , display )
3331 key = cv .waitKey ()
34- if key == 27 : # ESC
35- break
36- elif key == ord ( ' \r ' ):
37- img_select . append ( img ) # 'Enter' key: Select the image
32+ if key == ord ( ' \r ' ):
33+ img_select . append ( img ) # Enter: Select the image
34+ if key == 27 : # ESC: Exit (Complete image selection)
35+ break
3836
3937 cv .destroyAllWindows ()
4038 return img_select
@@ -47,23 +45,21 @@ def calib_camera_from_chessboard(images, board_pattern, board_cellsize, K=None,
4745 complete , pts = cv .findChessboardCorners (gray , board_pattern )
4846 if complete :
4947 img_points .append (pts )
50- assert len (img_points ) > 0 , 'There is no set of complete chessboard points!'
48+ assert len (img_points ) > 0
5149
5250 # Prepare 3D points of the chess board
5351 obj_pts = [[c , r , 0 ] for r in range (board_pattern [1 ]) for c in range (board_pattern [0 ])]
54- obj_points = [np .array (obj_pts , dtype = np .float32 ) * board_cellsize ] * len (img_points ) # Must be ' np.float32'
52+ obj_points = [np .array (obj_pts , dtype = np .float32 ) * board_cellsize ] * len (img_points ) # Must be ` np.float32`
5553
5654 # Calibrate the camera
5755 return cv .calibrateCamera (obj_points , img_points , gray .shape [::- 1 ], K , dist_coeff , flags = calib_flags )
5856
59-
60-
6157if __name__ == '__main__' :
62- input_file = '../data/chessboard.avi'
58+ video_file = '../data/chessboard.avi'
6359 board_pattern = (10 , 7 )
6460 board_cellsize = 0.025
6561
66- img_select = select_img_from_video (input_file , board_pattern )
62+ img_select = select_img_from_video (video_file , board_pattern )
6763 assert len (img_select ) > 0 , 'There is no selected images!'
6864 rms , K , dist_coeff , rvecs , tvecs = calib_camera_from_chessboard (img_select , board_pattern , board_cellsize )
6965
0 commit comments