• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

浅谈opencv自动光学检测、目标分割和检测(连通区域和findContours)

python 搞代码 4年前 (2022-01-09) 35次浏览 已收录 0个评论

步骤如下:

1.图片灰化;

2.中值滤波 去噪

3.求图片的光影(自动光学检测)

4.除法去光影

5.阈值操作

6.实现了三种目标检测方法

主要分两种连通区域和findContours

过程遇到了错误主要是图片忘了灰化处理,随机颜色的问题。下面代码都已经进行了解决

这是findContours的效果

下面是连通区域的结果

#include <opencv2\core\utility.hpp>

#include <opencv2\imgproc.hpp>
#include <opencv2\highgui.hpp>
#include<opencv2\opencv.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\core\matx.hpp>
#include<string>
#include <iostream>
#include <limits>
using namespace std;
using namespace cv;
Mat img = imread("C:\\Users\\hasee\\Desktop\\luosi.jpg",0);
Mat removeLight(Mat imge, Mat pattern, int method);
Mat calculateLightPattern(Mat img);
static Scalar randomColor(RNG& rng);

void ConnectedComponents(Mat img);
void ConnectedComponetsStats(Mat img);
void FindContoursBasic(Mat img);
void main()
{
Mat img_noise;
medianBlur(img,img_noise,3);
Mat pattern = calculateLightPattern(img_noise);

Mat re_light = removeLight(img_noise, pattern, 1);

Mat img_thr;
threshold(re_light,img_thr,30,255,THRESH_BINARY);

//ConnectedComponents(img_thr);
ConnectedComponetsStats(img_thr);
//FindContoursBasic(img_thr);
waitKey(0);

}
Mat removeLight(Mat imge, Mat pattern, int method) {
Mat aux;
if (method == 1) {
Mat img32, pattern32;
imge.convertTo(img32, CV_32F);
pattern.convertTo(pattern32, CV_32F);
aux = 1 - (img32 / pattern32);
aux = aux * 255;
aux.convertTo(aux, CV_8U);
}
else {
aux = pattern - imge;
}
return aux;
}

Mat calculateLightPattern(Mat img) {
Mat pattern;
blur(img, pattern, Size(img.cols / 3, img.cols / 3));
return pattern;
}
static Scalar randomColor(RNG& rng)
{
int icolor = (unsigned)rng;
return Scalar(icolor & 255, (icolor >> 8) & 255, (icolor >> 16) & 255);
}
void ConnectedComponents(Mat img) {
Mat lables;
int num_objects = connectedComponents(img, lables);

if (num_objects < 2) {
cout << "未检测到目标" << endl;
return;
}
else {
cout << "检测到的目标数量: " << num_objects - 1 << endl;
}
Mat output = Mat::zeros(img.rows,img.cols,CV_8UC3);
RNG rng(0xFFFFFFFF);

for (int i = 1; i < num_objects;i++) {
Mat mask = lables == i;
output.setTo(randomColor(rng),mask);
}
imshow("Result",output);
}

void ConnectedC<a style="color:transparent">本文来源gao($daima.com搞@代@#码(网5</a>omponetsStats(Mat img) {
Mat labels, stats, centroids;
int num_objects = connectedComponentsWithStats(img,labels,stats,centroids);
if (num_objects<2) {
cout << "未检测到目标" << endl;
return;
}
else {
cout << "检测到的目标数量: " << num_objects - 1 << endl;
}
Mat output = Mat::zeros(img.rows, img.cols, CV_8UC3);
RNG rng(0xFFFFFFFF);
for (int i = 1; i < num_objects; i++) {
Mat mask = labels == i;
output.setTo(randomColor(rng), mask);
stringstream ss;
ss << "area: " << stats.at<int>(i,CC_STAT_AREA);
putText(output,ss.str(), centroids.at<Point2d>(i),FONT_HERSHEY_SIMPLEX,0.4,Scalar(255,255,255));
}
imshow("Result", output);
}

void FindContoursBasic(Mat img) {
vector<vector<Point>> contours;
findContours(img, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
Mat output = Mat::zeros(img.rows, img.cols, CV_8UC3);
if (contours.size()==0) {
cout << "未检测到对象" << endl;
return;
}else{
cout << "检测到对象数量: " << contours.size() << endl;
}
RNG rng(0xFFFFFFFF);
for (int i = 0; i < contours.size(); i++)
drawContours(output,contours,i,randomColor(rng));
imshow("Result", output);
}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:浅谈opencv自动光学检测、目标分割和检测(连通区域和findContours)

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址