Python cv2 模块,initUndistortRectifyMap() 实例源码

我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用cv2.initUndistortRectifyMap()

项目:camera_calibration_frontend    作者:groundmelon    | 项目源码 | 文件源码
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)
项目:camera_calibration_frontend    作者:groundmelon    | 项目源码 | 文件源码
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)
项目:cvcalib    作者:Algomorph    | 项目源码 | 文件源码
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
项目:imgProcessor    作者:radjkarl    | 项目源码 | 文件源码
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
项目:kaggle_amazon_from_space    作者:N01Z3    | 项目源码 | 文件源码
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
项目:pybot    作者:spillai    | 项目源码 | 文件源码
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)
项目:Smart-Car    作者:jimchenhub    | 项目源码 | 文件源码
def rectify(mtx1, dist1, mtx2, dist2, R, T):
    # R????????????P?3*4????????Q?4*4??????
    R1, R2, P1, P2, Q, roi1, roi2 = cv2.stereoRectify(
        mtx1, dist1,
        mtx2, dist2,
        (BINIMG_W, BINIMG_H),
        R,
        T,
        flags=cv2.CALIB_ZERO_DISPARITY,
        alpha=-1,
        newImageSize=(BINIMG_W, BINIMG_H)
    )
    if __name__ == '__main__':
        printMat(R1, R2, P1, P2, Q, roi1, roi2)
    # ??????????????mapx, mapy?
    mapx1, mapy1 = cv2.initUndistortRectifyMap(
        mtx1, dist1,
        R1, P1,
        (BINIMG_W, BINIMG_H),
        cv2.CV_16SC2
    )
    mapx2, mapy2 = cv2.initUndistortRectifyMap(
        mtx2, dist2,
        R2, P2,
        (BINIMG_W, BINIMG_H),
        cv2.CV_16SC2
    )
    return mapx1, mapy1, mapx2, mapy2, Q, roi1, roi2
项目:pc-drone    作者:perrytsao    | 项目源码 | 文件源码
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
项目:AutoFan    作者:hgmeyer    | 项目源码 | 文件源码
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)