OpenCV C++ Program for coin detection
Last Updated :
28 Mar, 2023
The following is the explanation to the C++ code for coin detection in C++ using the tool OpenCV.
.jpg)
Things to know:
- The code will only compile in Linux environment.
- To run in windows, please use the file: ‘coin.o’ and run it in cmd. However if it does not run(problem in system architecture) then compile it in windows by making suitable and obvious changes to the code like: Use in place of .
- Compile command: g++ -w coin.cpp -o coin.exe `pkg-config –libs opencv`
- Run command: ./coin
- The image containing coin/coins has to be in the same directory as the code.
Before you run the code, please make sure that you have OpenCV installed on your // system.
Code Snippets Explained:
#include "opencv2/highgui/highgui.hpp"
// highgui - an interface to video and image capturing.
#include "opencv2/imgproc/imgproc.hpp"
// imgproc - An image processing module that for linear and non-linear
image filtering, geometrical image transformations, color space conversion and so on.
#include <iostream>
#include <stdio.h>
// The header files for performing input and output.
using namespace cv;
// Namespace where all the C++ OpenCV functionality resides.
using namespace std;
// For input output operations.
int main()
{
Mat image;
// Mat object is a basic image container. image is an object of Mat.
image=imread("coin-detection.jpg",CV_LOAD_IMAGE_GRAYSCALE);
// Take any image but make sure its in the same folder.
// first argument denotes the image to be loaded.
// second argument specifies the image format as follows:
// CV_LOAD_IMAGE_UNCHANGED (<0) loads the image as it is.
// CV_LOAD_IMAGE_GRAYSCALE ( 0) loads the image in Gray scale.
// CV_LOAD_IMAGE_COLOR (>0) loads the image in the BGR format.
// If the second argument is not there, it is implied CV_LOAD_IMAGE_COLOR.
vector coin;
// A vector data type to store the details of coins.
HoughCircles(image,coin,CV_HOUGH_GRADIENT,2,20,450,60,0,0 );
// Argument 1: Input image mode
// Argument 2: A vector that stores 3 values: x,y and r for each circle.
// Argument 3: CV_HOUGH_GRADIENT: Detection method.
// Argument 4: The inverse ratio of resolution.
// Argument 5: Minimum distance between centers.
// Argument 6: Upper threshold for Canny edge detector.
// Argument 7: Threshold for center detection.
// Argument 8: Minimum radius to be detected. Put zero as default
// Argument 9: Maximum radius to be detected. Put zero as default
int l=coin.size();
// Get the number of coins.
cout<<"\n The number of coins is: "<<l<<"\n\n";
// To draw the detected circles.
for( size_t i = 0; i < coin.size(); i++ )
{
Point center(cvRound(coin[i][0]),cvRound(coin[i][1]));
// Detect center
// cvRound: Rounds floating point number to nearest integer.
int radius=cvRound(coin[i][2]);
// To get the radius from the second argument of vector coin.
circle(image,center,3,Scalar(0,255,0),-1,8,0);
// circle center
// To get the circle outline.
circle(image,center,radius,Scalar(0,0,255),3,8,0);
// circle outline
cout<< " Center location for circle "<<i+1<<" :
"<<center<<"\n Diameter : "<<2*radius<<"\n";
}
cout<<"\n";
namedWindow("Coin Counter",CV_WINDOW_AUTOSIZE);
// Create a window called
//"A_good_name".
// first argument: name of the window.
// second argument: flag- types:
// WINDOW_NORMAL : The user can resize the window.
// WINDOW_AUTOSIZE : The window size is automatically adjusted to fit the
// displayed image() ), and you cannot change the window size manually.
// WINDOW_OPENGL : The window will be created with OpenGL support.
imshow("Coin Counter",image);
// first argument: name of the window
// second argument: image to be shown(Mat object)
waitKey(0); // Wait for infinite time for a key press.
Return 0; // Return from main function.
}
End of explanation.
About the Author:
Aditya Prakash is an undergraduate student at Indian Institute of Information Technology, Vadodara. He primarily codes in C++. The motto for him is: So far so good. He plays cricket, watches superhero movies, football and is a big fan of answering questions.
If you also wish to showcase your blog here, please see GBlog for guest blog writing on GeeksforGeeks.
Similar Reads
Opencv Python program for Face Detection
The objective of the program given is to detect object of interest(face) in real time and to keep tracking of the same object.This is a simple example of how to detect face in Python. You can try to use training samples of any other object of your choice to be detected by training the classifier on
2 min read
Blob Detection Using OpenCV
Blob detection is a basic method in computer vision used to locate areas of interest in a picture. These "blobs" frequently depict elements or items that have similar characteristics, like color, texture, or intensity. In this post, we explore the realm of blob identification with OpenCV, an effecti
5 min read
Circle Detection using OpenCV | Python
Circle detection finds a variety of uses in biomedical applications, ranging from iris detection to white blood cell segmentation. The technique followed is similar to the one used to detect lines, as discussed in this article. Basics of Circle Detection A circle can be described by the following eq
3 min read
Gun Detection using Python-OpenCV
Gun Detection using Object Detection is a helpful tool to have in your repository. It forms the backbone of many fantastic industrial applications. We can use this project for real threat detection in companies or organizations. Prerequisites: Python OpenCV OpenCV(Open Source Computer Vision Libra
4 min read
Real time object color detection using OpenCV
In this article, we will discuss how to detect a monochromatic colour object using python and OpenCV. Monochromatic color means light of a single wavelength. We will use the video, captured using a webcam as input and try to detect objects of a single color, especially Blue. But you can detect any c
4 min read
Object Detection with YOLO and OpenCV
Object Detection is a task of computer vision that helps to detect the objects in the image or video frame. It helps to recognize objects count the occurrences of them to keep records, etc. The objective of object detection is to identify and annotate each of the objects present in the media. YOLO(Y
6 min read
Face Detection using Python and OpenCV with webcam
Face detection is a important application of computer vision that involves identifying human faces in images or videos. In this Article, we will see how to build a simple real-time face detection application using Python and OpenCV where webcam will be used as the input source. Step 1: Installing Op
3 min read
Introduction to OpenCV
OpenCV is one of the most popular computer vision libraries. If you want to start your journey in the field of computer vision, then a thorough understanding of the concepts of OpenCV is of paramount importance. In this article, to understand the basic functionalities of Python OpenCV module, we wi
4 min read
White and black dot detection using OpenCV | Python
Image processing using Python is one of the hottest topics in today's world. But image processing is a bit complex and beginners get bored in their first approach. So in this article, we have a very basic image processing python program to count black dots in white surface and white dots in the blac
4 min read
Detect and Read Barcodes with OpenCV in Python
A barcode is a graphical representation of data that is machine-readable. It consists of parallel lines or rectangles of varying widths and spacings, along with specific patterns, that encode information. Barcodes are widely used for automatic identification and tracking of products, assets, invento
3 min read