【问题标题】:Python: scipy curve_fit for 2d array (an image)Python:用于二维数组的 scipy curve_fit(图像)
【发布时间】:2022-01-18 18:10:47
【问题描述】:

我尝试使用topic 中的解释(第一个答案),为具有 6 个参数的 2D 函数创建 lsq 拟合,该函数从数据(图像,因此是 2D 数组)中找到优化的参数。它通过将二维数组枚举为一维数组来实现这一点,并且给出的示例工作正常。

我得到一个

TypeError: only size-1 arrays can be converted to Python scalars

下面的代码。这可能是我得到的一个微不足道的错误,因为我仍然熟悉 Python,但我似乎无法理解为什么我的代码不起作用,而主题中的示例却可以。有人看到我的错误吗?

def lsqFitFunc(img):
        
    def fitFullFunc(xy, amplitude, sigmaY, shiftX, shiftY, background, sigmaX):
        x = xy % imspx.shape[1] # reconstruct x coordinates [0..size]
        y = xy // imspx.shape[1] 
        return background + amplitude * math.exp(-sigmaY * ( y - shiftY )**2  ) * ( (1/2) * ( 1+math.erf( ( x - shiftX )/(13*sigmaX/math.sqrt(2)) ) ) )    

    xy = np.arange(img.size)  # 0 is the top left pixel and 199 is the top right pixel    
    res = sp.optimize.curve_fit(fitFullFunc, xy, np.ravel(img))        
    return res

我正在为此函数使用以下输入:

img = np.array([[223, 228, 231, 233, 236, 241, 244, 247, 251, 250, 253, 255, 255,
        254, 255, 255, 255, 255, 255, 255, 255, 255],
       [205, 209, 214, 219, 220, 224, 229, 235, 240, 242, 246, 251, 254,
        254, 255, 255, 255, 255, 255, 255, 255, 255],
       [183, 188, 193, 196, 200, 205, 210, 217, 223, 229, 232, 241, 244,
        251, 255, 255, 255, 255, 255, 254, 255, 255],
       [166, 169, 172, 176, 181, 184, 191, 196, 202, 209, 216, 226, 234,
        243, 250, 255, 254, 255, 255, 255, 255, 255],
       [152, 156, 157, 161, 164, 167, 172, 178, 186, 193, 203, 212, 224,
        234, 245, 252, 252, 255, 255, 255, 255, 255],
       [145, 146, 147, 149, 152, 156, 158, 163, 171, 179, 191, 202, 217,
        228, 238, 249, 251, 254, 253, 255, 255, 255],
       [140, 142, 142, 144, 145, 148, 150, 156, 163, 174, 185, 197, 212,
        223, 235, 245, 251, 254, 255, 255, 255, 255],
       [140, 141, 142, 144, 144, 148, 150, 157, 165, 174, 188, 198, 213,
        226, 239, 246, 251, 254, 254, 253, 255, 254],
       [142, 144, 145, 146, 148, 153, 156, 162, 172, 181, 194, 206, 220,
        230, 241, 250, 252, 255, 253, 255, 255, 255],
       [147, 149, 151, 153, 156, 161, 167, 173, 184, 195, 206, 217, 230,
        237, 247, 253, 255, 255, 254, 255, 254, 253],
       [155, 158, 160, 165, 168, 176, 180, 190, 200, 208, 220, 229, 240,
        246, 253, 255, 255, 255, 254, 255, 254, 255],
       [168, 171, 176, 179, 187, 192, 198, 206, 214, 222, 232, 239, 248,
        255, 254, 255, 255, 255, 255, 255, 255, 255],
       [186, 188, 196, 201, 205, 209, 214, 223, 230, 236, 243, 249, 251,
        255, 255, 255, 255, 255, 255, 255, 255, 255]])

【问题讨论】:

标签: python image-processing scipy curve-fitting


【解决方案1】:

与同事一起,我们发现数学函数 (exp, erf) 不接受数组,这导致了错误。应使用这些函数的 numpy 和 scipy 函数:

return background + amplitude * np.exp(-sigmaY * ( y - shiftY )**2  ) * ( (1/2) * ( 1+ sp.special.erf( ( x - shiftX )/(13*sigmaX/math.sqrt(2)) ) ) )    

【讨论】:

  • 如果你解决了你的问题,你能接受你自己的答案吗?
  • 我会做,48小时后可以做
猜你喜欢
  • 2019-04-16
  • 1970-01-01
  • 2014-09-05
  • 2016-03-08
  • 2012-06-15
  • 2021-11-12
  • 2022-09-29
  • 2016-02-07
  • 1970-01-01
相关资源
最近更新 更多