當前位置: 首頁>>代碼示例>>Python>>正文


Python cv2.getOptimalNewCameraMatrix方法代碼示例

本文整理匯總了Python中cv2.getOptimalNewCameraMatrix方法的典型用法代碼示例。如果您正苦於以下問題:Python cv2.getOptimalNewCameraMatrix方法的具體用法?Python cv2.getOptimalNewCameraMatrix怎麽用?Python cv2.getOptimalNewCameraMatrix使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在cv2的用法示例。


在下文中一共展示了cv2.getOptimalNewCameraMatrix方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: live_undistort

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import getOptimalNewCameraMatrix [as 別名]
def live_undistort(camera, camera_matrix, distortion_coefficients):
    """ Using a given calibration matrix, display the distorted, undistorted, and cropped frame"""
    scaled_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(
        camera_matrix, distortion_coefficients, camera.size, 1, camera.size
    )
    while True:
        ret, frame = camera.cap.read()
        assert ret
        distorted_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        undistorted_frame = cv2.undistort(
            distorted_frame, camera_matrix, distortion_coefficients, None, scaled_camera_matrix,
        )
        roi_x, roi_y, roi_w, roi_h = roi
        cropped_frame = undistorted_frame[roi_y : roi_y + roi_h, roi_x : roi_x + roi_w]
        cv2.imshow("distorted %s" % (distorted_frame.shape,), distorted_frame)
        cv2.imshow("undistorted %s" % (undistorted_frame.shape,), undistorted_frame)
        cv2.imshow("cropped %s" % (cropped_frame.shape,), cropped_frame)
        cv2.waitKey(10) 
開發者ID:notkarol,項目名稱:derplearning,代碼行數:20,代碼來源:calibrate_camera.py

示例2: _undistort_image

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import getOptimalNewCameraMatrix [as 別名]
def _undistort_image(self, image):
        if self._camera_matrix is None or self._distortion_coefficients is None:
            import warnings
            warnings.warn("Undistortion has no effect because <camera_matrix>/<distortion_coefficients> is None!")
            return image

        h, w = image.shape[:2]
        new_camera_matrix, roi = cv2.getOptimalNewCameraMatrix(self._camera_matrix,
                                                               self._distortion_coefficients, (w, h),
                                                               1,
                                                               (w, h))
        undistorted = cv2.undistort(image, self._camera_matrix, self._distortion_coefficients, None,
                                    new_camera_matrix)
        return undistorted 
開發者ID:gaborvecsei,項目名稱:Color-Tracker,代碼行數:16,代碼來源:base_camera.py

示例3: main

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import getOptimalNewCameraMatrix [as 別名]
def main():

  basler_undistorted = pickle.load(open("basler_pickle.p", "rb"))

  #objects = []
  #with (open("./basler_pickle.p", "rb")) as openfile:
  #    while True:
  #        try:
  #            basler_undistorted.append(pickle.load(openfile))
  #        except EOFError:
  #            break
    
  image_width  = 1280
  image_height = 1024

  #print(basler_undistorted)

  cam_mtx = basler_undistorted["mtx"]
  cam_dist = basler_undistorted["dist"]
  rvecs = basler_undistorted["rvecs"]
  tvecs = basler_undistorted["tvecs"]
  #imageSize = image_height * image_width
  imageSize = ( image_height, image_width )
  
  #getOptimal...Mtx(cameraMatrix, distCoeffs, imageSize, alpha[, newImgSize[, centerPrincipalPoint]]) -> retval, validPixROI
  # Doesn't take Rect of validPixROI, contrary to the cpp method
  new_cam_mtx, valid_roi = cv.getOptimalNewCameraMatrix(cam_mtx, cam_dist, imageSize, 1, imageSize, 1) 
  
  # getOptimalNewCameraMatrix() possibly not working like in cpp
  #map1, map2 = cv.initUndistortRectifyMap(cam_mtx, cam_dist, np.eye(3), new_cam_mtx, imageSize, cv.CV_16SC2);
  map1, map2 = cv.initUndistortRectifyMap(cam_mtx, cam_dist, np.eye(3), cam_mtx, imageSize, cv.CV_16SC2);
  
  # map1 and map2 can be used together with cv.remap() for efficient real-time undistortion
  # Only need to be calculated once.
  maps = { "map1": map1, "map2": map2 }
  pickle.dump( maps, open("maps.p", "wb")) 
開發者ID:ddavid,項目名稱:fsoco,代碼行數:38,代碼來源:get-undistortion-maps.py

示例4: undistort

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import getOptimalNewCameraMatrix [as 別名]
def undistort(img, dist, mtx): #undistorstion routine
	h,  w = img.shape[:2]
	newcameramtx, roi=cv2.getOptimalNewCameraMatrix(mtx,dist,(w,h),1,(w,h))
	dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
	x,y,w,h = roi
	dst = dst[y:y+h, x:x+w]
	return dst

#testing undistort function 
開發者ID:karanchawla,項目名稱:Monocular-Visual-Inertial-Odometry,代碼行數:11,代碼來源:vo.py

示例5: get_view_matrix

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import getOptimalNewCameraMatrix [as 別名]
def get_view_matrix(self, alpha):
        """
        Returns camera matrix for handling image and coordinates distortion and undistortion. Based on alpha,
        up to all pixels of the distorted image can be visible in the undistorted image.

        :param alpha: Free scaling parameter between 0 (when all the pixels in the undistorted image are valid) and 1
                      (when all the source image pixels are retained in the undistorted image). For convenience for -1
                      returns custom camera matrix self.Kundistortion and None returns self.K.
        :type alpha: float or None
        :return: camera matrix for a view defined by alpha
        :rtype: array, shape=(3, 3)
        """
        if alpha == -1:
            Kundistortion = self.Kundistortion
        elif alpha is None:
            Kundistortion = self.K
        elif self.calibration_type == 'opencv':
            Kundistortion, _ = cv2.getOptimalNewCameraMatrix(self.K, self.opencv_dist_coeff, tuple(self.size_px), alpha)
        elif self.calibration_type == 'opencv_fisheye':
            Kundistortion = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(self.K, self.opencv_dist_coeff,
                                                                                   tuple(self.size_px), self.R,
                                                                                   balance=alpha)
        else:
            # TODO
            assert False, 'not implemented'
        return Kundistortion 
開發者ID:smidm,項目名稱:camera.py,代碼行數:28,代碼來源:camera.py

示例6: apply

# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import getOptimalNewCameraMatrix [as 別名]
def apply(self, img, crop = True):
		h,  w = img.shape[:2]
		newcameramtx, roi=cv2.getOptimalNewCameraMatrix(self.mtx, self.dist,(w,h),1,(w,h))

		# undistort
		dst = cv2.undistort(img, self.mtx, self.dist, None, newcameramtx)

		# crop the image
		if crop is not True:
			return dst

		x,y,w,h = roi
		dst = dst[y:y+h, x:x+w]

		return dst 
開發者ID:fatcloud,項目名稱:PyCV-time,代碼行數:17,代碼來源:undistort.py


注:本文中的cv2.getOptimalNewCameraMatrix方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。