zoukankan      html  css  js  c++  java
  • 图像仿射变换之图像平移 python实现

    写文章不易,如果您觉得此文对您有所帮助,请帮忙点赞、评论、收藏,感谢您!

    一. 仿射变换介绍:

            请参考:图解图像仿射变换:https://www.cnblogs.com/wojianxin/p/12518393.html

    二. 仿射变换 公式:


    仿射变换过程,(x,y)表示原图像中的坐标,(x',y')表示目标图像的坐标 ↑
     

    三. 仿射变换——图像平移 算法:


    仿射变换—图像平移算法,其中tx为在横轴上移动的距离,ty为在纵轴上移动的距离 ↑
     

    四. python实现仿射变换——图像平移

     1 import cv2
     2 import numpy as np
     3 
     4 # 图像仿射变换->图像平移 
     5 def affine(img, a, b, c, d, tx, ty):
     6     H, W, C = img.shape
     7 
     8     # temporary image
     9     tem = img.copy()
    10     img = np.zeros((H+2, W+2, C), dtype=np.float32)
    11     img[1:H+1, 1:W+1] = tem
    12 
    13     # get new image shape
    14     H_new = np.round(H * d).astype(np.int)
    15     W_new = np.round(W * a).astype(np.int)
    16     out = np.zeros((H_new+1, W_new+1, C), dtype=np.float32)
    17 
    18     # get position of new image
    19     x_new = np.tile(np.arange(W_new), (H_new, 1))
    20     y_new = np.arange(H_new).repeat(W_new).reshape(H_new, -1)
    21 
    22     # get position of original image by affine
    23     adbc = a * d - b * c
    24     x = np.round((d * x_new  - b * y_new) / adbc).astype(np.int) - tx + 1
    25     y = np.round((-c * x_new + a * y_new) / adbc).astype(np.int) - ty + 1
    26 
    27     # 避免目标图像对应的原图像中的坐标溢出
    28     x = np.minimum(np.maximum(x, 0), W+1).astype(np.int)
    29     y = np.minimum(np.maximum(y, 0), H+1).astype(np.int)
    30 
    31     # assgin pixcel to new image
    32     out[y_new, x_new] = img[y, x]
    33 
    34     out = out[:H_new, :W_new]
    35     out = out.astype(np.uint8)
    36 
    37     return out
    38 
    39 
    40 # Read image
    41 image1 = cv2.imread("../paojie.jpg").astype(np.float32)
    42 
    43 # Affine : 平移,tx(W向):向右30;ty(H向):向上100
    44 out = affine(image1, a=1, b=0, c=0, d=1, tx=30, ty=-100)
    45 
    46 # Save result
    47 cv2.imshow("result", out)
    48 cv2.imwrite("out.jpg", out)
    49 cv2.waitKey(0)
    50 cv2.destroyAllWindows()

    五. 代码实现过程中遇到的问题:   

     ① 原图像进行仿射变换时,原图像中的坐标可能超出了目标图像的边界,需要对原图像坐标进行截断处理。如何做呢?首先,计算目标图像坐标对应的原图像坐标,算法如下:


    目标图像坐标反向求解原图像坐标公式 ↑
     

            以下代码实现了该逆变换:

            # get position of original image by affine

            adbc = a * d - b * c

            x = np.round((d * x_new  - b * y_new) / adbc).astype(np.int) - tx + 1

            y = np.round((-c * x_new + a * y_new) / adbc).astype(np.int) - ty + 1

        然后对原图像坐标中溢出的坐标进行截断处理(取边界值),下面代码提供了这个功能:

            x = np.minimum(np.maximum(x, 0), W+1).astype(np.int)

            y = np.minimum(np.maximum(y, 0), H+1).astype(np.int)

            原图像范围:长W,高H

        ② 难点解答:

            x_new = np.tile(np.arange(W_new), (H_new, 1))

            y_new = np.arange(H_new).repeat(W_new).reshape(H_new, -1)


    x_new 矩阵横向长 W_new,纵向长H_new ↑
     
     

    y_new 矩阵横向长 W_new,纵向长H_new ↑  
     

            造这两个矩阵是为了这一步:    

            # assgin pixcel to new image

            out[y_new, x_new] = img[y, x]


    六. 实验结果:


    原图 ↑
     

    仿射变换—图像平移后结果(向右30像素,向上100像素) ↑
     

    七. 参考内容:

        ①  https://www.jianshu.com/p/1cfb3fac3798


    八. 版权声明:

        未经作者允许,请勿随意转载抄袭,抄袭情节严重者,作者将考虑追究其法律责任,创作不易,感谢您的理解和配合!

  • 相关阅读:
    十一周
    第五次作业
    第十周
    测试作业
    2016.06.24-2016.06.30这周工作时间和内容
    2016.06.10-2016.06.16这周工作时间和内容
    2016.06.03-2016.06.09这周工作时间和内容
    2016.05.26-2016.06.02这周工作时间和内容
    2016.05.20-2016.05.26这周工作时间和内容
    分答
  • 原文地址:https://www.cnblogs.com/wojianxin/p/12519498.html
Copyright © 2011-2022 走看看