The following is the explanation to the C++ code for coin detection in C++ using the tool OpenCV.
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:
// highgui - an interface to video and image capturing.
// imgproc - An image processing module that for linear and non-linear
image filtering, geometrical image transformations, color space conversion and so on.
// 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.
// Mat object is a basic image container. image is an object of Mat.
// 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.
// A vector data type to store the details of coins.
// 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
// 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++ )
// Detect center
// cvRound: Rounds floating point number to nearest integer.
// To get the radius from the second argument of vector coin.
// circle center
// To get the circle outline.
// circle outline
cout<< " Center location for circle "<<i+1<<" :
"<<center<<"\n Diameter : "<<2*radius<<"\n";
// Create a window called
// 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.
// 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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!