我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用skimage.transform.ProjectiveTransform()。
def process(self, im): # if side is right flip so it becomes right if self.side != 'left': im = np.fliplr(im) # slope of the perspective slope = tan(radians(self.degrees)) (h, w, _) = im.shape matrix_trans = np.array([[1, 0, 0], [-slope/2, 1, slope * h / 2], [-slope/w, 0, 1 + slope]]) trans = ProjectiveTransform(matrix_trans) img_trans = warp(im, trans) if self.side != 'left': img_trans = np.fliplr(img_trans) return img_trans
def warp(img, corners): """ Warpes an image by keeping its size, transforming the pixel data to be distorted between the four corners. """ width = len(img[0]) height = len(img) src = numpy.array(( corners['upper_left'], corners['lower_left'], corners['lower_right'], corners['upper_right'] )) dst = numpy.array(( (0, 0), (0, height), (width, height), (width, 0) )) tform = transform.ProjectiveTransform() tform.estimate(src, dst) return transform.warp(img, tform, output_shape=(height,width))
def scale_to_fit(img, size): """ Scales an image to a given size by warping with no regard to the ratio. Returns: warped image as ndarray """ width = len(img[0]) height = len(img) src = numpy.array(( (0, 0), (0, size[1]), (size[0], size[1]), (size[0], 0) )) dst = numpy.array(( (0, 0), (0, height), (width, height), (width, 0) )) tform = transform.ProjectiveTransform() tform.estimate(src, dst) return transform.warp(img, tform, output_shape=(size[1],size[0])) ######################################################################################################### #########################################################################################################
def warp_image_by_corner_points_projection(corner_points, image): """Given corner points of a Sudoku, warps original selection to a square image. :param corner_points: :type: corner_points: list :param image: :type image: :return: :rtype: """ # Clarify by storing in named variables. top_left, top_right, bottom_left, bottom_right = np.array(corner_points) top_edge = np.linalg.norm(top_right - top_left) bottom_edge = np.linalg.norm(bottom_right - bottom_left) left_edge = np.linalg.norm(top_left - bottom_left) right_edge = np.linalg.norm(top_right - bottom_right) L = int(np.ceil(max([top_edge, bottom_edge, left_edge, right_edge]))) src = np.array([top_left, top_right, bottom_left, bottom_right]) dst = np.array([[0, 0], [L - 1, 0], [0, L - 1], [L - 1, L - 1]]) tr = ProjectiveTransform() tr.estimate(dst, src) warped_image = warp(image, tr, output_shape=(L, L)) out = resize(warped_image, (500, 500)) return out
def projection(self, tile: HipsTile) -> ProjectiveTransform: """Estimate projective transformation on a HiPS tile.""" corners = tile.meta.skycoord_corners.to_pixel(self.geometry.wcs) src = np.array(corners).T.reshape((4, 2)) dst = tile_corner_pixel_coordinates(tile.meta.width) pt = ProjectiveTransform() pt.estimate(src, dst) return pt
def projective_transform_by_points(x, src, dst, map_args={}, output_shape=None, order=1, mode='constant', cval=0.0, clip=True, preserve_range=False): """Projective transform by given coordinates, usually 4 coordinates. see `scikit-image <http://scikit-image.org/docs/dev/auto_examples/applications/plot_geometric.html>`_. Parameters ----------- x : numpy array An image with dimension of [row, col, channel] (default). src : list or numpy The original coordinates, usually 4 coordinates of (x, y). dst : list or numpy The coordinates after transformation, the number of coordinates is the same with src. map_args : dict, optional Keyword arguments passed to inverse_map. output_shape : tuple (rows, cols), optional Shape of the output image generated. By default the shape of the input image is preserved. Note that, even for multi-band images, only rows and columns need to be specified. order : int, optional The order of interpolation. The order has to be in the range 0-5: - 0 Nearest-neighbor - 1 Bi-linear (default) - 2 Bi-quadratic - 3 Bi-cubic - 4 Bi-quartic - 5 Bi-quintic mode : {‘constant’, ‘edge’, ‘symmetric’, ‘reflect’, ‘wrap’}, optional Points outside the boundaries of the input are filled according to the given mode. Modes match the behaviour of numpy.pad. cval : float, optional Used in conjunction with mode ‘constant’, the value outside the image boundaries. clip : bool, optional Whether to clip the output to the range of values of the input image. This is enabled by default, since higher order interpolation may produce values outside the given input range. preserve_range : bool, optional Whether to keep the original range of values. Otherwise, the input image is converted according to the conventions of img_as_float. Examples -------- >>> Assume X is an image from CIFAR 10, i.e. shape == (32, 32, 3) >>> src = [[0,0],[0,32],[32,0],[32,32]] >>> dst = [[10,10],[0,32],[32,0],[32,32]] >>> x = projective_transform_by_points(X, src, dst) References ----------- - `scikit-image : geometric transformations <http://scikit-image.org/docs/dev/auto_examples/applications/plot_geometric.html>`_ - `scikit-image : examples <http://scikit-image.org/docs/dev/auto_examples/index.html>`_ """ if type(src) is list: # convert to numpy src = np.array(src) if type(dst) is list: dst = np.array(dst) if np.max(x)>1: # convert to [0, 1] x = x/255 m = transform.ProjectiveTransform() m.estimate(dst, src) warped = transform.warp(x, m, map_args=map_args, output_shape=output_shape, order=order, mode=mode, cval=cval, clip=clip, preserve_range=preserve_range) return warped # Numpy and PIL
def projective_transform_by_points(x, src, dst, map_args={}, output_shape=None, order=1, mode='constant', cval=0.0, clip=True, preserve_range=False): """Projective transform by given coordinates, usually 4 coordinates. see `scikit-image <http://scikit-image.org/docs/dev/auto_examples/applications/plot_geometric.html>`_. Parameters ----------- x : numpy array An image with dimension of [row, col, channel] (default). src : list or numpy The original coordinates, usually 4 coordinates of (width, height). dst : list or numpy The coordinates after transformation, the number of coordinates is the same with src. map_args : dict, optional Keyword arguments passed to inverse_map. output_shape : tuple (rows, cols), optional Shape of the output image generated. By default the shape of the input image is preserved. Note that, even for multi-band images, only rows and columns need to be specified. order : int, optional The order of interpolation. The order has to be in the range 0-5: - 0 Nearest-neighbor - 1 Bi-linear (default) - 2 Bi-quadratic - 3 Bi-cubic - 4 Bi-quartic - 5 Bi-quintic mode : {‘constant’, ‘edge’, ‘symmetric’, ‘reflect’, ‘wrap’}, optional Points outside the boundaries of the input are filled according to the given mode. Modes match the behaviour of numpy.pad. cval : float, optional Used in conjunction with mode ‘constant’, the value outside the image boundaries. clip : bool, optional Whether to clip the output to the range of values of the input image. This is enabled by default, since higher order interpolation may produce values outside the given input range. preserve_range : bool, optional Whether to keep the original range of values. Otherwise, the input image is converted according to the conventions of img_as_float. Examples -------- >>> Assume X is an image from CIFAR 10, i.e. shape == (32, 32, 3) >>> src = [[0,0],[0,32],[32,0],[32,32]] # [w, h] >>> dst = [[10,10],[0,32],[32,0],[32,32]] >>> x = projective_transform_by_points(X, src, dst) References ----------- - `scikit-image : geometric transformations <http://scikit-image.org/docs/dev/auto_examples/applications/plot_geometric.html>`_ - `scikit-image : examples <http://scikit-image.org/docs/dev/auto_examples/index.html>`_ """ if type(src) is list: # convert to numpy src = np.array(src) if type(dst) is list: dst = np.array(dst) if np.max(x)>1: # convert to [0, 1] x = x/255 m = transform.ProjectiveTransform() m.estimate(dst, src) warped = transform.warp(x, m, map_args=map_args, output_shape=output_shape, order=order, mode=mode, cval=cval, clip=clip, preserve_range=preserve_range) return warped # Numpy and PIL