Discovering OpenCV with Python: Coin Amount Calculation
Tags: opencv, python, imageanalysis, computervision
Published: 2020-08-10 22:12:01

Since I already explored coin detection, I decided to take the real-life application of OpenCV one step further. Now that I can find the coins, naturally, the next step would be to correctly identify the coins and subsequently calculate their amount. 

## Detect coins
As previously explored in a separate article [here](https://dev.to/tinazhouhui/coin-detection-discovering-opencv-with-python-1ka1), detecting the coins is the first step. I used the Hough Circle Transformation to find these and therefore had the radius of each coin and the coordinates of the center. The logic behind the value identification lies in the radii of each coin as we have only visual information, therefore the precision of the circles drawn around the coins needed to be high.


## Identify coins
Since each picture can be taken from a different height, we cannot directly translate the number of pixels to millimetres. Therefore the identification of coins had to be relative based on their radii. 

For example, The smallest coin (1 CZK) has a radius of 20 mm and the second to smallest coin (2 CZK) has a radius of 21.5 mm. Therefore, the 2 CZK coin’s radius is 1.075 times larger than the radius of the 1 CZK coin. Amount of pixels representing the radii in the picture must, therefore, also follow the same ratio. This logic works with any coin, but you have to let the program know, which coin is the base coin that you derive the ratios from. In my case, it was easiest to say that the smallest coin on the picture represents the smallest coin in real life so each analysed image had to have at least one 1 CZK coin. 

From there, I just created a dictionary of all important information related to each coin - name, value, count, radius, ratio-to-smallest-coin and ran a for cycle for each of the coins, or to be precise, circles found by the Hough circle Transformation.

Now, all that was left was that each time a coin is identified, the value is written to the center of the coin and added to the total value variable. After running through all the coins, the total amount is calculated. As usual, all work can be [found here, on GitHub](https://github.com/tinazhouhui/computer_vision/blob/master/image_analysis/coin_amount_calculate.py).
 
## Let’s test it!
First I tested the program on the most nicely scanned coins that the internet could offer and found this picture. After some tweaking with the parameters in Hough Transformation, I got this result:
![coint_amount](https://dev-to-uploads.s3.amazonaws.com/i/nkjkwlgqnst8uxem38f4.jpg)
```
The total amount is 88 CZK
1 CZK = 1x
2 CZK = 1x
5 CZK = 1x
10 CZK = 1x
20 CZK = 1x
50 CZK = 1x
```
This gave me the confidence to try to test it on a picture of coins that I took at home. I thought, just as with the picture above, that few tries with the parameters would result in the correct amount. 

Unfortunately, that was not the case. See, the biggest problem was that Hough could not detect the circles well enough to fit the ratio. It either made the circle too small and hence thought it was smaller value or the opposite or did not detect them at all. I tried changing the background from white to black, different distance to take the picture, different lighting and still nothing.  It took me days of trying almost every combination of parameters to realise that this was not the way. The secret lies in preprocessing the image. 

>The most important realization was that the output could be only as good as the input data that we are providing. 

Logically, by increasing the quality of input data, the quality (here it would be precision of circles drawn) would also increase. Consequently, making sure that the picture quality was high enough and [stretching the gamma](https://dev.to/tinazhouhui/discovering-opencv-with-python-gamma-correction-3cnh) to bring out the contrast helped Hough to better detect the edges. To some degree, the previous improvements like black background helped as well. Then suddenly, few tweaks later, success!

Here is the final output:
![coin amount](https://dev-to-uploads.s3.amazonaws.com/i/v80lk9gv1m8iguvcrxuh.jpg)
```
The total amount is 151 CZK
1 CZK = 4x
2 CZK = 1x
5 CZK = 5x
10 CZK = 2x
20 CZK = 5x
50 CZK = 0x
```
This project again represents the accumulation of previous knowledge but is more valuable for me as it has a tangible real-life application that I could test at home. The biggest lesson learnt was to think about why the program is not performing well (bad preprocessing of image) rather than just trying to change various parameters of the Hough Circle Transformation and hope for the best. As always, may the Python be with you.