def Calibrate_camera(img_list): counter = [] corners_list = [] id_list = [] first = True for im in img_list: img_gray = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY) corners, ids, rejectedImgPoints = aruco.detectMarkers( img_gray, aruco_dict, parameters=arucoParams) if first == True: corners_list = corners print(type(corners)) id_list = ids first = False else: corners_list = np.vstack((corners_list, corners)) id_list = np.vstack((id_list, ids)) counter.append(len(ids)) counter = np.array(counter) print("Calibrating camera .... Please wait...") #mat = np.zeros((3,3), float) ret, mtx, dist, rvecs, tvecs = aruco.calibrateCameraAruco( corners_list, id_list, counter, board, img_gray.shape, None, None) print("Camera matrix:\n", mtx, "\ndistCoeffs:\n", dist) return mtx, dist
def calibrate_aruco(dirpath, image_format, marker_length, marker_separation): aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_1000) arucoParams = aruco.DetectorParameters_create() board = aruco.GridBoard_create(5, 7, marker_length, marker_separation, aruco_dict) counter, corners_list, id_list = [], [], [] img_dir = pathlib.Path(dirpath) first = 0 # Find the ArUco markers inside each image for img in img_dir.glob(f'*.{image_format}'): image = cv2.imread(str(img)) img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) corners, ids, rejected = aruco.detectMarkers(img_gray, aruco_dict, parameters=arucoParams) if first == 0: corners_list = corners id_list = ids else: corners_list = np.vstack((corners_list, corners)) id_list = np.vstack((id_list, ids)) first = first + 1 counter.append(len(ids)) counter = np.array(counter) # Actual calibration ret, mtx, dist, rvecs, tvecs = aruco.calibrateCameraAruco( corners_list, id_list, counter, board, img_gray.shape, None, None) return [ret, mtx, dist, rvecs, tvecs]
def calibrate_aruco(allCornersConcat, allIdsConcat, markerCounter, board, video_params): print("calibrating...") tstart = time() cameraMat = np.eye(3) distCoeffs = np.zeros(5) dim = (video_params['width'], video_params['height']) error, cameraMat, distCoeffs, rvecs, tvecs = aruco.calibrateCameraAruco( allCornersConcat, allIdsConcat, markerCounter, board, dim, cameraMat, distCoeffs) tend = time() tdiff = tend - tstart print("calibration took {} minutes and {:.1f} seconds".format( int(tdiff / 60), tdiff - int(tdiff / 60) * 60)) out = dict() out['error'] = error out['camera_mat'] = cameraMat.tolist() out['dist_coeff'] = distCoeffs.tolist() out['width'] = video_params['width'] out['height'] = video_params['height'] out['fps'] = video_params['fps'] return out
def main(marker_length, marker_separation): cam = cv2.VideoCapture(0) ardict = aruco.Dictionary_get(aruco.DICT_4X4_1000) parameters = aruco.DetectorParameters_create() # create arUco board board = aruco.GridBoard_create(4, 5, marker_length, marker_separation, ardict) shape = None counter, corners_list, id_list = [], [], [] while cam.isOpened(): _, frame = cam.read() gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) shape = gray.shape corners, ids, rejected_img_points = aruco.detectMarkers( gray, ardict, parameters=parameters) # rvecs, tvecs, trash = aruco.estimatePoseSingleMarkers(corners, size_of_marker, mtx, dist) if corners is not None: aruco.drawDetectedMarkers(frame, corners, ids) cv2.imshow("123", frame) k = cv2.waitKey(17) if k != -1: print(k) if k == ord('p'): if len(corners_list) == 0: corners_list = corners id_list = ids else: corners_list = np.vstack((corners, corners_list)) id_list = np.vstack((ids, id_list)) counter.append(len(ids)) if k == ord('q'): cam.release() break counter = np.array(counter) print(corners_list, id_list, counter, shape, sep='\n') ret, mtx, dist, rvecs, tvecs = aruco.calibrateCameraAruco( corners_list, id_list, counter, board, shape, None, None) data = { 'camera_matrix': np.asarray(mtx).tolist(), 'dist_coeff': np.asarray(dist).tolist() } f = open("calib.json", 'w') json.dump(data, f) f.close()
def example_calibrate_folder(folder_in, folder_out,marker_length_mm = 3.75, marker_space_mm = 0.5,dct = aruco.DICT_6X6_1000): tools_IO.remove_files(folder_out) num_cols, num_rows = 4, 5 board = aruco.GridBoard_create(num_cols, num_rows, marker_length_mm, marker_space_mm,aruco.getPredefinedDictionary(dct)) filenames = numpy.unique(tools_IO.get_filenames(folder_in, '*.jpg,*.png'))[:3] counter, corners_list, id_list, first = [], [], [], True for filename_in in filenames: base_name, ext = filename_in.split('/')[-1].split('.')[0], filename_in.split('/')[-1].split('.')[1] image = cv2.imread(folder_in + filename_in) img_gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY) corners, ids, rejectedImgPoints = aruco.detectMarkers(img_gray, aruco.getPredefinedDictionary(dct)) if first == True: corners_list = corners id_list = ids first = False else: corners_list = numpy.vstack((corners_list, corners)) id_list = numpy.vstack((id_list,ids)) counter.append(len(ids)) image_temp = tools_image.desaturate(image.copy()) aruco.drawDetectedMarkers(image_temp, corners) cv2.imwrite(folder_out+base_name+'.png',image_temp) print(base_name) counter = numpy.array(counter) ret, camera_matrix, dist, rvecs, tvecs = aruco.calibrateCameraAruco(corners_list, id_list, counter, board, img_gray.shape, None, None ) print(camera_matrix) return
def calibrate(self): """calibrating camera """ # root directory of repo for relative path specification. root = Path(__file__).parent.absolute() # Set path to the images calib_imgs_path = root.joinpath("cam_data") # uncomment following block to draw and show the board img = self.board.draw((720, 1280)) cv2.imshow("aruco", img) img_list = [] calib_fnms = calib_imgs_path.glob('*.jpg') print('Using ...', end='') for idx, fn in enumerate(calib_fnms): print(idx, '', end='') img = cv2.imread(str(root.joinpath(fn))) img_list.append(img) h, w, c = img.shape print('Calibration images') counter, corners_list, id_list = [], [], [] first = True for im in tqdm(img_list): img_gray = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY) corners, ids, rejectedImgPoints = aruco.detectMarkers( img_gray, self.aruco_dict, parameters=self.aruco_params) if first == True: corners_list = corners id_list = ids first = False else: corners_list = np.vstack((corners_list, corners)) id_list = np.vstack((id_list, ids)) counter.append(len(ids)) print('Found {} unique markers'.format(np.unique(ids))) counter = np.array(counter) print("Calibrating camera .... Please wait...") #mat = np.zeros((3,3), float) ret, mtx, dist, rvecs, tvecs = aruco.calibrateCameraAruco( corners_list, id_list, counter, self.board, img_gray.shape, None, None) a = "Camera matrix is \n" b = "\n And is stored in calibration.yaml file\n" c = "along with distortion coefficients : \n" print(a, mtx, b + c, dist) data = { 'camera_matrix': np.asarray(mtx).tolist(), 'dist_coeff': np.asarray(dist).tolist() } with open("calibration.yaml", "w") as f: yaml.dump(data, f) cv2.destroyAllWindows()
def calibrate_camera(self, counter, shape): # distCoeff - вектор коэффициентов искажения _, self.camera_martix, self.dist_coeff, _, _ = aruco.calibrateCameraAruco( corners=self.corners, ids=self.ids, counter=counter, board=board, imageSize=shape, cameraMatrix=None, distCoeffs=None)
def calibrate_camera(image_dir, board, aruco_dict, aruco_params): """ Calibrate camera based on the images in image_dir :image_dir: str :board: aruco.GridBoard obj :aruco_dict: aruco.Dictionary obj :aruco_params: aruco.DetectorParameters obj """ img_list = [] calib_fnms = [ os.path.join(image_dir, i) for i in os.listdir(image_dir) if i[-4:] == '.jpg' ] print('Using', len(calib_fnms), 'images') for file_name in calib_fnms: img = cv2.imread(file_name) img_list.append(img) print('Calibration images') counter, corners_list, id_list = [], [], [] first = True for im in tqdm(img_list): img_gray = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY) corners, ids, rejectedImgPoints = aruco.detectMarkers( img_gray, aruco_dict, parameters=aruco_params) if first == True: corners_list = corners id_list = ids first = False else: corners_list = np.vstack((corners_list, corners)) id_list = np.vstack((id_list, ids)) counter.append(len(ids)) print('Found {} unique markers'.format(np.unique(ids))) counter = np.array(counter) print("Calibrating camera .... Please wait...") #mat = np.zeros((3,3), float) ret, mtx, dist, rvecs, tvecs = aruco.calibrateCameraAruco( corners_list, id_list, counter, board, img_gray.shape, None, None) print( "Camera matrix is \n", mtx, "\n And is stored in calibration.yaml file along with distortion coefficients : \n", dist) data = { 'camera_matrix': np.asarray(mtx).tolist(), 'dist_coeff': np.asarray(dist).tolist() } with open("calibration.yaml", "w") as f: yaml.dump(data, f)
def main(): board = arucoboard.arucoboard("../config/aruco.yml") root_dir = "D:\\data\\fbs_data\\1129\\kinect" img_list, depth_image_list = getImgList(root_dir) if len(img_list) != len(depth_image_list): print("numer of img and depth not same") return 0 # if len(img_list)<10: # print("number of img not enough") # return 0 depth_list = [] image_size = tuple([640,480]) image_list = [] obj_list = [] print(board.GetBoardAllPoints()) corners_list = [] id_list = [] counter = [] for i in range(len(img_list)): image = cv2.imread(img_list[i]) img_corners, ids, rejectedImgPoints = aruco.detectMarkers(image, board.dictionary, parameters=board.parameters) if len(corners_list) == 0: corners_list = img_corners id_list = ids else: corners_list = np.vstack((img_corners, corners_list)) id_list = np.vstack((ids, id_list)) counter.append(len(ids)) # imagepoints,objpoints = board.getObjImgPointList(image) # depth_points,rejectids = depthUtils.get_depth(imagepoints,depth_image_list[i]) # image_list.append(np.delete(imagepoints,rejectids,axis=0)) # depth_list.append(np.delete(depth_points,rejectids,axis=0)) # obj_list.append(np.delete(objpoints,rejectids,axis=0)) counter = np.array(counter) retval, cameraMatrix,distCoeffs, rvecs, tvecs = aruco.calibrateCameraAruco(corners_list,id_list,counter,board.board,image_size,None,None) rme, intrinsic,discoff = board.intrinsic(image_list,obj_list,image_size) print(intrinsic,rme) intrinsic,discoff = board.intrinsic_depth_opt(obj_list,image_list,depth_list,intrinsic,discoff) fs = cv2.FileStorage(root_dir+"/intrinsic.yml", cv2.FileStorage_WRITE) fs.write("cameraMatrix", intrinsic) fs.write("discoff", discoff) fs.release() return 0
def example_calibrate_aruco_markers(filename_in, marker_length_mm = 3.75, marker_space_mm = 0.5, dct = aruco.DICT_6X6_1000): image = cv2.imread(filename_in) gray = tools_image.desaturate(image) scale = (marker_length_mm / 2, marker_length_mm / 2, marker_length_mm / 2) num_cols, num_rows = 4,5 board = aruco.GridBoard_create(num_cols, num_rows, marker_length_mm, marker_space_mm,aruco.getPredefinedDictionary(dct)) image_AR, image_cube = gray.copy(), gray.copy() base_name, ext = filename_in.split('/')[-1].split('.')[0], filename_in.split('/')[-1].split('.')[1] #board_width_px = int((num_cols * marker_length_mm + (num_cols - 1) * marker_space_mm)) #board_height_px= int((num_rows * marker_length_mm + (num_rows - 1) * marker_space_mm)) #image_board = aruco.drawPlanarBoard(board, (board_width_px, board_height_px)) camera_matrix = None#tools_pr_geom.compose_projection_mat_3x3(image.shape[1], image.shape[0]) corners, ids, _ = aruco.detectMarkers(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY), aruco.getPredefinedDictionary(dct)) if len(corners)>0: if len(corners)==1:corners,ids = numpy.array([corners]),numpy.array([ids]) else:corners, ids = numpy.array(corners), numpy.array(ids) counters = numpy.array([len(ids)]) ret, camera_matrix, dist, rvecs, tvecs = aruco.calibrateCameraAruco(corners,ids,counters, board, gray.shape[:2],None, None) image_markers = [tools_image.saturate(aruco.drawMarker(aruco.getPredefinedDictionary(dct), id, 100)) for id in ids] #!!! #camera_matrix = tools_pr_geom.compose_projection_mat_3x3(4200,4200) for i,image_marker in enumerate(image_markers): rvecs, tvecs, _ = aruco.estimatePoseSingleMarkers(corners[i], marker_length_mm, camera_matrix, numpy.zeros(5)) image_AR = tools_render_CV.draw_image(image_AR,image_marker, camera_matrix, numpy.zeros(5), numpy.array(rvecs).flatten(), numpy.array(tvecs).flatten(),scale) image_cube = tools_render_CV.draw_cube_numpy(image_cube, camera_matrix, numpy.zeros(5), numpy.array(rvecs).flatten(),numpy.array(tvecs).flatten(), scale) cv2.imwrite(folder_out + base_name+'_AR.png', image_AR) cv2.imwrite(folder_out + base_name+'_AR_cube.png', image_cube) print(camera_matrix) return camera_matrix
def calibrate_aruco(allCornersConcat, allIdsConcat, markerCounter, board, video_params): print("calibrating...") tstart = time() cameraMat = np.eye(3) distCoeffs = np.zeros(5) dim = (video_params["width"], video_params["height"]) calib_flags = ( cv2.CALIB_ZERO_TANGENT_DIST + cv2.CALIB_FIX_K3 + cv2.CALIB_FIX_PRINCIPAL_POINT ) error, cameraMat, distCoeffs, rvecs, tvecs = aruco.calibrateCameraAruco( allCornersConcat, allIdsConcat, markerCounter, board, dim, cameraMat, distCoeffs, flags=calib_flags, ) tend = time() tdiff = tend - tstart print( "calibration took {} minutes and {:.1f} seconds".format( int(tdiff / 60), tdiff - int(tdiff / 60) * 60 ) ) out = dict() out["error"] = error out["camera_mat"] = cameraMat.tolist() out["dist_coeff"] = distCoeffs.tolist() out["width"] = video_params["width"] out["height"] = video_params["height"] out["fps"] = video_params["fps"] return out
def calibrate_camera(aruco_path, calibration_path): img_list = [] for img_path in glob.glob(aruco_path + "/*.jpg"): img = cv2.imread(img_path) img_list.append(img) counter = [] corners_list = [] id_list = [] first = True for im in img_list: img_gray = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY) corners, ids, _ = aruco.detectMarkers(img_gray, aruco_dict, parameters=arucoParams) if first: corners_list = corners id_list = ids first = False else: corners_list = np.vstack((corners_list, corners)) id_list = np.vstack((id_list, ids)) counter.append(len(ids)) counter = np.array(counter) ret, mtx, dist, rvecs, tvecs = aruco.calibrateCameraAruco( corners_list, id_list, counter, board, img_gray.shape, None, None) data = { 'camera_matrix': np.asarray(mtx).tolist(), 'dist_coeff': np.asarray(dist).tolist() } with open(calibration_path, "w") as f: yaml.dump(data, f)
allIdsConcatenated = [] for i in range(len(all_corners)): markerCounterPerFrame.append(len(all_corners[i])) for j in range(len(all_corners[i])): allCornersConcatenated.append(all_corners[i][j]) allIdsConcatenated.append(all_ids[i][j]) all_corners = np.array(all_corners) allIdsConcatenated = np.array(allIdsConcatenated) markerCounterPerFrame = np.array(markerCounterPerFrame) camera_matrix = np.zeros(shape=(3, 3)) camera_matrix[0:3, 0:3] = np.identity(3) camera_matrix[0:2, 2] = np.array([n / 2, m / 2]) dist_coeffs = np.zeros(5) res, intrinsic, dist, rvecs, tvecs = aruco.calibrateCameraAruco(allCornersConcatenated, allIdsConcatenated, markerCounterPerFrame, board, (m, n), camera_matrix, dist_coeffs, None, None, 2) res = np.array([res]) from datetime import datetime as dt tdatetime = dt.now() tstr = tdatetime.strftime('%Y-%m-%d_%H-%M-%S') np.savez(os.path.join(ARGS.data_path, "params_{}.npz".format(tstr)), res=res, intrinsic=intrinsic, dist=dist) print(intrinsic) print(dist)
first = True for img_sample in img_list: img_gray = cv.cvtColor(img_sample, cv.COLOR_RGB2GRAY) corners, ids, rejectedImgPoints = aruco.detectMarkers(img_gray, aruco_dict) if first: corners_list = corners id_list = ids first = False else: corners_list = np.vstack((corners_list, corners)) id_list = np.vstack((id_list, ids)) counter.append(len(ids)) print('Found {} unique markers'.format(np.unique(id_list))) counter = np.array(counter) # aruco.drawDetectedMarkers(img, corners, ids, (255, 0, 0)) # cv.imshow("Frame", img) # cv.waitKey(0) ret, cameraMatrix, distCoeffs, _, _ = aruco.calibrateCameraAruco(corners_list, id_list, counter, aruco_board, img_gray.shape, None, None) print("Camera matrix is \n", cameraMatrix) print() print("Distance coeff are \n", distCoeffs) aruco.drawDetectedMarkers(img_gray, corners, ids) cv.imshow("Frame", img_gray) cv.waitKey(0)
def calibrate_cap(cap, num, step): """ Calibrates the camera for intrinsic parameters. Parameters: cap : The video object. num: Number of frames that will be used for calibration. step: Distance between frames. Returns: cameraMatrix: Camera matrix. distCoeffs: Distortion coefficients. """ # create dictionary aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_100) # Creating a theoretical board we'll use to calculate marker positions board = aruco.GridBoard_create(markersX=5, markersY=7, markerLength=0.047, markerSeparation=0.012, dictionary=aruco_dict) # Initialize parameters and criteria for markers' detection parameters = aruco.DetectorParameters_create() criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-3) counter = [] corners_list = [] id_list = [] first = True # For each image detect markers. for i in range(num): for t in range(step): ret, img2 = cap.read() ret, img = cap.read() if not ret: break gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Detect Aruco markers corners, ids, rejectedImgPoints = aruco.detectMarkers( gray, aruco_dict, parameters=parameters) for k in range(len(corners)): cv2.cornerSubPix(gray, corners[k], (7, 7), (-1, -1), criteria) # Put each marker's id and corners in separate lists, # along with the number of markers if len(ids) > 1: if first: corners_list = corners print(type(corners)) id_list = ids first = False else: corners_list = np.vstack((corners_list, corners)) id_list = np.vstack((id_list, ids)) counter.append(len(ids)) # Calibrate counter = np.array(counter) ret, cameraMatrix, distCoeffs, rvecs, tvecs = aruco.calibrateCameraAruco( corners_list, id_list, counter, board, gray.shape, None, None) return cameraMatrix, distCoeffs
img_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) corners, ids, rejectedImgPoints = aruco.detectMarkers(img_gray, aruco_dict, parameters=arucoParams) if first: corners_list = corners id_list = ids first = False else: corners_list = np.vstack((corners_list, corners)) id_list = np.vstack((id_list, ids)) counter.append(len(ids)) print(f'Found {np.unique(ids)} unique markers') counter = np.array(counter) print('Calibrating camera .... Please wait...') ret, mtx, dist, rvecs, tvecs = aruco.calibrateCameraAruco(corners_list, id_list, counter, board, img_gray.shape, None, None) print("Camera matrix is \n", mtx, "\n And is stored in calibration.yaml file along with distortion coeddicients : \n", dist) data = {'camera_matrix': np.asarray(mtx).tolist(), 'dist_coeff': np.asarray(dist).tolist()} with open('calibration.yaml', 'w') as f: yaml.dump(data, f) else: camera = cv2.VideoCapture(0) ret, img = camera.read() with open('calibration.yaml') as f: loadeddict = yaml.load(f) mtx = np.array(loadeddict.get('camera_matrix')) dist = np.array(loadeddict.get('dist_coeff'))
if first: corners_list = corners # print(type(corners)) id_list = ids first = False else: corners_list = np.vstack((corners_list, corners)) id_list = np.vstack((id_list, ids)) counter.append(len(ids)) counter = np.array(counter) print(counter) print("Calibrating camera .... Please wait...") # mat = np.zeros((3,3), float) ret, mtx, dist, rvecs, tvecs = \ aruco.calibrateCameraAruco(corners_list, id_list, counter, board, img_gray.shape, None, None) print("Camera matrix is \n", mtx, "\n And is stored in calibration.yaml file along with distortion coefficients : \n", dist) data = {'camera_matrix': np.asarray(mtx).tolist(), 'dist_coeff': np.asarray(dist).tolist()} with open(os.path.join(path, "calibration.yaml"), "w") as f: yaml.dump(data, f) # validation camera = cv2.VideoCapture(0) ret, img = camera.read() with open('calibration.yaml') as f: loadeddict = yaml.load(f) mtx = loadeddict.get('camera_matrix') dist = loadeddict.get('dist_coeff')