2016-12-16 4 views
1

2つの重なり合う円の2Dイメージをフィットしようとしています。その中の1つの円は完全に1で、もう1つは完全にゼロで構成されています。以下は最小の実例です。2つの重なり合う円のイメージを合わせる

curve_fitはフィッティングを全くしていないようですが、ちょうど最初の推測を返します。私はサークルの1つに1つのパラメータをフィットさせようとしましたが、最終的には、両方の円の中心と各円の半径のすべてのパラメータに合わせたいと思います。

ご協力いただければ幸いです。ありがとう。

%matplotlib inline 

import numpy as np 
import matplotlib.pyplot as plt 

from scipy.optimize import curve_fit 

def overlapping_circles((x, y), x0, y0, r0, x1, y1, r1): 
    img = np.zeros_like(x) 

    img[np.sqrt((x - x0)**2 + (y - y0)**2) <= r0] = 1. 
    img[np.sqrt((x - x1)**2 + (y - y1)**2) <= r1] = 0. 

    return img.ravel() 

size = 100. 
# Create x and y indices 
x = np.linspace(0, size-1, size) 
y = np.linspace(0, size-1, size) 
x, y = np.meshgrid(x, y) 
x0, y0 = size/2., size/2. 
r0 = 30. 
x1, y1 = size/2., size/4. 
r1 = 30. 
img = overlapping_circles((x, y), x0, y0, r0, x1, y1, r1) 
plt.imshow(img.reshape(size, size), interpolation='none') 

popt, pcov = curve_fit(lambda (x,y), guess_x0: overlapping_circles((x,y), guess_x0, y0, r0, x1, y1, r1), (x, y), img, p0=x0/2.) 
print(popt, x0/2.) 

重複丸:可能な解決策のためのlucianopazへ

Overlapping circles


感謝。私はコードを貼り付けて、後世のために以下のような力を使うという提案を実装します。

%matplotlib inline 

import numpy as np 
import matplotlib.pyplot as plt 

from scipy.optimize import brute 

def overlapping_circles((x, y), x0, y0, r0, x1, y1, r1): 
    img = np.zeros_like(x) 

    img[np.sqrt((x - x0)**2 + (y - y0)**2) <= r0] = 1. 
    img[np.sqrt((x - x1)**2 + (y - y1)**2) <= r1] = 0. 

    return img.ravel() 

def residuals((x, y), x0, y0, r0, x1, y1, r1, img): 
    return np.sum((overlapping_circles((x, y), x0, y0, r0, x1, y1, r1) - img.ravel())**2.) 

size = 100. 
# Create x and y indices 
x = np.linspace(0, size-1, size) 
y = np.linspace(0, size-1, size) 
x, y = np.meshgrid(x, y) 
x0, y0 = size/2., size/2. 
r0 = 30. 
x1, y1 = size/2., size/4. 
r1 = 30. 
img = overlapping_circles((x, y), x0, y0, r0, x1, y1, r1) 
plt.imshow(img.reshape(size, size), interpolation='none') 

simplified_residuals = lambda fit_x0: residuals((x, y), fit_x0[0], fit_x0[1], r0, x1, y1, r1, img) 

rranges = (slice(0, 100, 1), slice(0, 100, 1)) 
resbrute = brute(simplified_residuals, rranges, full_output=True, finish=None) 
print(resbrute[0], x0, y0) 
+1

'scipy.optimize.curve_fit'は、このジョブの適切なツールではありません。私はファンクションがあなたが思っていることをしているとは思わない。 –

答えて

1

curve_fitの実装では、私がxyがravel'edとdocsは、独立変数があるべきと言うので、配列に置かれるべきだと思うわずかなコメントで、正しいです:

k個の予測子を持つ関数のM長のシーケンスまたは(k、M)型の配列。データが測定される独立変数。

しかし、私はあなたの問題はそこにはないと信じていますが、機能overlapping_circlesの調整が不十分だと思います。パラメータの小さな変化がピクセル離散化imgのためにimgに変化をもたらさないので、微分可能ではありません。適合を改善するために、最小化された関数のjacobiansもhessiansも使用しないメソッドを使用する必要があります。私はあなたが見てhereherehereをお勧めします。もちろん、あなたの問題については、パラメータのおおよその見積もりを得るためにbrute forceの手法を使用するだけで役に立ちます。

+0

パーフェクト!強引な力を使用することのあなたの提案は働いた。私はオリジナルの質問の編集として使用した解決策を貼り付けました。ありがとう。 – decaelus

関連する問題