当前位置: 首页>>代码示例>>Python>>正文


Python cv2.remap方法代码示例

本文整理汇总了Python中cv2.remap方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.remap方法的具体用法?Python cv2.remap怎么用?Python cv2.remap使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cv2的用法示例。


在下文中一共展示了cv2.remap方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _elastic

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import remap [as 别名]
def _elastic(image, p, alpha=None, sigma=None, random_state=None):
    """Elastic deformation of images as described in [Simard2003]_ (with modifications).
    .. [Simard2003] Simard, Steinkraus and Platt, "Best Practices for
         Convolutional Neural Networks applied to Visual Document Analysis", in
         Proc. of the International Conference on Document Analysis and
         Recognition, 2003.
     Based on https://gist.github.com/erniejunior/601cdf56d2b424757de5
     From: 
     https://www.kaggle.com/bguberfain/elastic-transform-for-data-augmentation
    """
    if random.random() > p:
        return image
    if alpha == None:
        alpha = image.shape[0] * random.uniform(0.5,2)
    if sigma == None:
        sigma = int(image.shape[0] * random.uniform(0.5,1))
    if random_state is None:
        random_state = np.random.RandomState(None)

    shape = image.shape[:2]
    
    dx, dy = [cv2.GaussianBlur((random_state.rand(*shape) * 2 - 1) * alpha, (sigma|1, sigma|1), 0) for _ in range(2)]
    x, y = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]))
    x, y = np.clip(x+dx, 0, shape[1]-1).astype(np.float32), np.clip(y+dy, 0, shape[0]-1).astype(np.float32)
    return cv2.remap(image, x, y, interpolation=cv2.INTER_LINEAR, borderValue= 0, borderMode=cv2.BORDER_REFLECT) 
开发者ID:ShuangXieIrene,项目名称:ssds.pytorch,代码行数:27,代码来源:data_augment.py

示例2: __init__

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import remap [as 别名]
def __init__(self, fin, scale=1.0, fmask=None):
            self.fin = fin
            # read in distort
            with open(fin, 'r') as f:
                header = f.readline().rstrip()
                chunks = re.sub(r'[^0-9,]', '', header).split(',')
                self.mapu = np.zeros((int(chunks[1]), int(chunks[0])),
                                     dtype=np.float32)
                self.mapv = np.zeros((int(chunks[1]), int(chunks[0])),
                                     dtype=np.float32)
                for line in f.readlines():
                    chunks = line.rstrip().split(' ')
                    self.mapu[int(chunks[0]), int(chunks[1])] = float(chunks[3])
                    self.mapv[int(chunks[0]), int(chunks[1])] = float(chunks[2])
            # generate a mask
            self.mask = np.ones(self.mapu.shape, dtype=np.uint8)
            self.mask = cv2.remap(self.mask, self.mapu, self.mapv, cv2.INTER_LINEAR)
            kernel = np.ones((30, 30), np.uint8)
            self.mask = cv2.erode(self.mask, kernel, iterations=1)
            # crop black regions out
            h, w = self.mask.shape
            self.x_lim = [f(np.where(self.mask[int(h/2), :])[0])
                          for f in [np.min, np.max]]
            self.y_lim = [f(np.where(self.mask[:, int(w/2)])[0])
                          for f in [np.min, np.max]] 
开发者ID:ethz-asl,项目名称:hierarchical_loc,代码行数:27,代码来源:nclt.py

示例3: warp

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import remap [as 别名]
def warp(self, img, order=1):
        """Warp image into new coordinate system.

        Parameters
        ----------
        img : np.ndarray
            Image to be warped. Any number of channels and dtype either uint8 or float32.

        order : int
            Interpolation order.
                * 0 - nearest neigbours
                * 1 - linear
                * 2 - cubic

        Returns
        -------
        warped_img : np.ndarray
            Warped image. The same number of channels and same dtype as the `img`.

        """
        tform_x, tform_y = self.transformation
        warped_img = cv2.remap(img, tform_x, tform_y, order)

        return warped_img 
开发者ID:jankrepl,项目名称:pychubby,代码行数:26,代码来源:base.py

示例4: spline_transform_multi

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import remap [as 别名]
def spline_transform_multi(img, mask):
    bimask=mask>0
    M,N=np.where(bimask)
    w=np.ptp(N)+1
    h=np.ptp(M)+1
    kernel=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
    bound=cv2.dilate(bimask.astype('uint8'),kernel)-bimask
    y,x=np.where(bound>0)

    if x.size>4:
        newxy=thin_plate_transform(x,y,w,h,mask.shape[:2],num_points=5)

        new_img=cv2.remap(img,newxy,None,cv2.INTER_LINEAR)
        new_msk=cv2.remap(mask,newxy,None,cv2.INTER_NEAREST)
    elif x.size>0:
        new_img=img
        new_msk=mask
    return new_img,new_msk 
开发者ID:yelantingfeng,项目名称:pyLucid,代码行数:20,代码来源:lucidDream.py

示例5: random_warp

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import remap [as 别名]
def random_warp( image ):
    assert image.shape == (256,256,3)
    range_ = numpy.linspace( 128-80, 128+80, 5 )
    mapx = numpy.broadcast_to( range_, (5,5) )
    mapy = mapx.T

    mapx = mapx + numpy.random.normal( size=(5,5), scale=5 )
    mapy = mapy + numpy.random.normal( size=(5,5), scale=5 )

    interp_mapx = cv2.resize( mapx, (80,80) )[8:72,8:72].astype('float32')
    interp_mapy = cv2.resize( mapy, (80,80) )[8:72,8:72].astype('float32')

    warped_image = cv2.remap( image, interp_mapx, interp_mapy, cv2.INTER_LINEAR )

    src_points = numpy.stack( [ mapx.ravel(), mapy.ravel() ], axis=-1 )
    dst_points = numpy.mgrid[0:65:16,0:65:16].T.reshape(-1,2)
    mat = umeyama( src_points, dst_points, True )[0:2]

    target_image = cv2.warpAffine( image, mat, (64,64) )

    return warped_image, target_image 
开发者ID:DerWaldi,项目名称:youtube-video-face-swap,代码行数:23,代码来源:image_augmentation.py

示例6: get_points_from_masks

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import remap [as 别名]
def get_points_from_masks(mask_t0, mask_t1, point_img_t0, point_img_t1, flow_t1_t0, img_t0, img_t1, calibration_params):
    #point_img_t0[np.logical_not(mask_t0)] = [0, 0, 0]

    h, w = flow_t1_t0.shape[:2]
    flow = -flow_t1_t0
    flow[:, :, 0] += np.arange(w)
    flow[:, :, 1] += np.arange(h)[:, np.newaxis]

    point_img_t0 = cv2.remap(point_img_t0, flow, None, cv2.INTER_NEAREST)

    mask_t0_warped = cv2.remap(mask_t0, flow, None, cv2.INTER_NEAREST)
    mask_t0_warped = np.equal(mask_t0_warped, 1).astype(np.uint8)

    mask_overlap = np.logical_and(mask_t0_warped.astype(np.bool), mask_t1.astype(np.bool))

    object_points = np.concatenate((np.expand_dims(point_img_t0[mask_overlap], axis=1), np.expand_dims(point_img_t1[mask_overlap], axis=1)), axis=1)
    colors = np.concatenate((np.expand_dims(img_t0[mask_overlap], axis=1), np.expand_dims(img_t1[mask_overlap], axis=1)), axis=1)

    return object_points, colors 
开发者ID:tobiasfshr,项目名称:MOTSFusion,代码行数:21,代码来源:reconstruction_utils.py

示例7: rectify_images_float

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import remap [as 别名]
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 
开发者ID:pubgeo,项目名称:dfc2019,代码行数:26,代码来源:test-mvs.py

示例8: _random_warp

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import remap [as 别名]
def _random_warp(self, batch):
        """ Randomly warp the input batch """
        logger.trace("Randomly warping batch")
        mapx = self._constants["warp_mapx"]
        mapy = self._constants["warp_mapy"]
        pad = self._constants["warp_pad"]
        slices = self._constants["warp_slices"]

        rands = np.random.normal(size=(self._batchsize, 2, 5, 5),
                                 scale=self._scale).astype("float32")
        batch_maps = np.stack((mapx, mapy), axis=1) + rands
        batch_interp = np.array([[cv2.resize(map_, (pad, pad))[slices, slices] for map_ in maps]
                                 for maps in batch_maps])
        warped_batch = np.array([cv2.remap(image, interp[0], interp[1], cv2.INTER_LINEAR)
                                 for image, interp in zip(batch, batch_interp)])

        logger.trace("Warped image shape: %s", warped_batch.shape)
        return warped_batch 
开发者ID:deepfakes,项目名称:faceswap,代码行数:20,代码来源:training_data.py

示例9: E2P

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import remap [as 别名]
def E2P(image, corner_i, corner_j, wall_height, camera, resolution=512, is_wall=True):
    """convert panorama to persepctive image
    """
    corner_i = corner_i - camera
    corner_j = corner_j - camera

    if is_wall:
        xs = np.linspace(corner_i[0], corner_j[0], resolution)[None].repeat(resolution, 0)
        ys = np.linspace(corner_i[1], corner_j[1], resolution)[None].repeat(resolution, 0)
        zs = np.linspace(-camera[-1], wall_height - camera[-1], resolution)[:, None].repeat(resolution, 1)
    else:
        xs = np.linspace(corner_i[0], corner_j[0], resolution)[None].repeat(resolution, 0)
        ys = np.linspace(corner_i[1], corner_j[1], resolution)[:, None].repeat(resolution, 1)
        zs = np.zeros_like(xs) + wall_height - camera[-1]

    coorx, coory = xyz_2_coorxy(xs, ys, zs)

    persp = cv2.remap(image, coorx.astype(np.float32), coory.astype(np.float32), 
                      cv2.INTER_CUBIC, borderMode=cv2.BORDER_WRAP)

    return persp 
开发者ID:bertjiazheng,项目名称:Structured3D,代码行数:23,代码来源:visualize_mesh.py

示例10: warp_by_params

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import remap [as 别名]
def warp_by_params (params, img, can_warp, can_transform, can_flip, border_replicate, cv2_inter=cv2.INTER_CUBIC):
    rw = params['rw']
    
    if (can_warp or can_transform) and rw is not None:
        img = cv2.resize(img, (64,64), interpolation=cv2_inter)
        
    if can_warp:
        img = cv2.remap(img, params['mapx'], params['mapy'], cv2_inter )
    if can_transform:
        img = cv2.warpAffine( img, params['rmat'], (params['w'], params['w']), borderMode=(cv2.BORDER_REPLICATE if border_replicate else cv2.BORDER_CONSTANT), flags=cv2_inter )
    
    
    if (can_warp or can_transform) and rw is not None:
        img = cv2.resize(img, (rw,rw), interpolation=cv2_inter)
    
    if len(img.shape) == 2:
        img = img[...,None]
    if can_flip and params['flip']:
        img = img[:,::-1,...]
    return img 
开发者ID:iperov,项目名称:DeepFaceLab,代码行数:22,代码来源:warp.py

示例11: optical_distortion

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import remap [as 别名]
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 
开发者ID:albumentations-team,项目名称:albumentations,代码行数:27,代码来源:functional.py

示例12: undistort

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import remap [as 别名]
def undistort(self, img, crop=True):
            undistorted = cv2.resize(cv2.remap(img, self.mapu, self.mapv,
                                               cv2.INTER_LINEAR),
                                     (self.mask.shape[1], self.mask.shape[0]),
                                     interpolation=cv2.INTER_CUBIC)
            if crop:
                undistorted = undistorted[self.y_lim[0]:self.y_lim[1],
                                          self.x_lim[0]:self.x_lim[1]]
            return undistorted 
开发者ID:ethz-asl,项目名称:hierarchical_loc,代码行数:11,代码来源:nclt.py

示例13: random_warp

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import remap [as 别名]
def random_warp( in_image ):
    assert in_image.shape[:2] == (256,256)

    image = in_image.copy()


    scale = 5

    range_ = numpy.linspace( 128-120, 128+120, scale )
    mapx = numpy.broadcast_to( range_, (scale,scale) )
    mapy = mapx.T

    mapx = mapx + numpy.random.normal( size=(scale,scale), scale= 6 )
    mapy = mapy + numpy.random.normal( size=(scale,scale), scale= 6 )

    interp_mapx = cv2.resize( mapx, (80,80) )[8:72,8:72].astype('float32')
    interp_mapy = cv2.resize( mapy, (80,80) )[8:72,8:72].astype('float32')

    warped_image = cv2.remap( image[:,:,:3], interp_mapx, interp_mapy, cv2.INTER_CUBIC )

    src_points = numpy.stack( [ mapx.ravel(), mapy.ravel() ], axis=-1 )
    dst_points = numpy.mgrid[0:65:16,0:65:16].T.reshape(-1,2)
    mat = umeyama( src_points, dst_points, True )[0:2]

    target_image = cv2.warpAffine( image, mat, (64,64) )

    target_mask = target_image[:,:,3].reshape((64,64,1))
    target_image = target_image[:,:,:3]


    if len(target_image.shape)>2:
      return ( warped_image, 
               target_image, 
               target_mask )
    else:
      return ( warped_image, 
               target_image ) 
开发者ID:dfaker,项目名称:df,代码行数:39,代码来源:image_augmentation.py

示例14: random_warp

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import remap [as 别名]
def random_warp(image):
  range_ = numpy.linspace(0, 256, 20)
  mapx = numpy.broadcast_to(range_, (20, 20))
  mapy = mapx.T
  numpy.random.seed( int(time.time()) )
  mapx = mapx + numpy.random.normal(size=(20, 20), scale=5)
  mapy = mapy + numpy.random.normal(size=(20, 20), scale=5)

  interp_mapx = cv2.resize(mapx, (256, 256)).astype('float32')
  interp_mapy = cv2.resize(mapy, (256, 256)).astype('float32')


  return cv2.remap(image, interp_mapx, interp_mapy, cv2.INTER_LINEAR) 
开发者ID:dfaker,项目名称:df,代码行数:15,代码来源:exampleTrainer.py

示例15: warp_flow

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import remap [as 别名]
def warp_flow(img, flow):
    h, w = flow.shape[:2]
    flow = -flow
    flow[:,:,0] += np.arange(w)
    flow[:,:,1] += np.arange(h)[:,np.newaxis]
    res = cv2.remap(img, flow, None, cv2.INTER_LINEAR)
    return res 
开发者ID:makelove,项目名称:OpenCV-Python-Tutorial,代码行数:9,代码来源:opt_flow.py


注:本文中的cv2.remap方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。