這篇文章主要講解了opencv自動光學(xué)檢測、目標(biāo)分割和檢測的詳細(xì)分析,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。

步驟如下:
1.圖片灰化;
2.中值濾波 去噪
3.求圖片的光影(自動光學(xué)檢測)
4.除法去光影
5.閾值操作
6.實(shí)現(xiàn)了三種目標(biāo)檢測方法
主要分兩種連通區(qū)域和findContours
過程遇到了錯誤主要是圖片忘了灰化處理,隨機(jī)顏色的問題。下面代碼都已經(jīng)進(jìn)行了解決
這是findContours的效果

下面是連通區(qū)域的結(jié)果

#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 << "未檢測到目標(biāo)" << endl;
return;
}
else {
cout << "檢測到的目標(biāo)數(shù)量: " << 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 ConnectedComponetsStats(Mat img) {
Mat labels, stats, centroids;
int num_objects = connectedComponentsWithStats(img,labels,stats,centroids);
if (num_objects<2) {
cout << "未檢測到目標(biāo)" << endl;
return;
}
else {
cout << "檢測到的目標(biāo)數(shù)量: " << 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 << "檢測到對象數(shù)量: " << contours.size() << endl;
}
RNG rng(0xFFFFFFFF);
for (int i = 0; i < contours.size(); i++)
drawContours(output,contours,i,randomColor(rng));
imshow("Result", output);
}另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文標(biāo)題:opencv自動光學(xué)檢測、目標(biāo)分割和檢測的詳細(xì)分析-創(chuàng)新互聯(lián)
新聞來源:http://chinadenli.net/article48/diihhp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、虛擬主機(jī)、網(wǎng)站營銷、App開發(fā)、電子商務(wù)、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容