c++ - 使用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 );
这些是尝试检测的黑点
解决方案
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;
}
推荐阅读
- javascript - 如何在javascript中组合多个数组?
- python-3.x - 从 AWS Lambda 上的 S3 读取文件时出现 IncompleteReadError
- python-sphinx - 狮身人面像在 TOC 中不显示本地标题
- vue.js - 如何将我的数据存储在自定义 Vue 组件而不是根 Vue 实例中?
- java - 如何保护球衣服务器上的 xml 请求?
- syntax-highlighting - 如何在 atom 中为特定文件设置语法语言?
- excel - 如何创建将记录插入空单元格的 SSIS 包
- visual-studio-code - 在 VSCode 中对 Arduino 进行编程,并统一了多个档案
- excel - 循环遍历 Excel 单元格
- r - 如何组合每个代表不同响应值的数据框列?