Python 运行自制的旋转算法时获得错误的图片输出
为了更好地理解图像处理的工作原理,我决定创建自己的图像旋转算法,而不是使用cv2.rotate()。然而,我遇到了一个奇怪的图片裁剪和像素错位问题 我认为这可能与我的填充有关,但可能还有其他错误Python 运行自制的旋转算法时获得错误的图片输出,python,numpy,image-manipulation,image-rotation,Python,Numpy,Image Manipulation,Image Rotation,为了更好地理解图像处理的工作原理,我决定创建自己的图像旋转算法,而不是使用cv2.rotate()。然而,我遇到了一个奇怪的图片裁剪和像素错位问题 我认为这可能与我的填充有关,但可能还有其他错误 import cv2 import math import numpy as np # Load & Show original image img = cv2.imread('Lena.png', 0) cv2.imshow('Original', img) # Variable dec
import cv2
import math
import numpy as np
# Load & Show original image
img = cv2.imread('Lena.png', 0)
cv2.imshow('Original', img)
# Variable declarations
h = img.shape[0] # Also known as rows
w = img.shape[1] # Also known as columns
cX = h / 2 #Image Center X
cY = w / 2 #Image Center Y
theta = math.radians(100) #Change to adjust rotation angle
imgArray = np.array((img))
imgArray = np.pad(imgArray,pad_width=((100,100),(100,100)),mode='constant',constant_values=0)
#Add padding in an attempt to prevent image cropping
# loop pixel by pixel in image
for x in range(h + 1):
for y in range(w + 1):
try:
TX = int((x-cX)*math.cos(theta)+(y-cY)*math.sin(theta)+cX) #Rotation formula
TY = int(-(x-cX)*math.sin(theta)+(y-cY)*math.cos(theta)+cY) #Rotation formula
imgArray[x,y] = img[TX,TY]
except IndexError as error:
print(error)
cv2.imshow('Rotated', imgArray)
cv2.waitKey(0)
编辑:
我认为错位的图像位置可能与缺少正确的原点有关,但是我似乎找不到解决该问题的有效方法。虽然我没有深入研究域的数学部分,但基于给定的信息,我认为矩阵旋转公式应该是这样工作的: 更新: 正如我所承诺的那样,我深入了这个领域,找到了如下解决方案。我在循环中也交换了源索引和目标索引的主要技巧,因此舍入并不意味着任何问题:
import cv2
import math
import numpy as np
# Load & Show original image
img = cv2.imread('/home/george/Downloads/lena.png', 0)
cv2.imshow('Original', img)
# Variable declarations
h = img.shape[0] # Also known as rows
w = img.shape[1] # Also known as columns
p = 120
h += 2 * p
w += 2 * p
cX = h / 2 #Image Center X
cY = h / 2 #Image Center Y
theta = math.radians(45) #Change to adjust rotation angle
imgArray = np.zeros_like((img))
#Add padding in an attempt to prevent image cropping
imgArray = np.pad(imgArray, pad_width=p, mode='constant', constant_values=0)
img = np.pad(img, pad_width=p, mode='constant', constant_values=0)
# loop pixel by pixel in image
for TX in range(h + 1):
for TY in range(w + 1):
try:
x = int( +(TX - cX) * math.cos(theta) + (TY - cY) * math.sin(theta) + cX) #Rotation formula
y = int( -(TX - cX) * math.sin(theta) + (TY - cY) * math.cos(theta) + cY) #Rotation formula
imgArray[TX, TY] = img[x, y]
except IndexError as error:
pass
# print(error)
cv2.imshow('Rotated', imgArray)
cv2.waitKey(0)
exit()
注意:如果您想深入该领域,也请参阅usr2564301评论。Geeocode!很高兴再次见到你:D这么小的变化,这么大的结果。我可以粗鲁地问一下这些变化是怎么回事吗?将numpy数组更改为零的原因是什么,这不就是用0而不是图像值填充整个数组吗?我猜你对TX&TY做+100是为了将每个旋转的像素移动到图像中心,对吗?@MikkelGHansen请参见解释;)又是一份了不起的工作。谢谢你清楚的解释。为了一个简单的数组更改+填充补偿问题,我花了这么多时间,用了这么多不同的方法,在这个问题上工作了这么多小时,我觉得自己很傻:P现在的任务是清除恼人的黑点。考虑阈值和过滤,但灰度图像使其有点困难。可能是功能查找/耸耸肩,但再次感谢您的帮助!:D@MikkelGHansen老实说,这是我第一次尝试回答这个问题。cv2和矩阵旋转等等,但我发现它很有趣,而且真的很有趣。如果我有更多的时间,我可能会重新考虑我的答案,如果需要的话,也会进行一些更新。@MikkelGHansen在代码和描述中也看到了关于填充校正和插值的更新。