我需要计算一个小金属杯中金属球的数量。
我尝试了模板匹配,但是它只显示了一个概率最高的结果。
但我需要可见的金属球总数。
由于背景也是金属的,因此我无法进行颜色阈值处理。
我尝试了一种使用模板匹配来查找第一个匹配项,然后用RGB(0,0,0)填充该区域,然后再次对该图像进行模板匹配的方法,但是发生了一些错误的检测。
我的主要要求是找到杯子中装满三个球的图像,并且不应检测除三个以外的任何其他数量。

请查看杯子内填充的不同数量的images

最佳答案

使用Hough圈子-有关此操作的信息,请参见OpenCV documentation。然后只计算凭经验确定的半径范围内的圆。

以下是一些结果和代码,可让您执行所需的操作:

#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>

using namespace std;
using namespace cv;

bool circle_compare (Vec3f i,Vec3f j) { return (i[2]>j[2]); }


int main(int argc, char** argv)
{

    /// Read the image
    Mat one = imread("one.jpg", 1 );
    Mat two = imread("two.jpg", 1 );
    Mat three = imread("three.jpg", 1 );
    Mat four = imread("four.jpg", 1 );
    if(!one.data  || !two.data  || !three.data  || !four.data)
    {
        return -1;
    }

    // put all the images into one
    Mat src(one.rows * 2, one.cols * 2, one.type());
    Rect roi1(0, 0, one.cols, one.rows);
    one.copyTo(src(roi1));
    Rect roi2(one.cols, 0, one.cols, one.rows);
    two.copyTo(src(roi2));
    Rect roi3(0, one.rows, one.cols, one.rows);
    three.copyTo(src(roi3));
    Rect roi4(one.cols, one.rows, one.cols, one.rows);
    four.copyTo(src(roi4));

    // extract the blue channel because the circles show up better there
    vector<cv::Mat> channels;
    cv::split(src, channels);
    cv::Mat blue;
    GaussianBlur( channels[0], blue, Size(7, 7), 4, 4 );

    vector<Vec3f> circles;
    vector<Vec3f> candidate_circles;

    /// Find the circles
    HoughCircles( blue, candidate_circles, CV_HOUGH_GRADIENT, 1, 1, 30, 55);//, 0, 200 );

    // sort candidate cirles by size, largest first
    // so the accepted circles are the largest that meet other criteria
    std::sort (candidate_circles.begin(), candidate_circles.end(), circle_compare);

    /// Draw the circles detected
    for( size_t i = 0; i < candidate_circles.size(); ++i )
    {
        Point center(cvRound(candidate_circles[i][0]), cvRound(candidate_circles[i][4]));
        int radius = cvRound(candidate_circles[i][5]);

        // skip over big circles
        if(radius > 35)
            continue;

        // test whether centre of candidate_circle is inside of accepted circle
        bool inside = false;
        for( size_t j = 0; j < circles.size(); ++j )
        {
            Point c(cvRound(circles[j][0]), cvRound(circles[j][6]));
            int r = cvRound(circles[j][7]);

            int d = sqrt((center.x - c.x) * (center.x - c.x) + (center.y - c.y) * (center.y - c.y));

            if(d <= r)
            {
                inside = true; // candidate is inside an existing circle
            }
        }
        if(inside)
            continue;

        // accept the current candidate circle then draw it
        circles.push_back(candidate_circles[i]);
        circle( src, center, 3, Scalar(0,255,0), -1, 8, 0 );
        circle( src, center, radius, Scalar(0,0,255), 3, 8, 0 );
    }

    // now fill the circles in the quadrant that has three balls
    vector<Vec3f> tl, tr, bl, br;

    for( size_t i = 0; i < circles.size(); ++i )
    {
        Point center(cvRound(circles[i][0]), cvRound(circles[i][8]));
        int radius = cvRound(circles[i][9]);

        if(center.x < one.cols)
        {
            if(center.y < one.rows)
            {
                tl.push_back(circles[i]);
            }
            else
            {
                bl.push_back(circles[i]);
            }
        }
        else
        {
            if(center.y < one.rows)
            {
                tr.push_back(circles[i]);
            }
            else
            {
                br.push_back(circles[i]);
            }
        }

        vector<vector<Vec3f>> all;
        all.push_back(tl);
        all.push_back(tr);
        all.push_back(bl);
        all.push_back(bl);
        for( size_t k = 0; k < all.size(); ++k )
        {
            if(all[k].size() == 3)
            {
                for( size_t i = 0; i < all[k].size(); ++i )
                {
                    Point center(cvRound(all[k][i][0]), cvRound(all[k][i][10]));
                    int radius = cvRound(all[k][i][11]);
                    circle( src, center, radius, Scalar(0,255, 255), -1, 4, 0 );
                }
            }
        }
    }

    // resize for easier display
    resize(src, src, one.size());

    /// Save results and display them
    imwrite("balls.png", src);
    //namedWindow( "Balls", CV_WINDOW_AUTOSIZE );
    imshow( "Balls", src );

    waitKey(0);
    return 0;
}

08-18 05:41