In [6]:
file = '/Users/schriste/Desktop/FOXSI-SAAS-Mock-Target-Test.png'
In [12]:
import skimage
from skimage.io import imread
from skimage import data, filter, color
from skimage.transform import hough_circle
from skimage.feature import peak_local_max
from skimage.draw import circle_perimeter
from skimage.util import img_as_ubyte
print('skimage version %s' % skimage.__version__)
In [133]:
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib as mpl
mpl.rcParams['figure.figsize'] = (5, 5)
import matplotlib.cm as cm
In [24]:
import numpy as np
In [13]:
image = imread(file)
In [14]:
image
Out[14]:
In [134]:
plt.imshow(image, cmap=cm.Greys)
Out[134]:
In [135]:
edges = filter.canny(image, sigma=1)
In [136]:
plt.imshow(edges, cmap=cm.Greys)
Out[136]:
In [164]:
hough_radii = np.arange(15, 300, 5)
hough_res = hough_circle(edges, hough_radii)
In [165]:
hough_radii
Out[165]:
In [166]:
centers = []
accums = []
radii = []
for radius, h in zip(hough_radii, hough_res):
# For each radius, extract two circles
peaks = peak_local_max(h, num_peaks=2)
centers.extend(peaks)
accums.extend(h[peaks[:, 0], peaks[:, 1]])
radii.extend([radius, radius])
Now filter the results
In [171]:
best_centers = []
best_radii = []
best_x = []
best_y = []
number_of_best_circles = 10
for idx in np.argsort(accums)[::-1][:number_of_best_circles]:
center_x, center_y = centers[idx]
best_x.append(center_x)
best_y.append(center_y)
best_centers.append(centers[idx])
best_radii.append(radii[idx])
In [172]:
fig, ax = plt.subplots()
plt.imshow(image, cmap=plt.cm.gray)
for center, radius in zip(best_centers, best_radii):
circle = plt.Circle((center[1], center[0]), radius, color='r', fill=False)
ax.add_patch(circle)
plt.show()
In [173]:
print("Number of circles found: %s" % len(best_y))
print("Radii found: %s" % best_radii)
In [174]:
print("Calibrated Center X = %s +/- %s" % (np.average(best_x), np.std(best_x)))
print("Calibrated Center Y = %s +/- %s" % (np.average(best_y), np.std(best_y)))
In [ ]: