Detecting coins using optical sensing

One of the common methods to detect a coin is using optical sensors such as cameras. We can identify various coin models. You can connect your camera to embedded systems such as Raspberry Pi. Then, you can perform image processing using OpenCV. We will try this on next. Please install OpenCV for your platform. Please read http://opencv.org for installation process.

For testing, we'll develop a Python application to detect coins. I'll use Euro coins for demonstration. The application will detect 1 euro. The easier method to detect coins is to implement template matching. We extract an image as a source for a template.

Then, we try to match that image:

To write a program, your platform should have OpenCV with Python bindings installed. Consider that we have a file, 1euro.jpg, for template matching source and coins.jpg file for testing.

Now we'll write our Python program. You can write this script:

import cv2 
import numpy as np 
from matplotlib import pyplot as plt 
 
img = cv2.imread('coins.JPG',0) 
img2 = img.copy() 
template = cv2.imread('1euro.JPG',0) 
img3 = template.copy() 
w, h = template.shape[::-1] 
 
# Apply template Matching 
res = cv2.matchTemplate(img,template,cv2.TM_CCOEFF) 
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) 
 
top_left = max_loc 
bottom_right = (top_left[0] + w, top_left[1] + h) 
cv2.rectangle(img,top_left, bottom_right, (0,255,255), 50) 
 
plt.subplot(221),plt.imshow(img2) 
plt.title('Original Picture') 
plt.subplot(222),plt.imshow(img3) 
plt.title('Template') 
plt.subplot(223),plt.imshow(img) 
plt.title('Detect 1 euro coin') 
plt.suptitle('Template matching using TM_CCOEFF method') 
 
plt.show() 

Save this script to a file called coindetect.py. After that, you can run this program by typing this command:

    $ python coindetect.py  

After executing the program, you should see a dialog that shows that a 1-euro coin is detected. You can see a sample program output here:

  1. Firstly, we load the template and tested files.
img = cv2.imread('coins.JPG',0) 
img2 = img.copy() 
template = cv2.imread('1euro.JPG',0) 
img3 = template.copy() 
w, h = template.shape[::-1] 
  1. Then, we apply our template matching method by calling matchTemplate() with the TM_CCOEFF parameter. For further information about this function, read the OpenCV documentation at http://docs.opencv.org/3.0-beta/doc/tutorials/imgproc/histograms/template_matching/template_matching.html:
res = cv2.matchTemplate(img,template,cv2.TM_CCOEFF) 
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) 
  1. If a match is found to the template file on the target image, we draw a rectangle on the image:
top_left = max_loc 
bottom_right = (top_left[0] + w, top_left[1] + h) 
cv2.rectangle(img,top_left, bottom_right, (0,255,255), 50) 
  1. Lastly, we plot all results of our computation:
plt.subplot(221),plt.imshow(img2) 
plt.title('Original Picture') 
plt.subplot(222),plt.imshow(img3) 
plt.title('Template') 
plt.subplot(223),plt.imshow(img) 
plt.title('Detect 1 euro coin') 
plt.suptitle('Template matching using TM_CCOEFF method') 
plt.show() 

For your experiments, you can modify parameters of matchTemplate() to get more accuracy.

The template-matching method has a disadvantage. If a coin is rotated, this method cannot detect it. You should rotate the coin image to fit your template source.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.159.150