gpt4 book ai didi

python - (Python)如何旋转图像以使特征变为垂直?

转载 作者:太空宇宙 更新时间:2023-11-03 21:25:46 24 4
gpt4 key购买 nike

我想旋转图像(例如下面的图像),使其其中一个特征(类似于一条线)变得垂直。但是,我似乎无法找到一种方法在 Python 中以编程方式执行此操作。 Example_Image

最佳答案

旋转本身可以通过 scipy.ndimage.interpolation.rotate 来完成手术。

下面的第一部分是解决原始问题中示例场景的问题(有一个拉长的数据 block ),请参阅下面的更通用(但速度较慢)的方法。希望这可以帮助!

<小时/>

第一种方法:要找到轴以及线的角度,我建议对非零值使用 PCA:

from scipy.ndimage.interpolation import rotate
#from skimage.transform import rotate ## Alternatively
from sklearn.decomposition.pca import PCA ## Or use its numpy variant
import numpy as np

def verticalize_img(img):
"""
Method to rotate a greyscale image based on its principal axis.

:param img: Two dimensional array-like object, values > 0 being interpreted as containing to a line
:return rotated_img:
"""# Get the coordinates of the points of interest:
X = np.array(np.where(img > 0)).T
# Perform a PCA and compute the angle of the first principal axes
pca = PCA(n_components=2).fit(X)
angle = np.arctan2(*pca.components_[0])
# Rotate the image by the computed angle:
rotated_img = rotate(img,angle/pi*180-90)
return rotated_img

通常这个函数也可以写成单行函数:

rotated_img = rotate(img,np.arctan2(*PCA(2).fit(np.array(np.where(img > 0)).T).components_[0])/pi*180-90)

这是一个例子:

from matplotlib import pyplot as plt
# Example data:
img = np.array([[0,0,0,0,0,0,0],
[0,1,0,0,0,0,0],
[0,0,1,1,0,0,0],
[0,0,0,1,1,0,0],
[0,0,1,0,0,1,0],
[0,0,0,0,0,0,1]])
# Or alternatively a straight line:
img = np.diag(ones(15))
img = np.around(rotate(img,25))

# Or a distorted blob:
from sklearn import cluster, datasets
X, y = datasets.make_blobs(n_samples=100, centers = [[0,0]])
distortion = [[0.6, -0.6], [-0.4, 0.8]]
theta = np.radians(20)
rotation = np.array(((cos(theta),-sin(theta)), (sin(theta), cos(theta))))
X = np.dot(np.dot(X, distortion),rotation)
img = np.histogram2d(*X.T)[0] # > 0 ## uncomment for making the example binary

rotated_img = verticalize_img(img)
# Plot the results
plt.matshow(img)
plt.title('Original')
plt.matshow(rotated_img)
plt.title('Rotated'))

请注意,对于高噪声数据或没有明确方向的图像,此方法将产生任意旋转。

这是一个示例输出:

enter image description here enter image description here

<小时/>

第二种方法:在更复杂的设置中澄清实际任务后(请参阅评论),这里是基于模板匹配的第二种方法:

from matplotlib import pyplot as plt
import numpy as np
import pandas
from scipy.ndimage.interpolation import rotate
from scipy.signal import correlate2d#, fftconvolve
# Data from CSV file:
img = pandas.read_csv('/home/casibus/testdata.csv')
# Create a template:
template = np.zeros_like(img.values)
template[:,int(len(template[0])*1./2)] = 1
suggested_angles = np.arange(0,180,1) # Change to any resolution you like
overlaps = [np.amax(correlate2d(rotate(img,alpha,reshape=False),template,mode='same')) for alpha in suggested_angles]
# Determine the angle resulting in maximal overlap and rotate:
rotated_img = rotate(img.values,-suggested_angles[np.argmax(overlaps)])
plt.matshow(rotated_img)
plt.matshow(template)

rotated image template

关于python - (Python)如何旋转图像以使特征变为垂直?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53854066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com