平滑颠簸的圆圈 [英] Smooth a bumpy circle
问题描述
我正在检测圆形物体的边缘,并获得凹凸不平"的不规则边缘.是否有使边缘光滑的方法,以便使我具有更均匀的形状?
I am detecting edges of round objects and am obtaining "bumpy" irregular edges. Is there away to smooth the edges so that I have a more uniform shape?
例如,在下面的代码中,我生成一个凹凸不平"的圆圈(左).我可以使用某种平滑或移动平均函数来获得或近似平滑"圆(右).最好使用某种参数,因为我的实际图像不是完美的圆形,所以我可以控制它.
For example, in the code below I generate a "bumpy" circle (left). Is there a smoothing or moving average kind of function I could use to obtain or approximate the "smooth" circle (right). Preferably with some sort of parameter I can control as my actual images arn't perfectly circular.
import numpy as np
import matplotlib.pyplot as plt
fig, (bumpy, smooth) = plt.subplots(ncols=2, figsize=(14, 7))
an = np.linspace(0, 2 * np.pi, 100)
bumpy.plot(3 * np.cos(an) + np.random.normal(0,.03,100), 3 * np.sin(an) + np.random.normal(0,.03,100))
smooth.plot(3 * np.cos(an), 3 * np.sin(an))
推荐答案
您可以在频域中执行此操作.取曲线点的(x,y)
坐标并将信号构造为signal = x + yj
,然后对该信号进行傅立叶变换.过滤掉高频分量,然后进行傅立叶逆变换,您将获得一条平滑的曲线.您可以通过调整截止频率来控制平滑度.
You can do this in frequency domain. Take the (x,y)
coordinates of the points of your curve and construct the signal as signal = x + yj
, then take the Fourier transform of this signal. Filter out the high frequency components, then take the inverse Fourier transform and you'll get a smooth curve. You can control the smoothness by adjusting the cutoff frequency.
这是一个例子:
import numpy as np
from matplotlib import pyplot as plt
r = 3
theta = np.linspace(0, 2 * np.pi, 100)
noise_level = 2
# construct the signal
x = r * np.cos(theta) + noise_level * np.random.normal(0,.03,100)
y = r * np.sin(theta) + noise_level * np.random.normal(0,.03,100)
signal = x + 1j*y
# FFT and frequencies
fft = np.fft.fft(signal)
freq = np.fft.fftfreq(signal.shape[-1])
# filter
cutoff = 0.1
fft[np.abs(freq) > cutoff] = 0
# IFFT
signal_filt = np.fft.ifft(fft)
plt.figure()
plt.subplot(121)
plt.axis('equal')
plt.plot(x, y, label='Noisy')
plt.subplot(122)
plt.axis('equal')
plt.plot(signal_filt.real, signal_filt.imag, label='Smooth')
这篇关于平滑颠簸的圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!