Software for validating the correctness of coin shaped items leaving the production line.

A control systems engineer from Sweden hired me to implement a piece of software which would process photographs of disc shaped metal items to check if they were manufactured without errors.
The software needs to make sure that the 'coin' is circular in shape and has the correct diameter.
The piece of metal also has three bumps and the placement of those also needs to be checked and the software should be able to say if the 'coin' is flipped or not.

The input image is adaptively thresholded after analyzing the histogram of the luminance channel and then morphological close is performed to get rid of the small artifacts of thresholding.

Then, a distance transform is calculated to roughly locate the center of the 'coin'. Then a simple pattern match is performed to find the real center and to validate if the object is circular and has the desired diameter.

Then saturation channel is taken into account to locate the three bumps on the metal 'coin'.

And next, based on the gradient of saturation around the bumps, the direction of light (or shadow) is estimated and confronted with scene set up. If the calculated vector matches the direction of light from the fixed light source then the coin is marked as ok, otherwise it's considered flipped and needs to be flipped over.