Reputation: 11
I have an issue, and is that the Circle Hough Transform is trolling me. Basically, I want to do the Circle Hough transform on this binary image, so it can count the number of circles in the binary image.
However, after doing the Hough Transform, it gives me this image:
Here is the code:
import numpy as np
import cv2
from skimage import io, filters, measure
from skimage.filters import threshold_otsu, threshold_local
from scipy import ndimage
import matplotlib.pyplot as plt
import math
def Countcoins(img):
im2 = cv2.resize(img, (2000, 2000), interpolation = cv2.INTER_LINEAR)
b,g,r = cv2.split(im2)
r = cv2.GaussianBlur(r, (25,25), 0)
g = cv2.GaussianBlur(g, (25,25), 0)
b = cv2.GaussianBlur(b, (25,25), 0)
r = cv2.medianBlur(r, 15)
g = cv2.medianBlur(g, 15)
b = cv2.medianBlur(b, 15)
ret_red, coins_red = cv2.threshold(r, 0, 255, cv2.THRESH_OTSU)
ret_green, coins_green = cv2.threshold(g, 0, 255, cv2.THRESH_OTSU)
ret_blue, coins_blue = cv2.threshold(b, 0, 255, cv2.THRESH_OTSU)
image_merge = cv2.merge([coins_red, coins_green, coins_blue])
im_gray = cv2.cvtColor(image_merge, cv2.COLOR_BGR2GRAY)
im_gray = np.where(im_gray != im_gray[0,0], 255, 0)
im_gray = np.asarray(im_gray, dtype="uint8")*255
im_gray = cv2.morphologyEx(im_gray, cv2.MORPH_CLOSE, np.ones((6, 6), np.uint8), iterations=5)
plt.imshow(im_gray, cmap = 'grey')
plt.colorbar(shrink=0.8)
plt.show()
im_gray = np.asarray(im_gray, dtype="uint8")*255
circles = cv2.HoughCircles(im_gray,cv2.HOUGH_GRADIENT,1,250,param1 = 30, param2 = 5,minRadius=0,maxRadius=0)
circles = np.uint8(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(im2,(i[0],i[1]),i[2],(0,255,0),20)
# draw the center of the circle
cv2.circle(im2,(i[0],i[1]),2,(0,0,255),20)
print('Número de monedas: ' + str(len(circles[0,:])))
plt.imshow(im2)
im = cv2.imread('coins2.jpg')
Countcoins(im)
Upvotes: 1
Views: 34