本文整理汇总了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)
示例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
示例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"))
示例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
示例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
示例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