I have images with eight blue and eight red circles.
I want to find the center position ((x, y)
in pixels) of each circle and I also want to know whether the circle is red or blue.
The circles can have a slight variation in diameter.
It feels like this would not be so hard to solve, but it's to hard for me ...
I tried using OpenCV
and Template Matching by following a tutorial. It finds all the circles but I don't know how to pinpoint the center or pick the circle-color.
It also seems to draw a lot more than 1 rectangle per circle.
img_rgb = cv2.imread('images/img.png')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('red.png',0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.66
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 1)
I also gave Hough Circles
a try. But without any result at all. Maybe my circles are too small to be true circles, with the pixels showing.
Is Template Matching the right approach, even if I don't get it to work all the way, or can it be done more easily another way?
Any help would be much appreciated.
Thanks Martin