首页 > 解决方案 > 如何通过变换矩阵在python中进行图像变换操作(旋转、缩放、平移)

问题描述

我想在没有任何其他外部库(PIL,CV2)函数调用的情况下对图像执行矩阵变换操作,如旋转、缩放、平移。

我正在尝试使用基本的矩阵变换方法。但我面临一些问题,比如旋转有孔,图像被裁剪。

我想沿着中心执行上述所有操作。这里需要一些指导。

旋转逻辑:

newImage = np.zeros((2*row, 2*column, 3), dtype=np.uint8)
radAngle = math.radians(int(input("angle: ")))
c, s = math.cos(radAngle), math.sin(radAngle)
mid_coords = np.floor(0.5*np.array(image.shape))
for i in range(0, row-1):
    for j in range(0, column-1):
        x2 = mid_coords[0] + (i-mid_coords[0])*c - (j-mid_coords[1])*s
        y2 = mid_coords[1] + (i-mid_coords[0])*s + (j-mid_coords[1])*c 
        newImage[int(math.ceil(x2)), int(math.ceil(y2))] = image[i, j]

标签: opencvimage-processingmatrixcomputer-visionpython-imaging-library

解决方案


旋转图像中出现孔的原因是您将原始图像的每个像素分配给旋转图像中的计算像素位置。因此,旋转图像中的一些像素被遗漏了。你应该反过来做。对于旋转图像中的每个像素,从原始图像中找到它的值。请参考这个答案。


推荐阅读