Been trying algorithms that can detect the balls in the image.
So far, I have done multiple template matching with multiple templates. As I’ve understood, this method is likely not reliable (assumed that my object is not static and will likely move from time to time) and is a bit slow in performance because of the loops. And as what I’ve noticed, it can detect a detected object again as long as it matches from the existing templates which is another setback for me.
I have tried feature detections, like Haris and ORB, but I can’t seem to find the correct parameters that can make it to detect only the balls.
Lastly, I cannot use color masking as to the balls’ colors cannot be unique from it’s background.
Here’s my code for the template matching.
from imutils.object_detection import non_max_suppression
import numpy as np
import cv2
import glob
#empty list to store template images
templatesData = []
threshold = 0.8
#make a list of all templates images from a directory
templates = glob.glob('Templates/template*.png')
testImage = cv2.imread('scene8 .png')
testImageGray = cv2.cvtColor(testImage, cv2.COLOR_BGR2GRAY)
testImageBeforeNMS = testImage.copy()
testImageAfterNMS = testImage.copy()
for template in templates:
image = cv2.imread(template, 0)
templatesData.append(image)
for tmp in templatesData:
(tH, tW) = tmp.shape[:2]
result = cv2.matchTemplate(testImageGray, tmp, cv2.TM_CCOEFF_NORMED)
(yCoords, xCoords) = np.where(result >= threshold)
for (x,y) in zip(xCoords, yCoords):
#draw the bouding box on the image
cv2.rectangle(testImageBeforeNMS, (x,y), (x+tW, y+tH), (255,0,0), 3)
#initialize list of rectangles
rects = []
for (x,y) in zip(xCoords, yCoords):
#update our list of rectangles
rects.append((x,y, x+tW, y+tH))
pick = non_max_suppression(np.array(rects))
for(startX, startY, endX, endY) in pick:
#draw the bounding box of the image
cv2.rectangle(testImageAfterNMS, (startX, startY), (endX, endY), (255,0,0), 3)
cv2.imshow("Before NMS", testImageBeforeNMS)
cv2.imshow("After NMS", testImageAfterNMS)
cv2.waitKey(0)
I’m posting this one here hoping to get some insights on what and what not do. Thanks a lot!