以下是对使用工具OpenCV在C++中进行硬币检测的C++代码的说明。
要知道的事情:
- 该代码仅在Linux环境中编译。
- 要在Windows中运行,请使用文件:“ coin.o”并在cmd中运行它。但是,如果它没有运行(系统体系结构中的问题),则可以通过对代码进行适当且明显的更改在Windows中对其进行编译,例如:代替。
- 编译命令:g ++ -w coin.cpp -o coin.exe`pkg-config –libs opencv`
- 运行命令:./ coin
- 包含硬币的图像必须与代码位于同一目录中。
在运行代码之前,请确保已在//系统上安装了OpenCV。
代码片段说明:
#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
#include
// 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: "<
关于作者:
Aditya Prakash是瓦都达拉印度信息技术学院的一名本科生。他主要使用C++编写代码。他的座右铭是:到目前为止一切都很好。他打板球,看超级英雄电影,踢足球,并且是回答问题的忠实粉丝。
如果您还希望在此处展示您的博客,请参阅GBlog,以在GeeksforGeeks上撰写来宾博客。
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。