曲线拟合scipy.stats.multivariate_normal

发布于 2025-01-27 12:13:53 字数 1113 浏览 5 评论 0原文

我试图将曲线拟合2D多元正态分布与图像。方法在这里有效,但是我想用协方差矩阵来参数化结果,例如scipy.stats。 Multivariate_normal

这是我的代码:

def mvnorm(data_tuple, mean1, mean2, cov1, cov2, cov3):
     (x, y) = data_tuple
     pos = numpy.dstack((x, y))
     mean = [mean1,mean2]
     cov = [[cov1,cov3],[cov3,cov2]]
     result = scipy.stats.multivariate_normal.pdf(pos, mean, cov)

     return result

 def fit_mvnorm(array, coords, initial_guess):
     shp = numpy.shape(array)
     step1 = abs(coords[0]-coords[1])/shp[1]
     step2 = abs(coords[2]-coords[3])/shp[1]
     x, y = numpy.mgrid[coords[0]:coords[1]:step1, coords[2]:coords[3]:step2]    
     popt,pcov = scipy.optimize.curve_fit(mvnorm, (x,y), array, p0=initial_guess)

     return popt, pcov

我的数据numpy.ndarray包含图像的亮度值(float)并具有形状(548,548)。 坐标是轴限制坐标的列表。当我运行该功能时,我会收到以下错误:

ValueError: object too deep for desired array
...
error: Result from function call is not a proper array of floats.

您对尝试什么有任何想法吗?

I am trying to curve fit a 2D multivariate normal distribution to an image. The approach here works, however I want to parametrize the result with a covariance matrix, like in scipy.stats.multivariate_normal.

Here is my code:

def mvnorm(data_tuple, mean1, mean2, cov1, cov2, cov3):
     (x, y) = data_tuple
     pos = numpy.dstack((x, y))
     mean = [mean1,mean2]
     cov = [[cov1,cov3],[cov3,cov2]]
     result = scipy.stats.multivariate_normal.pdf(pos, mean, cov)

     return result

 def fit_mvnorm(array, coords, initial_guess):
     shp = numpy.shape(array)
     step1 = abs(coords[0]-coords[1])/shp[1]
     step2 = abs(coords[2]-coords[3])/shp[1]
     x, y = numpy.mgrid[coords[0]:coords[1]:step1, coords[2]:coords[3]:step2]    
     popt,pcov = scipy.optimize.curve_fit(mvnorm, (x,y), array, p0=initial_guess)

     return popt, pcov

My data numpy.ndarray contains the image's brightness values (float) and has shape (548,548). coords is a list of the axis limit coordinates. When I run the function, I get the following error:

ValueError: object too deep for desired array
...
error: Result from function call is not a proper array of floats.

Do you have any idea on what to try?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文