A computer vision-based coin detection and counting system with both CLI and GUI interfaces. The application uses ORB (Oriented FAST and Rotated BRIEF) feature matching to identify coins and calculate their total value.
- Dual Interface: Command-line and graphical user interface
- Multiple Detection Methods:
- Hough Circle Transform
- Contour-based detection with multiple thresholding techniques
- Coin Registration: Register reference coins with front and back images
- Automatic Identification: Match detected circles to registered coin types
- Value Calculation: Automatically sum the total value of detected coins
- Visual Output:
- Annotated images with detected coins
- Pie chart showing coin distribution
- Adjustable Parameters: Fine-tune detection for different lighting and backgrounds
A number of demo coin pictures have been provided here for users' testing
Watch the project in action:
Demo.Usage.mp4
pip install opencv-python numpy matplotlib pillow- Python 3.7+
- OpenCV (cv2)
- NumPy
- Matplotlib
- Pillow (PIL) - GUI only
- tkinter - GUI only (usually comes with Python)
- Clone or download the repository
- Install required packages:
pip install opencv-python numpy matplotlib pillowpip install coin-counteror download the wheel file and
pip install coin_counter-2.0.0-py3-none-any.whloptions:
usage: coin-counter [-h] [--cli] [--gui]
coin-counter-gui
coin-counter-cli
-h, --help show how help message and exit
--cli Run CLI version {alternative: coin-counter-cli}
--gui Run GUI version {alternative: coin-counter-gui}i}Refer to the package site here
Run the graphical interface:
python coin_counter_gui.py-
Register Coins (Register Coin tab):
- Enter coin label (e.g., "Quarter", "Dime")
- Enter coin value in dollars (e.g., 0.25)
- Browse and select front image
- Browse and select back image
- Click "Register Coin"
-
Detect Coins (Detect Coins tab):
- Click "Load Scene Image" to load an image with multiple coins
- Choose detection method (Hough or Contour)
- Adjust parameters as needed
- Click "Detect Coins"
- View results and pie chart
- Save annotated image or pie chart
Example:
python coin_counter_cli.py register --label Quarter --value 0.25 \
--front [path_to_coin_front_side_image] --back [path_to_coin_back_side_image]]or, if you installed as a python package:
coin-counter-cli register --label Quarter --value 0.25 --front [path_to_coin_front_side_image] --back [path_to_coin_back_side_image]python coin_counter_cli.py detect --scene [path_to_scene_image] --method hough \
--min_radius 10 --max_radius 150 --param2 300or, if you installed as a python package:
coin-counter-cli detect --scene [path_to_coin_scene_image] --method hough --min_radius 10 --max_radius 150 --param2 30python coin_counter.py detect --scene [path_to_coin_scene_image] --method contour \
--min_area 1000 --max_area 30000 --circularity 0.66or, if you installed as a python package:
coin-counter-cli detect --scene [path_to_coin_scene_image] --method contour --min_area 1000 --max_area 30000 --circularity 0.6Best for: Clean backgrounds, well-separated coins, consistent lighting
Parameters:
--dp: Inverse ratio of accumulator resolution (default: 1.2)--min_dist: Minimum distance between circle centers (default: 30)--param1: Higher threshold for Canny edge detector (default: 100)--param2: Accumulator threshold for circle detection (default: 30)--min_radius: Minimum circle radius in pixels (default: 8)--max_radius: Maximum circle radius in pixels (default: 200)
Best for: Challenging backgrounds, overlapping coins, varied lighting
Parameters:
--min_area: Minimum contour area in pixels² (default: 500)--max_area: Maximum contour area in pixels² (default: 50000)--circularity: Circularity threshold 0-1 (default: 0.7)
Uses multiple thresholding methods:
- Otsu's method
- Inverted Otsu (for dark coins on light background)
- Adaptive thresholding
- Canny edge detection
--match_threshold: Minimum ORB feature matches required for identification (default: 8)- Increase for stricter matching (fewer false positives)
- Decrease for more lenient matching (better for poor quality images)
- Use good lighting (natural daylight works best)
- Plain, contrasting background
- Coin should fill most of the frame
- Image should be in focus
- Take separate images of front and back
- Place coins on a contrasting background
- Avoid overlapping coins when possible
- Ensure consistent lighting
- Keep camera parallel to the surface
- Use adequate resolution (coins should be at least 50-100 pixels in diameter)
If coins are not detected:
- Hough: Lower
param2, increasemax_radius - Contour: Lower
circularity, increasemax_area
If too many false detections:
- Hough: Increase
param2, decreasemax_radius - Contour: Increase
circularity, decreasemax_area - Increase
match_thresholdfor stricter identification
If coins are identified incorrectly:
- Lower
match_threshold(may cause more "Unknown" results) - Retake reference images with better quality
- Ensure scene image quality is good
- Annotated Image: Shows detected coins with labels and values
- Pie Chart: Visual distribution of coin types and total value
- Console Output: Detailed detection results and summary
- Works best with circular coins
- Requires good image quality
- May struggle with:
- Heavily worn or dirty coins
- Extreme lighting conditions
- Very small coins in large images
- Overlapping coins
- Non-uniform backgrounds
Problem: No coins detected
- Try both detection methods
- Adjust radius/area parameters to match actual coin sizes
- Check image quality and lighting
Problem: Many false detections
- Increase
param2(Hough) orcircularity(Contour) - Restrict size parameters more narrowly
- Use a cleaner background
Problem: Coins detected but labeled "Unknown"
- Lower
match_threshold - Improve reference image quality
- Ensure scene image is clear and well-lit
- Register more reference coins if needed
Problem: Wrong coin identification
- Increase
match_thresholdfor stricter matching - Retake reference images
- Ensure coins in scene are similar to reference images
- Circle Detection: Finds circular shapes using Hough or Contour methods
- Feature Extraction: Computes ORB features for each detected circle
- Feature Matching: Compares features against registered reference coins
- Identification: Matches detected coins to registered types based on feature similarity
- Aggregation: Counts coins and calculates total value
Feel free to submit issues or pull requests to improve the application.
Built using OpenCV's computer vision algorithms and ORB feature detection.