...

OpenCV Coin Detection in Python with Canny and Contours

How to detect coins using Python and OpenCv contours 1

Introduction

This tutorial shows how to perform OpenCV coin detection in Python using Canny edge detection and contour detection.


You will load an image of coins, smooth it to reduce noise, extract edges with Canny, and find each coin with contours.


Then you will annotate every coin with its index and mark its center on the original image.
This approach is fast, reliable, and perfect for beginners who want a practical computer vision example.
The content and headers are optimized for the focus keyword OpenCV coin detection to help your post rank for hands-on queries.

You can find more similar tutorials in my blog posts page here : https://eranfeit.net/blog/

check out our video here : https://www.youtube.com/watch?v=_Coth4YESzk

Code Overview

The script reads an image, converts it to grayscale, and applies Gaussian blur to suppress noise.
Canny edge detection creates an edge map that highlights coin boundaries.
Contours are extracted from the edge map, sorted by area, and used to compute bounding boxes.
The approximate center of each coin is calculated and drawn with a circle and an index label.
The result is displayed in OpenCV windows and saved to disk for documentation or sharing

This is our test image :

OpenCV Coin Detection

Here is the code :

### Import OpenCV for image processing. import cv2  ### Read the source image of coins from disk. ### Replace the path with your own image if needed. img = cv2.imread("Open-CV/Coin-Edge-Detection/coins.jpg")  ### Convert the BGR image to grayscale to simplify processing. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  ### Smooth the image using Gaussian blur to reduce noise before edge detection. ### Kernel size (7,7) is odd and controls the amount of smoothing. blur = cv2.GaussianBlur(gray, (7, 7), 0)  ### Run Canny edge detection to highlight coin boundaries. ### The two thresholds control edge linking via hysteresis. canny = cv2.Canny(blur, 90, 255)  ### Find external contours from the edge map to isolate each coin. ### RETR_EXTERNAL returns only the outermost contours, which is ideal for separated coins. contours, hierarchy = cv2.findContours(canny.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  ### Sort contours by area so small coins are processed first and large ones last. sortedCon = sorted(contours, key=cv2.contourArea)  ### Loop over each contour and enumerate it for indexing. for i, cont in enumerate(sortedCon):     ### Compute an upright bounding rectangle around the contour to estimate location and size.     x, y, w, h = cv2.boundingRect(cont)      ### Optionally print the index for debugging or logging.     print(i)      ### Derive the approximate center of the coin from the rectangle midpoint.     X = x + int(w / 2)     Y = y + int(h / 2)      ### Draw a green circle around the estimated center to visualize detection.     img = cv2.circle(img, (X, Y), 50, (0, 255, 0), 2)      ### Overlay the coin index near the center using a readable Hershey font.     img = cv2.putText(         img=img,         text=str(i),         org=(X, Y),         fontFace=cv2.FONT_HERSHEY_DUPLEX,         fontScale=1.0,         color=(0, 255, 255),         thickness=2     )  ### Show the annotated original image in a window. cv2.imshow("original image", img)  ### Show the Canny edge map for insight into the detection pipeline. cv2.imshow("canny", canny)  ### Wait indefinitely for a key press so the windows stay open until you close them. cv2.waitKey(0)  ### Save the annotated result to disk as a PNG for sharing or documentation. cv2.imwrite("c:/temp/1.png", img)  ### Close all OpenCV windows and release GUI resources. cv2.destroyAllWindows() 

You can find the full Code here : https://ko-fi.com/s/856c283e25

The Result :

OpenCV Coin Detection

Connect :

☕ Buy me a coffee — https://ko-fi.com/eranfeit

🖥️ Email : feitgemel@gmail.com

🌐 https://eranfeit.net

🤝 Fiverr : https://www.fiverr.com/s/mB3Pbb

Enjoy,

Eran

error: Content is protected !!
Eran Feit