首页 > 解决方案 > 使用opencv在洗衣机上进行点检测

问题描述

我正在使用 opencv blob 检测功能来检测黑点,但它会导致速度慢和 CPU 消耗高。有没有更有效的方法来检测那些黑点?和 Blob 检测有时无法检测到一些黑点

这是我的示例图片

在此处输入图像描述

这是我现有的代码

SimpleBlobDetector::Params params;
params.minThreshold = 50;
params.maxThreshold = 200;
params.filterByArea = true;
params.minArea = 500;
params.filterByCircularity = true;
params.minCircularity = 0.1;
std::vector<KeyPoint> keypoints;
Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);
detector->detect( im, keypoints);
Mat im_with_keypoints;
drawKeypoints( im, keypoints, im_with_keypoints, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );

这些是尝试检测的黑点

在此处输入图像描述

标签: c++opencvcomputer-visionblobdiplib

解决方案


1-为了能够在 期间提高速度SimpleBlobDetector,您可以调整输入源的大小除以 2 或更多。这仍然有助于找到 blob,也将提高速度。

2-另一方面,为了获得精确的解决方案,您可以检测每个轮廓并在它们周围画圈。您可以使用半径过滤圆,也可以输入圆的内部来计算像素,过滤轮廓大小等。您可以继续使用形态学功能来完成任务。

这是指导和输出的代码:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;
RNG rng(12345);

int main()
{
    
    Mat img = imread("/ur/img/directory/image.png",0);
    imshow("Input",img);
    medianBlur(img,img,5);
    Mat canny_output;
    Canny( img, canny_output, 145, 145*3 );
    vector<vector<Point> > contours;
    findContours( canny_output, contours, RETR_TREE, CHAIN_APPROX_SIMPLE );
    vector<vector<Point> > contours_poly( contours.size() );
    vector<Point2f>centers( contours.size() );
    vector<float>radius( contours.size() );
    for( size_t i = 0; i < contours.size(); i++ )
    {
        approxPolyDP( contours[i], contours_poly[i], 3, true );
        minEnclosingCircle( contours_poly[i], centers[i], radius[i] );
    }
    Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
    for( size_t i = 0; i< contours.size(); i++ )
    {
        Scalar color = Scalar( 0,255,255);
        drawContours( drawing, contours_poly, (int)i, color );
        if((int)radius[i]>0 && (int)radius[i]<100)
            circle( img, centers[i], (int)radius[i], color, 2 );
    }
    imshow("Output",img);
    imshow("Contours",drawing);
    waitKey(0);
    return 0;
}

在此处输入图像描述


推荐阅读