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へ
感謝。私はコードを貼り付けて、後世のために以下のような力を使うという提案を実装します。
%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)
'scipy.optimize.curve_fit'は、このジョブの適切なツールではありません。私はファンクションがあなたが思っていることをしているとは思わない。 –