• 大小: 6.81MB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2023-09-23
  • 语言: 其他
  • 标签: opencv3  缺陷检测  

资源简介

基于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 < contours.size(); 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 < contours1.size(); 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\link.command.1.tlog
     文件        2734  2018-06-28 13:21  huahen\huahen\Debug\huahen.tlog\link.read.1.tlog
     文件         318  2018-06-28 13:21  huahen\huahen\Debug\huahen.tlog\link.write.1.tlog
     文件      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个文件信息

评论

共有 条评论