Python cv2.initUndistortRectifyMap() Examples

The following are 13 code examples of cv2.initUndistortRectifyMap(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cv2 , or try the search function .
Example #1
Source File: utils.py    From cvcalib with Apache License 2.0 7 votes vote down vote up
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 
Example #2
Source File: test-mvs.py    From dfc2019 with MIT License 6 votes vote down vote up
def rectify_images_float(img1, x1, img2, x2, K, d, F, shearing=False):
    imsize = (img1.shape[1], img1.shape[0])
    H1, H2, rms, max_error = epipolar.rectify_uncalibrated(x1, x2, F, imsize)
    if shearing:
        S = epipolar.rectify_shearing(H1, H2, imsize)
        H1 = S.dot(H1)
    rH = la.inv(K).dot(H1).dot(K)
    lH = la.inv(K).dot(H2).dot(K)
    map1x, map1y = cv2.initUndistortRectifyMap(K, d, rH, K, imsize, cv.CV_16SC2)
    map2x, map2y = cv2.initUndistortRectifyMap(K, d, lH, K, imsize, cv.CV_16SC2)

    rimg1 = cv2.remap(img1, map1x, map1y,
                      interpolation=cv.INTER_NEAREST,
                      borderMode=cv2.BORDER_CONSTANT,
                      borderValue=(0, 0, 0, 0))
    rimg2 = cv2.remap(img2, map2x, map2y,
                      interpolation=cv.INTER_NEAREST,
                      borderMode=cv2.BORDER_CONSTANT,
                      borderValue=(0, 0, 0, 0))

    return rimg1, rimg2


# get NITF metadata that we embedded in the GeoTIFF header 
Example #3
Source File: functional.py    From albumentations with MIT License 6 votes vote down vote up
def optical_distortion(
    img, k=0, dx=0, dy=0, interpolation=cv2.INTER_LINEAR, border_mode=cv2.BORDER_REFLECT_101, value=None
):
    """Barrel / pincushion distortion. Unconventional augment.

    Reference:
        |  https://stackoverflow.com/questions/6199636/formulas-for-barrel-pincushion-distortion
        |  https://stackoverflow.com/questions/10364201/image-transformation-in-opencv
        |  https://stackoverflow.com/questions/2477774/correcting-fisheye-distortion-programmatically
        |  http://www.coldvision.io/2017/03/02/advanced-lane-finding-using-opencv/
    """
    height, width = img.shape[:2]

    fx = width
    fy = height

    cx = width * 0.5 + dx
    cy = height * 0.5 + dy

    camera_matrix = np.array([[fx, 0, cx], [0, fy, cy], [0, 0, 1]], dtype=np.float32)

    distortion = np.array([k, k, 0, 0, 0], dtype=np.float32)
    map1, map2 = cv2.initUndistortRectifyMap(camera_matrix, distortion, None, None, (width, height), cv2.CV_32FC1)
    img = cv2.remap(img, map1, map2, interpolation=interpolation, borderMode=border_mode, borderValue=value)
    return img 
Example #4
Source File: dataset.py    From stereo_ptam with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, 
            width, height,
            intrinsic_matrix, 
            undistort_rectify=False,
            extrinsic_matrix=None,
            distortion_coeffs=None,
            rectification_matrix=None,
            projection_matrix=None):

        self.width = width
        self.height = height
        self.intrinsic_matrix = intrinsic_matrix
        self.extrinsic_matrix = extrinsic_matrix
        self.distortion_coeffs = distortion_coeffs
        self.rectification_matrix = rectification_matrix
        self.projection_matrix = projection_matrix
        self.undistort_rectify = undistort_rectify
        self.fx = intrinsic_matrix[0, 0]
        self.fy = intrinsic_matrix[1, 1]
        self.cx = intrinsic_matrix[0, 2]
        self.cy = intrinsic_matrix[1, 2]

        if undistort_rectify:
            self.remap = cv2.initUndistortRectifyMap(
                cameraMatrix=self.intrinsic_matrix,
                distCoeffs=self.distortion_coeffs,
                R=self.rectification_matrix,
                newCameraMatrix=self.projection_matrix,
                size=(width, height),
                m1type=cv2.CV_8U)
        else:
            self.remap = None 
Example #5
Source File: get-undistortion-maps.py    From fsoco with Apache License 2.0 5 votes vote down vote up
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")) 
Example #6
Source File: transforms.py    From kaggle_carvana_segmentation with MIT License 5 votes vote down vote up
def __call__(self, img, mask=None):
        if random.random() < self.prob:
            height, width, channel = img.shape

            if 0:
                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(-self.distort_limit, self.distort_limit) * 0.00001
            dx = random.uniform(-self.shift_limit, self.shift_limit) * width
            dy = random.uniform(-self.shift_limit, self.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)
            if mask is not None:
                mask = cv2.remap(mask, map_x, map_y, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT_101)
        return img, mask 
Example #7
Source File: blob_detect.py    From pc-drone with MIT License 5 votes vote down vote up
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 
Example #8
Source File: test-mvs.py    From dfc2019 with MIT License 5 votes vote down vote up
def rectify_images_rgb(img1, x1, img2, x2, K, d, F, shearing=False):
    imsize = (img1.shape[1], img1.shape[0])
    H1, H2, rms, max_error = epipolar.rectify_uncalibrated(x1, x2, F, imsize)
    if shearing:
        S = epipolar.rectify_shearing(H1, H2, imsize)
        H1 = S.dot(H1)
    rH = la.inv(K).dot(H1).dot(K)
    lH = la.inv(K).dot(H2).dot(K)

    # TODO: lRect or rRect for img1/img2 ??
    map1x, map1y = cv2.initUndistortRectifyMap(K, d, rH, K, imsize, cv.CV_16SC2)
    map2x, map2y = cv2.initUndistortRectifyMap(K, d, lH, K, imsize, cv.CV_16SC2)
    rimg1 = cv2.remap(img1, map1x, map1y,
                      interpolation=cv.INTER_CUBIC,
                      borderMode=cv2.BORDER_CONSTANT,
                      borderValue=(0, 0, 0))
    rimg2 = cv2.remap(img2, map2x, map2y,
                      interpolation=cv.INTER_CUBIC,
                      borderMode=cv2.BORDER_CONSTANT,
                      borderValue=(0, 0, 0))

    return rimg1, rimg2, rms, max_error


# rectify a floating point image pair based on the Fundamental matrix
# use this for XYZ images 
Example #9
Source File: functional.py    From dsb2018_topcoders with MIT License 5 votes vote down vote up
def distort1(img, k=0, dx=0, dy=0):
    """"
    ## unconverntional augmnet ################################################################################3
    ## https://stackoverflow.com/questions/6199636/formulas-for-barrel-pincushion-distortion

    ## https://stackoverflow.com/questions/10364201/image-transformation-in-opencv
    ## https://stackoverflow.com/questions/2477774/correcting-fisheye-distortion-programmatically
    ## http://www.coldvision.io/2017/03/02/advanced-lane-finding-using-opencv/

    ## barrel\pincushion distortion
    """
    height, width = img.shape[:2]
    #  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
    k = k * 0.00001
    dx = dx * width
    dy = dy * height
    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 
Example #10
Source File: functional.py    From dsb2018_topcoders with MIT License 5 votes vote down vote up
def distort1(img, k=0, dx=0, dy=0):
    """"
    ## unconverntional augmnet ################################################################################3
    ## https://stackoverflow.com/questions/6199636/formulas-for-barrel-pincushion-distortion

    ## https://stackoverflow.com/questions/10364201/image-transformation-in-opencv
    ## https://stackoverflow.com/questions/2477774/correcting-fisheye-distortion-programmatically
    ## http://www.coldvision.io/2017/03/02/advanced-lane-finding-using-opencv/

    ## barrel\pincushion distortion
    """
    height, width = img.shape[:2]
    #  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
    k = k * 0.00001
    dx = dx * width
    dy = dy * height
    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 
Example #11
Source File: functional.py    From dsb2018_topcoders with MIT License 5 votes vote down vote up
def distort1(img, k=0, dx=0, dy=0):
    """"
    ## unconverntional augmnet ################################################################################3
    ## https://stackoverflow.com/questions/6199636/formulas-for-barrel-pincushion-distortion

    ## https://stackoverflow.com/questions/10364201/image-transformation-in-opencv
    ## https://stackoverflow.com/questions/2477774/correcting-fisheye-distortion-programmatically
    ## http://www.coldvision.io/2017/03/02/advanced-lane-finding-using-opencv/

    ## barrel\pincushion distortion
    """
    height, width = img.shape[:2]
    #  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
    k = k * 0.00001
    dx = dx * width
    dy = dy * height
    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 
Example #12
Source File: test-mvs.py    From dfc2019 with MIT License 4 votes vote down vote up
def rectify_images(img1, x1, img2, x2, K, d, F, shearing=False):
    imsize = (img1.shape[1], img1.shape[0])
    H1, H2, rms, max_error = epipolar.rectify_uncalibrated(x1, x2, F, imsize)
    if shearing:
        S = epipolar.rectify_shearing(H1, H2, imsize)
        H1 = S.dot(H1)
    rH = la.inv(K).dot(H1).dot(K)
    lH = la.inv(K).dot(H2).dot(K)

    # check for y parallax
    max_yparallax = get_y_parallax(x1, x2, rH, lH, imsize)

    # TODO: lRect or rRect for img1/img2 ??
    map1x, map1y = cv2.initUndistortRectifyMap(K, d, rH, K, imsize,
                                               cv.CV_16SC2)
    map2x, map2y = cv2.initUndistortRectifyMap(K, d, lH, K, imsize,
                                               cv.CV_16SC2)

    # Convert the images to RGBA (add an axis with 4 values)
    img1 = np.tile(img1[:, :, np.newaxis], [1, 1, 4])
    img1[:, :, 3] = 255
    img2 = np.tile(img2[:, :, np.newaxis], [1, 1, 4])
    img2[:, :, 3] = 255

    rimg1 = cv2.remap(img1, map1x, map1y,
                      interpolation=cv.INTER_NEAREST,
                      borderMode=cv2.BORDER_CONSTANT,
                      borderValue=(0, 0, 0, 0))
    rimg2 = cv2.remap(img2, map2x, map2y,
                      interpolation=cv.INTER_NEAREST,
                      borderMode=cv2.BORDER_CONSTANT,
                      borderValue=(0, 0, 0, 0))

    # Put a red background on the invalid values
    # TODO: Return a mask for valid/invalid values
    # TODO: There is aliasing happening on the images border. We should
    # invalidate a margin around the border so we're sure we have only valid
    # pixels
    rimg1[rimg1[:, :, 3] == 0, :] = (255, 0, 0, 255)
    rimg2[rimg2[:, :, 3] == 0, :] = (255, 0, 0, 255)

    return rimg1, rimg2, rms, max_error, lH, rH, max_yparallax


# rectify an image pair based on the Fundamental matrix 
Example #13
Source File: calibration.py    From StereoVision with GNU General Public License v3.0 4 votes vote down vote up
def calibrate_cameras(self):
        """Calibrate cameras based on found chessboard corners."""
        criteria = (cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS,
                    100, 1e-5)
        flags = (cv2.CALIB_FIX_ASPECT_RATIO + cv2.CALIB_ZERO_TANGENT_DIST +
                 cv2.CALIB_SAME_FOCAL_LENGTH)
        calib = StereoCalibration()
        (calib.cam_mats["left"], calib.dist_coefs["left"],
         calib.cam_mats["right"], calib.dist_coefs["right"],
         calib.rot_mat, calib.trans_vec, calib.e_mat,
         calib.f_mat) = cv2.stereoCalibrate(self.object_points,
                                            self.image_points["left"],
                                            self.image_points["right"],
                                            self.image_size,
                                            calib.cam_mats["left"],
                                            calib.dist_coefs["left"],
                                            calib.cam_mats["right"],
                                            calib.dist_coefs["right"],
                                            calib.rot_mat,
                                            calib.trans_vec,
                                            calib.e_mat,
                                            calib.f_mat,
                                            criteria=criteria,
                                            flags=flags)[1:]
        (calib.rect_trans["left"], calib.rect_trans["right"],
         calib.proj_mats["left"], calib.proj_mats["right"],
         calib.disp_to_depth_mat, calib.valid_boxes["left"],
         calib.valid_boxes["right"]) = cv2.stereoRectify(calib.cam_mats["left"],
                                                      calib.dist_coefs["left"],
                                                      calib.cam_mats["right"],
                                                      calib.dist_coefs["right"],
                                                      self.image_size,
                                                      calib.rot_mat,
                                                      calib.trans_vec,
                                                      flags=0)
        for side in ("left", "right"):
            (calib.undistortion_map[side],
             calib.rectification_map[side]) = cv2.initUndistortRectifyMap(
                                                        calib.cam_mats[side],
                                                        calib.dist_coefs[side],
                                                        calib.rect_trans[side],
                                                        calib.proj_mats[side],
                                                        self.image_size,
                                                        cv2.CV_32FC1)
        # This is replaced because my results were always bad. Estimates are
        # taken from the OpenCV samples.
        width, height = self.image_size
        focal_length = 0.8 * width
        calib.disp_to_depth_mat = np.float32([[1, 0, 0, -0.5 * width],
                                              [0, -1, 0, 0.5 * height],
                                              [0, 0, 0, -focal_length],
                                              [0, 0, 1, 0]])
        return calib