我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用cv2.getOptimalNewCameraMatrix()。
def update(self,frame,events): if self.collect_new: img = frame.img status, grid_points = cv2.findCirclesGrid(img, (4,11), flags=cv2.CALIB_CB_ASYMMETRIC_GRID) if status: self.img_points.append(grid_points) self.obj_points.append(self.obj_grid) self.collect_new = False self.count -=1 self.button.status_text = "{:d} to go".format(self.count) if self.count<=0 and not self.calculated: self.calculate() self.button.status_text = '' if self.window_should_close: self.close_window() if self.show_undistortion: adjusted_k,roi = cv2.getOptimalNewCameraMatrix(cameraMatrix= self.camera_intrinsics[0], distCoeffs=self.camera_intrinsics[1], imageSize=self.camera_intrinsics[2], alpha=0.5,newImgSize=self.camera_intrinsics[2],centerPrincipalPoint=1) self.undist_img = cv2.undistort(frame.img, self.camera_intrinsics[0], self.camera_intrinsics[1],newCameraMatrix=adjusted_k)
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 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 undistort_image(im, K, D): """ Optionally: newcamera, roi = cv2.getOptimalNewCameraMatrix(self.K, self.D, (W,H), 0) """ H,W = im.shape[:2] Kprime, roi = cv2.getOptimalNewCameraMatrix(K, D, (W,H), 1, (W,H)) return cv2.undistort(im, K, D, None, K) # def camera_from_P(P):
def __init__(self, image_points, dest_points, size=(3840, 2160)): dest_3dpoints = [[x, y, 0] for x, y in dest_points] _, camera_matrix, dist_coeffs, _, _ = cv2.calibrateCamera( [np.float32([dest_3dpoints])], [np.float32([image_points])], size, None, None, flags=self.flags) self.image_size = size self.camera_matrix = camera_matrix self.dist_coeffs = dist_coeffs self.new_camera_matrix = cv2.getOptimalNewCameraMatrix( camera_matrix, dist_coeffs, self.image_size, 0)[0]
def remove_distortion(images): out = calibrate(images) matrix = out['camera_matrix'] dist = out['distortion_coefficient'] undistorted_images = [] for (image, color_image) in images: size = image.shape[::-1] new_matrix, roi = cv.getOptimalNewCameraMatrix(matrix, dist, size, 1, size) img = cv.undistort(color_image, matrix, dist, None, new_matrix) undistorted_images.append(img) return undistorted_images
def undistortPoints(self, points, keepSize=False): ''' points --> list of (x,y) coordinates ''' s = self.img.shape cam = self.coeffs['cameraMatrix'] d = self.coeffs['distortionCoeffs'] pts = np.asarray(points, dtype=np.float32) if pts.ndim == 2: pts = np.expand_dims(pts, axis=0) (newCameraMatrix, roi) = cv2.getOptimalNewCameraMatrix(cam, d, s[::-1], 1, s[::-1]) if not keepSize: xx, yy = roi[:2] pts[0, 0] -= xx pts[0, 1] -= yy return cv2.undistortPoints(pts, cam, d, P=newCameraMatrix)
def __init__(self, x, y, K, d, camid=0, visualize=False, debug=False): # Initialize multiprocessing.Process parent multiprocessing.Process.__init__(self) # Exit event for stopping process self._exit = multiprocessing.Event() # Event that is set, everytime an image has been unwarped self.newframe_event = multiprocessing.Event() # Event that pauses the main loop if set self._pause_event = multiprocessing.Event() # Defines whether to visualize the camera output self._visualize = visualize # Switches debugging mode self._debug = debug # Some variable for storing the time of the last frame self._oldtime = time.time() # Set camera parameters self._cam_device_id = camid # Get camera ID self._x = x # Get width self._y = y # Get height # An empty array in shared memory to store the current image frame self._currentframe = sharedmem.empty((y, x), dtype='uint8') # Define camera matrix K self._K = K # Define distortion coefficients d self._d = d # Setup camera object using OpenCV self._cam = cv2.VideoCapture(self._cam_device_id) self._cam.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, self._x) self._cam.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, self._y) # Generate optimal camera matrix self._newcameramatrix, self._roi = cv2.getOptimalNewCameraMatrix(self._K, self._d, (self._x, self._y), 0) # Generate LUTs for undistortion self._mapx, self._mapy = cv2.initUndistortRectifyMap(self._K, self._d, None, self._newcameramatrix, (self._x, self._y), 5)