如何查找此图像中的圆圈数或对象数

时间:2018-06-08 07:54:30

标签: opencv image-processing

Image from which I want to find out number of objects/circles.

我想使用OpenCV和C ++找到此图像中的圆/结节总数。

我已经写了这段代码:

src = imread("src.bmp");   //src is source image 
cvtColor(src, src_gray, CV_BGR2GRAY);   // Gray scale convert
Mat bw = src_gray > 128;
imshow("Gray Scaled Source",src_gray);  

Canny(src_gray, canny_output, thresh, thresh * 2, 3); /// Detect edges using canny


/// Find contours
findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

/// Draw contours
drawing = Mat::zeros(canny_output.size(), CV_8UC3);

cout<<contours.size();   // total no of contours

通过这段代码我的答案是:117

但正确的答案是:62

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码从给定图像中提取圆/对象

Mat src, src_gray,binarySeg;
vector< vector <Point> > contours;
vector <Vec4i> hierarchy;

src = imread("src.bmp");
cvtColor(src, src_gray, CV_BGR2GRAY);
threshold(src_gray,binarySeg,75,255,CV_THRESH_BINARY);
morphologyEx(binarySeg,binarySeg,MORPH_DILATE,Mat::ones(3,3,CV_32F),Point(-1,-1),1);

findContours(binarySeg, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
cout<<contours.size(); 

这里发生的是,您首先根据您给出的值对图像进行阈值处理。然后你做一个扩大操作来删除小区域。之后你会找到轮廓的数量。