我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用cv2.CV_32FC1。
def set_alpha(self, a): """ Set the alpha value for the calibrated camera solution. The alpha value is a zoom, and ranges from 0 (zoomed in, all pixels in calibrated image are valid) to 1 (zoomed out, all pixels in original image are in calibrated image). """ # NOTE: Prior to Electric, this code was broken such that we never actually saved the new # camera matrix. In effect, this enforced P = [K|0] for monocular cameras. # TODO: Verify that OpenCV #1199 gets applied (improved GetOptimalNewCameraMatrix) ncm, _ = cv2.getOptimalNewCameraMatrix(self.intrinsics, self.distortion, self.size, a) for j in range(3): for i in range(3): self.P[j,i] = ncm[j, i] self.mapx, self.mapy = cv2.initUndistortRectifyMap(self.intrinsics, self.distortion, self.R, ncm, self.size, cv2.CV_32FC1)
def set_alpha(self, a): """ Set the alpha value for the calibrated camera solution. The alpha value is a zoom, and ranges from 0 (zoomed in, all pixels in calibrated image are valid) to 1 (zoomed out, all pixels in original image are in calibrated image). """ cv2.stereoRectify(self.l.intrinsics, self.l.distortion, self.r.intrinsics, self.r.distortion, self.size, self.R, self.T, self.l.R, self.r.R, self.l.P, self.r.P, alpha = a) cv2.initUndistortRectifyMap(self.l.intrinsics, self.l.distortion, self.l.R, self.l.P, self.size, cv2.CV_32FC1, self.l.mapx, self.l.mapy) cv2.initUndistortRectifyMap(self.r.intrinsics, self.r.distortion, self.r.R, self.r.P, self.size, cv2.CV_32FC1, self.r.mapx, self.r.mapy)
def compute_stereo_rectification_maps(stereo_rig, im_size, size_factor): new_size = (int(im_size[1] * size_factor), int(im_size[0] * size_factor)) rotation1, rotation2, pose1, pose2 = \ cv2.stereoRectify(cameraMatrix1=stereo_rig.cameras[0].intrinsics.intrinsic_mat, distCoeffs1=stereo_rig.cameras[0].intrinsics.distortion_coeffs, cameraMatrix2=stereo_rig.cameras[1].intrinsics.intrinsic_mat, distCoeffs2=stereo_rig.cameras[1].intrinsics.distortion_coeffs, imageSize=(im_size[1], im_size[0]), R=stereo_rig.cameras[1].extrinsics.rotation, T=stereo_rig.cameras[1].extrinsics.translation, flags=cv2.CALIB_ZERO_DISPARITY, newImageSize=new_size )[0:4] map1x, map1y = cv2.initUndistortRectifyMap(stereo_rig.cameras[0].intrinsics.intrinsic_mat, stereo_rig.cameras[0].intrinsics.distortion_coeffs, rotation1, pose1, new_size, cv2.CV_32FC1) map2x, map2y = cv2.initUndistortRectifyMap(stereo_rig.cameras[1].intrinsics.intrinsic_mat, stereo_rig.cameras[1].intrinsics.distortion_coeffs, rotation2, pose2, new_size, cv2.CV_32FC1) return map1x, map1y, map2x, map2y
def getUndistortRectifyMap(self, imgWidth, imgHeight): if self.mapx is not None and self.mapx.shape == (imgHeight, imgWidth): return self.mapx, self.mapy cam = self.coeffs['cameraMatrix'] d = self.coeffs['distortionCoeffs'] (newCameraMatrix, self.roi) = cv2.getOptimalNewCameraMatrix(cam, d, (imgWidth, imgHeight), 1, (imgWidth, imgHeight)) self.mapx, self.mapy = cv2.initUndistortRectifyMap(cam, d, None, newCameraMatrix, (imgWidth, imgHeight), cv2.CV_32FC1) return self.mapx, self.mapy
def randomDistort1(img, distort_limit=0.35, shift_limit=0.25, u=0.5): if random.random() < u: height, width, channel = img.shape # debug # img = img.copy() # for x in range(0,width,10): # cv2.line(img,(x,0),(x,height),(1,1,1),1) # for y in range(0,height,10): # cv2.line(img,(0,y),(width,y),(1,1,1),1) k = random.uniform(-distort_limit, distort_limit) * 0.00001 dx = random.uniform(-shift_limit, shift_limit) * width dy = random.uniform(-shift_limit, shift_limit) * height # map_x, map_y = cv2.initUndistortRectifyMap(intrinsics, dist_coeffs, None, None, (width,height),cv2.CV_32FC1) # https://stackoverflow.com/questions/6199636/formulas-for-barrel-pincushion-distortion # https://stackoverflow.com/questions/10364201/image-transformation-in-opencv x, y = np.mgrid[0:width:1, 0:height:1] x = x.astype(np.float32) - width / 2 - dx y = y.astype(np.float32) - height / 2 - dy theta = np.arctan2(y, x) d = (x * x + y * y) ** 0.5 r = d * (1 + k * d * d) map_x = r * np.cos(theta) + width / 2 + dx map_y = r * np.sin(theta) + height / 2 + dy img = cv2.remap(img, map_x, map_y, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT_101) return img # http://pythology.blogspot.sg/2014/03/interpolation-on-regular-distorted-grid.html ## grid distortion
def __init__(self, left, right): self.cams = [left, right] self.undistortion_map = {} self.rectification_map = {} for cidx, cam in enumerate(self.cams): (self.undistortion_map[cidx], self.rectification_map[cidx]) = cv2.initUndistortRectifyMap( cam.K, cam.D, cam.R, cam.P, cam.shape[:2], cv2.CV_32FC1)
def __filter_candidate(greyscale_image, coord, neighborhood_size): window = greyscale_image[coord[0] - neighborhood_size:coord[0] + neighborhood_size + 1, coord[1] - neighborhood_size:coord[1] + neighborhood_size + 1] grad_x = cv2.Sobel(window, cv2.CV_32FC1, dx=1, dy=0, ksize=3) grad_y = cv2.Sobel(window, cv2.CV_32FC1, dx=0, dy=1, ksize=3) grad_mag = np.abs(grad_x) + np.abs(grad_y) grad_mag_flat = grad_mag.flatten() orientations_flat = (cv2.phase(grad_x, grad_y) % pi).flatten() # phase accuracy: about 0.3 degrees hist = (np.histogram(orientations_flat, bins=64, range=(0, pi), weights=grad_mag_flat)[0] / (neighborhood_size * neighborhood_size)) return hist, grad_mag
def process_frame(self): super().process_frame() if self.cur_frame_number == self.ground_truth_frame_numbers[self.gt_frame_ix]: # we have struck upon a frame we can evaluate against ground truth gt_file_path = os.path.join(self.ground_truth_folder, self.ground_truth_frame_filenames[self.gt_frame_ix]) gt_mask = cv2.imread(gt_file_path, cv2.IMREAD_GRAYSCALE) self.gt_frame_ix += 1 # advance for next hit test_mask = self.mask.copy() test_mask[test_mask < MaskLabel.PERSISTENCE_LABEL.value] = 0 test_mask[test_mask >= MaskLabel.PERSISTENCE_LABEL.value] = 1 gt_mask[gt_mask == 255] = 1 test_mask = test_mask.astype(np.int8) # to allow subtraction errors = test_mask - gt_mask false_positives = errors.copy() false_negatives = errors.copy() false_positives[false_positives == -1] = 0 false_negatives[false_negatives == 1] = 0 n_fp = false_positives.sum() n_fn = -false_negatives.sum() penalty_map = cv2.filter2D(gt_mask, cv2.CV_32FC1, self.smoothing_kernel) cv2.normalize(penalty_map, penalty_map, 0, 1.0, cv2.NORM_MINMAX) weighted_fn = (penalty_map[false_negatives == -1]).sum() penalty_map = penalty_map.max() - penalty_map # invert weighted_fp = (penalty_map[false_positives == 1]).sum() self.cum_fp += n_fp self.cum_fn += n_fn self.cum_wfn += weighted_fn self.cum_wfp += weighted_fp self.tested_frame_coutner += 1
def init_undistort(): #cv2.initUndistortRectifyMap(cameraMatrix, distCoeffs, R, newCameraMatrix, size, m1type[, map1[, map2]]) -> map1, map2 frame_size=(640,480) map1, map2=cv2.initUndistortRectifyMap(mtx, dist, None, newcameramtx, frame_size, cv2.CV_32FC1) return map1, map2 # this is a faster undistort_crop that only does remapping. Requires call to init_undistort first to # to create the map1 and map2