1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
fixed_size = tuple((32, 32))
img = cv2.imread("00000_00000_00000.png")
img = cv2.resize(img, fixed_size,interpolation = cv2.INTER_AREA)
org=img.copy()
gray_im = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Contrast adjusting with gamma correction y = 1.2
gray_correct = np.array(255 * (gray_im / 255) ** 0.3 , dtype='uint8')
# Contrast adjusting with histogramm equalization
gray_equ = cv2.equalizeHist(gray_correct)
edges = feature.canny(gray_equ, sigma=0.03, low_threshold=15, high_threshold=55)
hough_radii = np.arange(5, 55, 1)
hough_res = hough_circle(edges, hough_radii)
# Select the most prominent 3 circles
accums, cx, cy, radii = hough_circle_peaks(hough_res, hough_radii,
total_num_peaks=3)
index = list(radii).index(max(radii))
circy, circx = circle_perimeter(cy[index], cx[index], radii[index], shape=img.shape)
H, W = img.shape[:2]
x, y = np.meshgrid(np.arange(W), np.arange(H))
x, y = np.meshgrid(np.arange(W), np.arange(H))
# squared distance from the center of the circle
d2 = (x - cx[index])**2 + (y - cy[index])**2
# mask is True inside of the circle
mask = d2 > radii[index]**2
img[mask] = (0,0,0)
#img[circy, circx] = (220, 20, 20)
fig=plt.figure()
fig.add_subplot(1, 2, 1)
plt.imshow(org)
plt.axis('off')
plt.title("Originale")
fig.add_subplot(1, 2, 2)
plt.title("Segmentée")
plt.imshow(img)
plt.axis('off')
plt.show() |
Partager