def preview_charuco(): """ Display an image until user presses "q" to quit """ charuco = charucoBoard.draw(SCREEN_RESOLUTION) show_fullscreen_image(charuco) while True: if cv2.waitKey(1) & 255 == ord(QUIT_KEY): break cv2.destroyAllWindows()
def calibrate_camera(): """ Calibrate our camera """ # Display Charuco Calibration Board charuco = charucoBoard.draw(SCREEN_RESOLUTION) show_fullscreen_image(charuco) # Step 1: Initialize Camera # stream = cv2.VideoCapture(0) # stream.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) # stream.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080) stream = rs.RealSenseCamera() time.sleep(2) # Step 2: Identify Charuco Corners and IDs corners, ids = get_charuco_corners(stream) # Step 3: Calculate and Save Camera Matrix & Distortion Coefficients print('Finished collecting data, computing...') try: err, camera_matrix, dist_coeffs, _, _ = aruco.calibrateCameraCharuco( corners, ids, charucoBoard, CAMERA_RESOLUTION, None, None) print('Calibrated with error: ', err) save_json({ 'camera_matrix': camera_matrix.tolist(), 'dist_coeffs': dist_coeffs.tolist(), 'err': err }) print('...DONE') except Exception as e: print(e) # Step 4: Generate Undistortion / Rectify Map camera_matrix, dist_coeffs = load_camera_props(CAMERA_CONFIG_PATH) mapx, mapy = get_undistort_maps(camera_matrix, dist_coeffs) # Step 5: Show Calibrated Image while True: #cap_success, frame = stream.read() frame = stream.read_rgb() frame = undistort_image(frame, mapx, mapy) cv2.imshow('Calibrated Image', frame) if cv2.waitKey(1) & 255 == ord('q'): break # stream.stop() cv2.destroyAllWindows()
def calibrate_camera(): """ Calibrate our Camera """ required_count = 50 resolution = (960, 720) stream = VideoStream(usePiCamera=True, resolution=resolution).start() time.sleep(2) # Warm up the camera all_corners = [] all_ids = [] frame_idx = 0 frame_spacing = 5 success = False calibration_board = charucoBoard.draw((1680, 1050)) show_calibration_frame(calibration_board) while True: frame = stream.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) marker_corners, marker_ids, _ = aruco.detectMarkers( gray, charucoDictionary, parameters=detectorParams) if len(marker_corners) > 0 and frame_idx % frame_spacing == 0: ret, charuco_corners, charuco_ids = aruco.interpolateCornersCharuco( marker_corners, marker_ids, gray, charucoBoard ) if charuco_corners is not None and charuco_ids is not None and len(charuco_corners) > 3: all_corners.append(charuco_corners) all_ids.append(charuco_ids) aruco.drawDetectedMarkers(gray, marker_corners, marker_ids) if cv2.waitKey(1) & 255 == ord('q'): break frame_idx += 1 print("Found: " + str(len(all_ids)) + " / " + str(required_count)) if len(all_ids) >= required_count: success = True break
def calibrate_camera(camera_name): """ Calibrate our camera """ required_count = 50 resolution = (640, 480) cap = cv2.VideoCapture(0) #stream = VideoStream(usePiCamera=True, resolution=resolution).start() #time.sleep(2) # Warm up the camera all_corners = [] all_ids = [] frame_idx = 0 frame_spacing = 10 success = False calibration_board = charucoBoard.draw((1680, 1050)) show_calibration_frame(calibration_board) while True: ret, frame = cap.read() #frame = stream.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) marker_corners, marker_ids, _ = aruco.detectMarkers( gray, charucoDictionary) if len(marker_corners) > 0 and frame_idx % frame_spacing == 0: ret, charuco_corners, charuco_ids = aruco.interpolateCornersCharuco( marker_corners, marker_ids, gray, charucoBoard) if charuco_corners is not None and charuco_ids is not None and len( charuco_corners) > 3: all_corners.append(charuco_corners) all_ids.append(charuco_ids) aruco.drawDetectedMarkers(gray, marker_corners, marker_ids) cv2.imshow( 'frame', gray) # If we're showing the calibration image, we can't preview if cv2.waitKey(1) & 255 == ord('q'): break frame_idx += 1 print("Found: " + str(len(all_ids)) + " / " + str(required_count)) if len(all_ids) >= required_count: success = True break hide_calibration_frame() if success: print('Finished collecting data, computing...') try: err, camera_matrix, dist_coeffs, rvecs, tvecs = aruco.calibrateCameraCharuco( all_corners, all_ids, charucoBoard, resolution, None, None) print('Calibrated with error: ', err) save_json( { 'camera_matrix': camera_matrix.tolist(), 'dist_coeffs': dist_coeffs.tolist(), 'err': err }, camera_name) print('...DONE') except Exception as e: print(e) success = False # Generate the corrections new_camera_matrix, valid_pix_roi = cv2.getOptimalNewCameraMatrix( camera_matrix, dist_coeffs, resolution, 0) mapx, mapy = cv2.initUndistortRectifyMap(camera_matrix, dist_coeffs, None, new_camera_matrix, resolution, 5) while True: ret, frame = cap.read() #frame = stream.read() if mapx is not None and mapy is not None: frame = cv2.remap(frame, mapx, mapy, cv2.INTER_LINEAR) cv2.imshow('frame', frame) if cv2.waitKey(1) & 255 == ord('q'): break cap.release() #stream.stop() cv2.destroyAllWindows()