我们从Python开源项目中,提取了以下29个代码示例,用于说明如何使用cv2.TERM_CRITERIA_MAX_ITER。
def find_points(images): pattern_size = (9, 6) obj_points = [] img_points = [] # Assumed object points relation a_object_point = np.zeros((PATTERN_SIZE[1] * PATTERN_SIZE[0], 3), np.float32) a_object_point[:, :2] = np.mgrid[0:PATTERN_SIZE[0], 0:PATTERN_SIZE[1]].T.reshape(-1, 2) # Termination criteria for sub pixel corners refinement stop_criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, 0.001) print('Finding points ', end='') debug_images = [] for (image, color_image) in images: found, corners = cv.findChessboardCorners(image, PATTERN_SIZE, None) if found: obj_points.append(a_object_point) cv.cornerSubPix(image, corners, (11, 11), (-1, -1), stop_criteria) img_points.append(corners) print('.', end='') else: print('-', end='') if DEBUG: cv.drawChessboardCorners(color_image, PATTERN_SIZE, corners, found) debug_images.append(color_image) sys.stdout.flush() if DEBUG: display_images(debug_images, DISPLAY_SCALE) print('\nWas able to find points in %s images' % len(img_points)) return obj_points, img_points # images is a lis of tuples: (gray_image, color_image)
def _get_corners(img, board, refine = True, checkerboard_flags=0): """ Get corners for a particular chessboard for an image """ h = img.shape[0] w = img.shape[1] if len(img.shape) == 3 and img.shape[2] == 3: mono = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) else: mono = img (ok, corners) = cv2.findChessboardCorners(mono, (board.n_cols, board.n_rows), flags = cv2.CALIB_CB_ADAPTIVE_THRESH | cv2.CALIB_CB_NORMALIZE_IMAGE | checkerboard_flags) if not ok: return (ok, corners) # If any corners are within BORDER pixels of the screen edge, reject the detection by setting ok to false # NOTE: This may cause problems with very low-resolution cameras, where 8 pixels is a non-negligible fraction # of the image size. See http://answers.ros.org/question/3155/how-can-i-calibrate-low-resolution-cameras BORDER = 8 if not all([(BORDER < corners[i, 0, 0] < (w - BORDER)) and (BORDER < corners[i, 0, 1] < (h - BORDER)) for i in range(corners.shape[0])]): ok = False if refine and ok: # Use a radius of half the minimum distance between corners. This should be large enough to snap to the # correct corner, but not so large as to include a wrong corner in the search window. min_distance = float("inf") for row in range(board.n_rows): for col in range(board.n_cols - 1): index = row*board.n_rows + col min_distance = min(min_distance, _pdist(corners[index, 0], corners[index + 1, 0])) for row in range(board.n_rows - 1): for col in range(board.n_cols): index = row*board.n_rows + col min_distance = min(min_distance, _pdist(corners[index, 0], corners[index + board.n_cols, 0])) radius = int(math.ceil(min_distance * 0.5)) cv2.cornerSubPix(mono, corners, (radius,radius), (-1,-1), ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1 )) return (ok, corners)
def _findChessboard(self): # Find the chess board corners flags = cv2.CALIB_CB_FAST_CHECK if self._detect_sensible: flags = (cv2.CALIB_CB_FAST_CHECK | cv2.CALIB_CB_ADAPTIVE_THRESH | cv2.CALIB_CB_FILTER_QUADS | cv2.CALIB_CB_NORMALIZE_IMAGE) (didFindCorners, corners) = cv2.findChessboardCorners( self.img, self.opts['size'], flags=flags ) if didFindCorners: # further refine corners, corners is updatd in place cv2.cornerSubPix(self.img, corners, (11, 11), (-1, -1), # termination criteria for corner estimation for # chessboard method (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) ) # returns None return didFindCorners, corners
def k(screen): Z = screen.reshape((-1,3)) # convert to np.float32 Z = np.float32(Z) # define criteria, number of clusters(K) and apply kmeans() criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) K = 2 ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS) # Now convert back into uint8, and make original image center = np.uint8(center) res = center[label.flatten()] res2 = res.reshape((screen.shape)) return res2
def color_quant(input,K,output): img = cv2.imread(input) Z = img.reshape((-1,3)) # convert to np.float32 Z = np.float32(Z) # define criteria, number of clusters(K) and apply kmeans() criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 15, 1.0) ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS) # Now convert back into uint8, and make original image center = np.uint8(center) res = center[label.flatten()] res2 = res.reshape((img.shape)) cv2.imshow('res2',res2) cv2.waitKey(0) cv2.imwrite(output, res2) cv2.destroyAllWindows()
def draw_chessboard_corners(image): # Find the chess board corners gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) ret, corners = cv2.findChessboardCorners(gray_image, (9, 6), None) # Draw image if ret is True: criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) corners2 = cv2.cornerSubPix(gray_image, corners, (11, 11), (-1, -1), criteria) img = cv2.drawChessboardCorners(image, (9, 6), corners2, ret) return img
def calculateCorners(self, gray, points=None): ''' gray is OpenCV gray image, points is Marker.points >>> marker.calculateCorners(gray) >>> print(marker.corners) ''' if points is None: points = self.points if points is None: raise TypeError('calculateCorners need a points value') ''' rotations = 0 -> 0,1,2,3 rotations = 1 -> 3,0,1,2 rotations = 2 -> 2,3,0,1 rotations = 3 -> 1,2,3,0 => A: 1,0,3,2; B: 0,3,2,1; C: 2,1,0,3; D: 3,2,1,0 ''' i = self.rotations A = (1,0,3,2)[i]; B = (0,3,2,1)[i]; C = (2,1,0,3)[i]; D = (3,2,1,0)[i] corners = np.float32([points[A], points[B], points[C], points[D]]) criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1) self.corners = cv2.cornerSubPix(gray, corners, (5,5), (-1,-1), criteria)
def k_means(self, a_frame, K=2): """ :param a_frame: :param K: :return: np.ndarray draw the frame use K color's centers """ i = 0 Z = a_frame.reshape((-1, 1)) Z = np.float32(Z) criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) ret, label, center = cv2.kmeans(Z, K, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) center = np.uint8(center) res = center[label.flatten()] res2 = res.reshape((a_frame.shape)) return res2
def cluster(frame_matrix): new_frame_matrix = [] i = 0 for frame in frame_matrix: print "reader {} frame".format(i) i += 1 Z = frame.reshape((-1, 1)) Z = np.float32(Z) criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) K = 2 ret, label, center = cv2.kmeans(Z, K, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) center = np.uint8(center) res = center[label.flatten()] res2 = res.reshape((frame.shape)) new_frame_matrix.append(res2) cv2.imshow('res2', res2) cv2.waitKey(1) cv2.destroyAllWindows()
def gen_codebook(dataset, descriptors, k = 64): """ Generate a k codebook for the dataset. Args: dataset (Dataset object): An object that stores information about the dataset. descriptors (list of integer arrays): The descriptors for every class. k (integer): The number of clusters that are going to be calculated. Returns: list of integer arrays: The k codewords for the dataset. """ iterations = 10 epsilon = 1.0 criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, iterations, epsilon) compactness, labels, centers = cv2.kmeans(descriptors, k , criteria, iterations, cv2.KMEANS_RANDOM_CENTERS) return centers
def get_vectors(image, points, mtx, dist): # order points points = _order_points(points) # set up criteria, image, points and axis criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) imgp = np.array(points, dtype='float32') objp = np.array([[0.,0.,0.],[1.,0.,0.], [1.,1.,0.],[0.,1.,0.]], dtype='float32') # calculate rotation and translation vectors cv2.cornerSubPix(gray,imgp,(11,11),(-1,-1),criteria) rvecs, tvecs, _ = cv2.solvePnPRansac(objp, imgp, mtx, dist) return rvecs, tvecs
def cal_fromcorners(self, good): # Perform monocular calibrations lcorners = [(l, b) for (l, r, b) in good] rcorners = [(r, b) for (l, r, b) in good] self.l.cal_fromcorners(lcorners) self.r.cal_fromcorners(rcorners) lipts = [ l for (l, _, _) in good ] ripts = [ r for (_, r, _) in good ] boards = [ b for (_, _, b) in good ] opts = self.mk_object_points(boards, True) flags = cv2.CALIB_FIX_INTRINSIC self.T = numpy.zeros((3, 1), dtype=numpy.float64) self.R = numpy.eye(3, dtype=numpy.float64) if LooseVersion(cv2.__version__).version[0] == 2: cv2.stereoCalibrate(opts, lipts, ripts, self.size, self.l.intrinsics, self.l.distortion, self.r.intrinsics, self.r.distortion, self.R, # R self.T, # T criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 1, 1e-5), flags = flags) else: cv2.stereoCalibrate(opts, lipts, ripts, self.l.intrinsics, self.l.distortion, self.r.intrinsics, self.r.distortion, self.size, self.R, # R self.T, # T criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 1, 1e-5), flags = flags) self.set_alpha(0.0)
def test_kmeans(img): ## K???? z = img.reshape((-1, 3)) z = np.float32(z) criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) ret, label, center = cv2.kmeans(z, 20, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) center = np.uint8(center) res = center[label.flatten()] res2 = res.reshape((img.shape)) cv2.imshow('preview', res2) cv2.waitKey()
def find_label_clusters(kitti_base, kittiLabels, shape, num_clusters, descriptors=None): if descriptors is None: progressbar = ProgressBar('Computing descriptors', max=len(kittiLabels)) descriptors = [] for label in kittiLabels: progressbar.next() img = getCroppedSampleFromLabel(kitti_base, label) # img = cv2.resize(img, (shape[1], shape[0]), interpolation=cv2.INTER_AREA) img = resizeSample(img, shape, label) hist = get_hog(img) descriptors.append(hist) progressbar.finish() else: print 'find_label_clusters,', 'Using supplied descriptors.' print len(kittiLabels), len(descriptors) assert(len(kittiLabels) == len(descriptors)) # X = np.random.randint(25,50,(25,2)) # Y = np.random.randint(60,85,(25,2)) # Z = np.vstack((X,Y)) # convert to np.float32 Z = np.float32(descriptors) # define criteria and apply kmeans() K = num_clusters print 'find_label_clusters,', 'kmeans:', K criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) attempts = 10 ret,label,center=cv2.kmeans(Z,K,None,criteria,attempts,cv2.KMEANS_RANDOM_CENTERS) # ret,label,center=cv2.kmeans(Z,2,criteria,attempts,cv2.KMEANS_PP_CENTERS) print 'ret:', ret # print 'label:', label # print 'center:', center # # Now separate the data, Note the flatten() # A = Z[label.ravel()==0] # B = Z[label.ravel()==1] clusters = partition(kittiLabels, label) return clusters # # Plot the data # from matplotlib import pyplot as plt # plt.scatter(A[:,0],A[:,1]) # plt.scatter(B[:,0],B[:,1],c = 'r') # plt.scatter(center[:,0],center[:,1],s = 80,c = 'y', marker = 's') # plt.xlabel('Height'),plt.ylabel('Weight') # plt.show()
def find_sample_clusters(pos_reg_generator, window_dims, hog, num_clusters): regions = list(pos_reg_generator) descriptors = trainhog.compute_hog_descriptors(hog, regions, window_dims, 1) # convert to np.float32 descriptors = [rd.descriptor for rd in descriptors] Z = np.float32(descriptors) # define criteria and apply kmeans() K = num_clusters print 'find_label_clusters,', 'kmeans:', K criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) attempts = 10 ret,label,center=cv2.kmeans(Z,K,None,criteria,attempts,cv2.KMEANS_RANDOM_CENTERS) # ret,label,center=cv2.kmeans(Z,2,criteria,attempts,cv2.KMEANS_PP_CENTERS) print 'ret:', ret # print 'label:', label # print 'center:', center # # Now separate the data, Note the flatten() # A = Z[label.ravel()==0] # B = Z[label.ravel()==1] clusters = partition(regions, label) return clusters
def train_svm(svm_save_path, descriptors, labels): # train_data = convert_to_ml(descriptors) train_data = np.array(descriptors) responses = np.array(labels, dtype=np.int32) print "Start training..." svm = cv2.ml.SVM_create() # Default values to train SVM svm.setCoef0(0.0) svm.setDegree(3) # svm.setTermCriteria(TermCriteria(cv2.TERMCRIT_ITER + cv2.TERMCRIT_EPS, 1000, 1e-3)) svm.setTermCriteria((cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS, 1000, 1e-3)) svm.setGamma(0) svm.setKernel(cv2.ml.SVM_LINEAR) svm.setNu(0.5) svm.setP(0.1) # for EPSILON_SVR, epsilon in loss function? svm.setC(0.01) # From paper, soft classifier svm.setType(cv2.ml.SVM_EPS_SVR) # C_SVC; # EPSILON_SVR; # may be also NU_SVR; # do regression task svm.train(train_data, cv2.ml.ROW_SAMPLE, responses) print "...[done]" svm.save(svm_save_path) # def test_classifier(svm_file_path, window_dims): # # Set the trained svm to my_hog # hog_detector = get_svm_detector(svm_file_path) # hog = get_hog_object(window_dims) # hog.setSVMDetector(hog_detector) # # locations = hog.detectMultiScale(img)
def createTrainingInstances(self, images): instances = [] img_descriptors = [] master_descriptors = [] cv2.ocl.setUseOpenCL(False) orb = cv2.ORB_create() for img, label in images: print img img = read_color_image(img) keypoints = orb.detect(img, None) keypoints, descriptors = orb.compute(img, keypoints) if descriptors is None: descriptors = [] img_descriptors.append(descriptors) for i in descriptors: master_descriptors.append(i) master_descriptors = np.float32(master_descriptors) criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) ret, labels, centers = cv2.kmeans(master_descriptors, self.center_num, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) labels = labels.ravel() count = 0 img_num = 0 for img, label in images: histogram = np.zeros(self.center_num) feature_vector = img_descriptors[img_num] for f in xrange(len(feature_vector)): index = count + f histogram.itemset(labels[index], 1 + histogram.item(labels[index])) count += len(feature_vector) pairing = Instance(histogram, label) instances.append(pairing) self.training_instances = instances self.centers = centers
def local_bow_train(image): instances = [] img_descriptors = [] master_descriptors = [] cv2.ocl.setUseOpenCL(False) orb = cv2.ORB_create() for img, label in images: print img img = read_color_image(img) keypoints = orb.detect(img, None) keypoints, descriptors = orb.compute(img, keypoints) if descriptors is None: descriptors = [] img_descriptors.append(descriptors) for i in descriptors: master_descriptors.append(i) master_descriptors = np.float32(master_descriptors) criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) ret, labels, centers = cv2.kmeans(master_descriptors, self.center_num, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) labels = labels.ravel() count = 0 img_num = 0 for img, label in images: histogram = np.zeros(self.center_num) feature_vector = img_descriptors[img_num] for f in xrange(len(feature_vector)): index = count + f histogram.itemset(labels[index], 1 + histogram.item(labels[index])) count += len(feature_vector) pairing = Instance(histogram, label) instances.append(pairing) self.training_instances = instances self.centers = centers
def downsample_and_detect(self, img): """ Downsample the input image to approximately VGA resolution and detect the calibration target corners in the full-size image. Combines these apparently orthogonal duties as an optimization. Checkerboard detection is too expensive on large images, so it's better to do detection on the smaller display image and scale the corners back up to the correct size. Returns (scrib, corners, downsampled_corners, board, (x_scale, y_scale)). """ # Scale the input image down to ~VGA size height = img.shape[0] width = img.shape[1] scale = math.sqrt( (width*height) / (640.*480.) ) if scale > 1.0: scrib = cv2.resize(img, (int(width / scale), int(height / scale))) else: scrib = img # Due to rounding, actual horizontal/vertical scaling may differ slightly x_scale = float(width) / scrib.shape[1] y_scale = float(height) / scrib.shape[0] if self.pattern == Patterns.Chessboard: # Detect checkerboard (ok, downsampled_corners, board) = self.get_corners(scrib, refine = True) # Scale corners back to full size image corners = None if ok: if scale > 1.0: # Refine up-scaled corners in the original full-res image # TODO Does this really make a difference in practice? corners_unrefined = downsampled_corners.copy() corners_unrefined[:, :, 0] *= x_scale corners_unrefined[:, :, 1] *= y_scale radius = int(math.ceil(scale)) if len(img.shape) == 3 and img.shape[2] == 3: mono = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) else: mono = img cv2.cornerSubPix(mono, corners_unrefined, (radius,radius), (-1,-1), ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.1 )) corners = corners_unrefined else: corners = downsampled_corners else: # Circle grid detection is fast even on large images (ok, corners, board) = self.get_corners(img) # Scale corners to downsampled image for display downsampled_corners = None if ok: if scale > 1.0: downsampled_corners = corners.copy() downsampled_corners[:,:,0] /= x_scale downsampled_corners[:,:,1] /= y_scale else: downsampled_corners = corners return (scrib, corners, downsampled_corners, board, (x_scale, y_scale))
def getP(self, dst): """ dst: ?????? return self.MTX,self.DIST,self.RVEC,self.TVEC: ?? ????????????????? """ if self.SceneImage is None: return None corners = np.float32([dst[1], dst[0], dst[2], dst[3]]) gray = cv2.cvtColor(self.SceneImage, cv2.COLOR_BGR2GRAY) # termination criteria criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) # prepare object points, like (0,0,0), (1,0,0), (1,0,0), (1,1,0) objp = np.zeros((2*2,3), np.float32) objp[:,:2] = np.mgrid[0:2,0:2].T.reshape(-1,2) corners2 = cv2.cornerSubPix(gray,corners,(11,11),(-1,-1),criteria) if self.PTimes < self.PCount or self.PCount == 0: # Arrays to store object points and image points from all the images. objpoints = self.OBJPoints # 3d point in real world space imgpoints = self.IMGPoints # 2d points in image plane. if len(imgpoints) == 0 or np.sum(np.abs(imgpoints[-1] - corners2)) != 0: objpoints.append(objp) imgpoints.append(corners2) # Find mtx, dist, rvecs, tvecs ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1],None,None) if not ret: self.PTimes += 1 return None self.OBJPoints = objpoints self.IMGPoints = imgpoints self.MTX = mtx self.DIST = dist self.RVEC = rvecs[0] self.TVEC = tvecs[0] else: # Find the rotation and translation vectors. _, rvec, tvec, _= cv2.solvePnPRansac(objp, corners2, self.MTX, self.DIST) self.RVEC = rvec self.TVEC = tvec self.PTimes += 1 return self.MTX,self.DIST,self.RVEC,self.TVEC
def calibrate_intrinsics(camera, image_points, object_points, use_rational_model=True, use_tangential=False, use_thin_prism=False, fix_radial=False, fix_thin_prism=False, max_iterations=30, use_existing_guess=False, test=False): flags = 0 if test: flags = flags | cv2.CALIB_USE_INTRINSIC_GUESS # fix everything flags = flags | cv2.CALIB_FIX_PRINCIPAL_POINT flags = flags | cv2.CALIB_FIX_ASPECT_RATIO flags = flags | cv2.CALIB_FIX_FOCAL_LENGTH # apparently, we can't fix the tangential distance. What the hell? Zero it out. flags = flags | cv2.CALIB_ZERO_TANGENT_DIST flags = fix_radial_flags(flags) flags = flags | cv2.CALIB_FIX_S1_S2_S3_S4 criteria = (cv2.TERM_CRITERIA_MAX_ITER, 1, 0) else: if fix_radial: flags = fix_radial_flags(flags) if fix_thin_prism: flags = flags | cv2.CALIB_FIX_S1_S2_S3_S4 criteria = (cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS, max_iterations, 2.2204460492503131e-16) if use_existing_guess: flags = flags | cv2.CALIB_USE_INTRINSIC_GUESS if not use_tangential: flags = flags | cv2.CALIB_ZERO_TANGENT_DIST if use_rational_model: flags = flags | cv2.CALIB_RATIONAL_MODEL if len(camera.intrinsics.distortion_coeffs) < 8: camera.intrinsics.distortion_coeffs.resize((8,)) if use_thin_prism: flags = flags | cv2.CALIB_THIN_PRISM_MODEL if len(camera.intrinsics.distortion_coeffs) != 12: camera.intrinsics.distortion_coeffs = np.resize(camera.intrinsics.distortion_coeffs, (12,)) return __calibrate_intrinsics(camera, image_points, object_points, flags, criteria)
def camera_cal(self, image): # termination criteria criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001) nx = 8 ny = 6 dst = np.copy(image) # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0) objp = np.zeros((ny * nx, 3), np.float32) objp[:,:2] = np.mgrid[0:nx, 0:ny].T.reshape(-1,2) # Arrays to store object points and image points from all the images. objpoints = [] # 3d points in real world space imgpoints = [] # 2d points in image plane. # Search for chessboard corners grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #ret_thresh, mask = cv2.threshold(grey, 30, 255, cv2.THRESH_BINARY) ret, corners = cv2.findChessboardCorners(image, (nx, ny), None) #flags=(cv2.cv.CV_CALIB_CB_ADAPTIVE_THRESH + cv2.cv.CV_CALIB_CB_FILTER_QUADS)) # If found, add object points, image points if ret == True: objpoints.append(objp) cv2.cornerSubPix(grey,corners, (11,11), (-1,-1), criteria) imgpoints.append(corners) self.calibrated = True print ("FOUND!") #Draw and display the corners cv2.drawChessboardCorners(image, (nx, ny), corners, ret) # Do camera calibration given object points and image points ret, self.mtx, self.dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, grey.shape[::-1], None, None) # Save the camera calibration result for later use (we won't worry about rvecs / tvecs) dist_pickle = {} dist_pickle["mtx"] = self.mtx dist_pickle["dist"] = self.dist dist_pickle['objpoints'] = objpoints dist_pickle['imgpoints'] = imgpoints pickle.dump( dist_pickle, open( "/home/wil/ros/catkin_ws/src/av_sim/computer_vision/camera_calibration/data/camera_cal_pickle.p", "wb" ) ) #else: #print("Searching...") return image
def find_chessboard(self, sx=6, sy=9): """Finds the corners of an sx X sy chessboard in the image. Parameters ---------- sx : int Number of chessboard corners in x-direction. sy : int Number of chessboard corners in y-direction. Returns ------- :obj:`list` of :obj:`numpy.ndarray` A list containing the 2D points of the corners of the detected chessboard, or None if no chessboard found. """ # termination criteria 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((sx * sy, 3), np.float32) objp[:, :2] = np.mgrid[0:sx, 0:sy].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. # create images img = self.data.astype(np.uint8) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Find the chess board corners ret, corners = cv2.findChessboardCorners(gray, (sx, sy), None) # If found, add object points, image points (after refining them) if ret: objpoints.append(objp) cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria) imgpoints.append(corners) if corners is not None: return corners.squeeze() return None