我正在使用相机信号。摄像机的典型信号用以下模拟:
import cairo
import numpy
import matplotlib.pyplot as plt
data = numpy.zeros((352, 352), dtype = numpy.uint8)
surface = cairo.ImageSurface.create_for_data(
data, cairo.FORMAT_A8, 352, 352)
cr = cairo.Context(surface)
cr.scale(0.85, 1.15)
cr.arc(200, 150, 100, 0, 2. * numpy.pi)
cr.set_line_width(15)
cr.stroke()
plt.imshow(data, cmap = "gray", interpolation = "None")
plt.show()
从此图片数据集中,我需要获取以下参数:x-center
,y-center
,radius
,thickness
,ellipticity
,{{1}白色椭圆环。
获得这些参数的快速准确方法是什么?
我多年来获得的代码片段是以下2D高斯拟合:
angle of the ellipse
但是,我无法将import scipy.odr
def gauss_2D(p, r):
# p = [amplitude, x-cen, y-cen, x-waist, y-waist, angle_in_rad, z-offset]
x = r[0,]
y = r[1,]
dx, dy = (x - p[1]), (y - p[2])
xp = dx * numpy.cos(p[5]) + dy * numpy.sin(p[5])
yp = dy * numpy.cos(p[5]) - dx * numpy.sin(p[5])
data = (p[0] * numpy.exp(-2 * (xp / p[3])**2 - 2 * (yp / p[4])**2))
return data + p[6]
def fit_gauss(p_guess, data_set):
rr = numpy.indices(data_set.shape[:2])
N = rr.shape[1] * rr.shape[2]
fix = [1, 1, 1, 1, 1, 1, 1]
model = scipy.odr.Model(gauss_2D)
data = scipy.odr.Data(rr.reshape((2, N)), data_set.reshape((1, N)))
odr = scipy.odr.ODR(data, model, p_guess, ifixb = fix)
odr.set_job(fit_type = 2)
output = odr.run()
coeff = output.beta
coeff_sd = output.sd_beta
return rr, coeff, coeff_sd
重写为适用于具有特定线条粗细的椭圆的函数。
感谢所有帮助!