basic_model.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:sea-lion-counter 作者: rdinse 项目源码 文件源码
def applyLinearTransformToImage(self, image, angle, shear_x, shear_y, scale, size_out):
    '''Apply the image transformation specified by three parameters.

    Time it takes warping a 256 x 256 RGB image with various affine warping functions:

    * 0.25ms cv2.warpImage, nearest interpolation
    * 0.26ms cv2.warpImage, linear interpolation
    * 5.11ms ndii.affine_transform, order=0
    * 5.93ms skimage.transform._warps_cy._warp_fast, linear interpolation

    Args:
      x: 2D numpy array, a single image.
      angle: Angle by which the image is rotated.
      shear_x: Shearing factor along the x-axis by which the image is sheared.
      shear_y: Shearing factor along the x-axis by which the image is sheared.
      scale: Scaling factor by which the image is scaled.
      channel_axis: Index of axis for channels in the input tensor.

    Returns:
      A tuple of transformed version of the input and the correction scale factor.

    '''
    # Positions of the image center before and after the transformation in
    # pixel coordinates.
    s_out = (size_out, size_out)
    c_in = .5 * np.asarray(image.shape[:2], dtype=np.float64).reshape((2, 1))
    c_out = .5 * np.asarray(s_out, dtype=np.float64).reshape((2, 1)) 

    angle = -angle
    M_rot_inv = np.asarray([[math.cos(angle), -math.sin(angle)], \
                            [math.sin(angle),  math.cos(angle)]])
    M_shear_inv = (1. / (shear_x * shear_y - 1.)) \
                * np.asarray([[-1., shear_x], [shear_y, -1.]])

    M_inv = np.dot(M_shear_inv, M_rot_inv)  # First undo rotation, then shear.

    M_inv /= scale

    offset = c_in - np.dot(M_inv, c_out)

    # cv2.warpAffine transform according to dst(p) = src(M_inv * p + offset).
    # No need to reverse the channels because the channels are interpolated
    # separately.
    warped = cv2.warpAffine(image, np.concatenate((M_inv, offset), axis=1), s_out, 
                            flags=cv2.INTER_LANCZOS4 | cv2.WARP_INVERSE_MAP)
                            # flags=cv2.INTER_CUBIC | cv2.WARP_INVERSE_MAP)

    return warped
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号