資源簡介
基于opencv3的劃痕缺陷檢測,分辨效率高,代碼清晰明了。

代碼片段和文件信息
#include??????
//#include???
#include?
//#include?
#include?
//#include?“opencv2/features2d.hpp“
using?namespace?cv;
using?namespace?std;
int?main(int?argc?char**?argv)
{
//cv::Mat?image?imagemean?diff?Mask;
Mat?image?imagemean?diff?Mask;
image?=?imread(“1.png“);
blur(image?imagemean?Size(13?13));?????????????//低通濾波blur平滑增強,抑制圖片邊界
subtract(imagemean?image?diff);?????????????????//圖像的減運算
threshold(diff?Mask?5?255?THRESH_BINARY_INV);//二值化,同動態閾值分割dyn_threshold
imshow(“image“?image);
imshow(“imagemean“?imagemean);
imshow(“diff“?diff);
imshow(“Mask“?Mask);
Mat?imagegray;
cvtColor(Mask?imagegray?CV_RGB2GRAY);???????//顏色空間轉換
vector?>?contours;??????????????//雙重向量
vector?hierarchy;
findContours(imagegray?contours?hierarchy?CV_RETR_TREE?CV_CHAIN_APPROX_SIMPLE?Point(0?0));????????????//檢測物體的輪廓
Mat?drawing?=?Mat::zeros(Mask.size()?CV_8U);
int?j?=?0;
for?(int?i?=?0;?i? {
Moments?moms?=?moments(Mat(contours[i]));
double?area?=?moms.m00;????//零階矩即為二值圖像的面積??double?area?=?moms.m00;??
//如果面積超出了設定的范圍,則不再考慮該斑點??
if?(area?>?10?&&?area?2000)?????????????????//劃痕面積范圍
{
drawContours(drawing?contours?i?Scalar(255)?FILLED?8?hierarchy?0?Point());
j?=?j?+?1;
}
}
cv::Mat?element15(3?3?CV_8U?cv::Scalar(1));
cv::Mat?close;
cv::morphologyEx(drawing?close?cv::MORPH_CLOSE?element15);??????//閉運算?,先膨脹后腐蝕的過程稱為閉運算。它具有填充物體內細小空洞,連接鄰近物體和平滑邊界的作用。
imshow(“drawing“?drawing);
vector?>?contours1;
vector?hierarchy1;
findContours(close?contours1?hierarchy1?CV_RETR_TREE?CV_CHAIN_APPROX_SIMPLE?Point(0?0));
imshow(“close“?close);
j?=?0;
int?m?=?0;
for?(int?i?=?0;?i? {
Moments?moms?=?moments(Mat(contours1[i]));????//moments()來計算圖像中的中心矩
double?area?=?moms.m00;????//零階矩即為二值圖像的面積??double?area?=?moms.m00;??
//如果面積超出了設定的范圍,則不再考慮該斑點??
double?area1?=?contourArea(contours1[i]);
if?(area?>?200?&&?area?2000)
{
drawContours(image?contours1?i?Scalar(0?0?255)?FILLED?8?hierarchy1?0?Point());//紅
j?=?j?+?1;
}
else?if?(area?>=?10?&&?area?<=?200)
{
drawContours(image?contours1?i?Scalar(255?0?0)?FILLED?8?hierarchy1?0?Point());??//?藍?????????????????B?G?R
m?=?m?+?1;
}
}
/*char?t[256];
sprintf_s(t?“%01d“?j);
string?s?=?t;
string?txt?=?“Long?NG?:?“?+?s;
putText(image?txt?Point(20?30)?CV_FONT_HERSHEY_COMPLEX?1
Scalar(0?0?255)?2?8);
sprintf_s(t?“%01d“?m);
s?=?t;
txt?=?“Short?NG?:?“?+?s;
putText(image?txt?Point(20?60)?CV_FONT_HERSHEY_COMPLEX?1
Scalar(255?0?0)?2?8);
*/
imshow(“漏洞“?image);
imwrite(“52.jpg“?close);
imwrite(“53.jpg“?image);
waitKey();
return?0;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-07-03?16:06??huahen\
?????目錄???????????0??2018-05-10?10:21??huahen\Debug\
?????文件???????96256??2018-06-28?13:21??huahen\Debug\huahen.exe
?????文件?????1051156??2018-06-28?13:21??huahen\Debug\huahen.ilk
?????文件?????1723392??2018-06-28?13:21??huahen\Debug\huahen.pdb
?????目錄???????????0??2018-06-28?13:21??huahen\huahen\
?????文件??????228462??2018-05-09?15:31??huahen\huahen\1.png
?????文件????????9270??2018-05-10?09:53??huahen\huahen\12.jpg
?????文件???????86799??2018-05-10?09:53??huahen\huahen\13.jpg
?????文件???????14762??2018-05-09?15:33??huahen\huahen\2.jpg
?????文件??????928123??2018-05-09?15:41??huahen\huahen\3.jpg
?????文件??????481363??2018-05-09?15:43??huahen\huahen\4.png
?????文件??????299815??2018-05-09?20:58??huahen\huahen\5.jpg
?????文件???????20875??2018-06-28?13:21??huahen\huahen\52.jpg
?????文件??????100775??2018-06-28?13:21??huahen\huahen\53.jpg
?????文件??????200057??2018-05-09?21:01??huahen\huahen\6.jpg
?????目錄???????????0??2018-06-28?13:21??huahen\huahen\Debug\
?????文件?????????549??2018-05-09?20:48??huahen\huahen\Debug\huahen.Build.CppClean.log
?????文件????????2169??2018-06-28?13:21??huahen\huahen\Debug\huahen.log
?????目錄???????????0??2018-06-28?13:21??huahen\huahen\Debug\huahen.tlog\
?????文件???????17446??2018-06-28?13:21??huahen\huahen\Debug\huahen.tlog\CL.read.1.tlog
?????文件?????????340??2018-06-28?13:21??huahen\huahen\Debug\huahen.tlog\CL.write.1.tlog
?????文件?????????580??2018-06-28?13:21??huahen\huahen\Debug\huahen.tlog\cl.command.1.tlog
?????文件?????????153??2018-06-28?13:21??huahen\huahen\Debug\huahen.tlog\huahen.lastbuildstate
?????文件????????1088??2018-06-28?13:21??huahen\huahen\Debug\huahen.tlog\li
?????文件????????2734??2018-06-28?13:21??huahen\huahen\Debug\huahen.tlog\li
?????文件?????????318??2018-06-28?13:21??huahen\huahen\Debug\huahen.tlog\li
?????文件??????317064??2018-06-28?13:21??huahen\huahen\Debug\main.obj
?????文件??????437248??2018-06-28?13:21??huahen\huahen\Debug\vc120.idb
?????文件??????733184??2018-06-28?13:21??huahen\huahen\Debug\vc120.pdb
?????文件????????4083??2018-05-09?15:32??huahen\huahen\huahen.vcxproj
............此處省略5個文件信息
評論
共有 條評論