Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中将图像中的曲线转换为直线?_Python_Image_Opencv_Image Processing_Scikit Image - Fatal编程技术网

如何在Python中将图像中的曲线转换为直线?

如何在Python中将图像中的曲线转换为直线?,python,image,opencv,image-processing,scikit-image,Python,Image,Opencv,Image Processing,Scikit Image,我是图像处理新手,正在处理以下图像: 在这些图片中,我需要拉直多条曲线,使它们看起来像一条直线。这里有一个快速解决方案。它可以通过对特征进行样条拟合来改进,而不仅仅是拟合抛物线。该算法根据拟合的抛物线分别移动图像中的每一行: from skimage import io, measure, morphology from matplotlib import pyplot as plt from scipy.optimize import curve_fit image = io.imread

我是图像处理新手,正在处理以下图像:


在这些图片中,我需要拉直多条曲线,使它们看起来像一条直线。

这里有一个快速解决方案。它可以通过对特征进行样条拟合来改进,而不仅仅是拟合抛物线。该算法根据拟合的抛物线分别移动图像中的每一行:

from skimage import io, measure, morphology
from matplotlib import pyplot as plt
from scipy.optimize import curve_fit

image = io.imread('curves.png', as_gray=True)
# need a binary mask of features
mask = image == image.min()
# close holes in features
mask = morphology.binary_closing(mask, morphology.square(3))

plt.matshow(mask, cmap='gray')

# need to get the coordinates of each feature
rp = measure.regionprops(measure.label(mask))

# going to fit a parabola to the features
def parabola(x, x0, A, y0):
    return A*(x-x0)**2 + y0

# get coords of one of the features
coords = rp[0].coords
# do parabola fit
pop, pcov = curve_fit(parabola, coords[:,0], coords[:,1])
# generate fit
fit = parabola(np.arange(mask.shape[0]), *pop)
# plot fit
plt.plot(fit, np.arange(mask.shape[0])) # invert axes

# generate new image to shift
out = np.empty_like(image)
# shift each row individually and add to out array
for i, row in enumerate(image):
    out[i] = np.roll(row, -int(round(fit[i] - pop[-1])))

plt.matshow(out, cmap='gray')
原始面罩和安装的抛物线:

from skimage import io, measure, morphology
from matplotlib import pyplot as plt
from scipy.optimize import curve_fit

image = io.imread('curves.png', as_gray=True)
# need a binary mask of features
mask = image == image.min()
# close holes in features
mask = morphology.binary_closing(mask, morphology.square(3))

plt.matshow(mask, cmap='gray')

# need to get the coordinates of each feature
rp = measure.regionprops(measure.label(mask))

# going to fit a parabola to the features
def parabola(x, x0, A, y0):
    return A*(x-x0)**2 + y0

# get coords of one of the features
coords = rp[0].coords
# do parabola fit
pop, pcov = curve_fit(parabola, coords[:,0], coords[:,1])
# generate fit
fit = parabola(np.arange(mask.shape[0]), *pop)
# plot fit
plt.plot(fit, np.arange(mask.shape[0])) # invert axes

# generate new image to shift
out = np.empty_like(image)
# shift each row individually and add to out array
for i, row in enumerate(image):
    out[i] = np.roll(row, -int(round(fit[i] - pop[-1])))

plt.matshow(out, cmap='gray')

结果: