Our test identification algorithm is based on simple template matching. Basically, the template image of a desired object is convolved with the original image and the correlation between the two is found at every point. The correlation is then normalized with respect to the intensity of the original image, giving a correlation value in the range between -1 and 1. This process is encapsulated in the matlab function normxcorr2, whith takes two grayscale image matrices and returns one correlation matrix whose width and height are the sum of the widths and heights of the original matrices.
The program sets a threshold value (around .7 by experimentation) to determine if our template has matched a cup in the original image. Each color channel runs and is compared with the threshold separately. The program then ands the resulting filtered correlation matrices together so a match is only found if it matches in terms of red, green, and blue. This prevents a red (100% red, 0% green, 0% blue) from matching with white (100% red, 100% green, 100% blue). At this stage, all points that exceed the threshold are considered matches. Inorder to find the actual location of the cup the algorithm finds the maximum correlation overall, records a cup at that location, and then masks out the area of the found cup. This neutralizes the other over threshold points around corresponding to the same cup, preventing overlapping cup hits. The algorithm then finds the next greatest maximum value and repeats until all points over threshold have been accounted.
Unfortunately, this approach only works for one size of cup in the source image (the size of the template). To detect all cup sizes the scale of the template relative to the source image must change and the correlation must be run for each respective size. Our algorithm scales down the original image using imresize and leaves the template small (to save on runtime by reducing the correlation size instead of increasing it). After each small change in size the correlation function runs and saves matched regions to an accumulation array. The function also keeps track of the masks of previous match regions so smaller cups aren’t found erroneously inside of larger cups. The match regions are recorded at the scale of the original image, so the algorithm keeps track of the scale factor at each step and sizes the recorded region accordingly.




