资源简介
基于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\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个文件信息
相关资源
- opencv3.0交叉编译用parallel.cpp
- 基于Qt5.8+OpenCV3.2的Basler多相机触发开
- OpenCV3.3+contrib-master,VS2013编译后的l
- 焊点缺陷检测必看
- 基于OpenCV3.0的手势识别.rar
- bumblebee双目标定/伪彩色深度图/鼠标测
- Ubuntu16.04中配置Qt5.9.1和OpenCV3.4.0过程
- 车道检测VS2015+OPENCV3.3
- Opencv3编程案例详解源码
- 胶囊缺陷检测算法
- opencv3.4.1+opencv_contrib经vs2015编译后生成
- OpenCV3.3最小二乘法直线拟合
- 支持OpenCV3.2的opencv_ffmpeg.7z
- OpenCV 3 Blueprints.pdf[382页可复制]
- vgg_generated_48(6480120).i
- 基于数字图像处理技术的缺陷检测算
- VS2015+Opencv310的X86动态库lib
- OpenCV3入门pdf
- opencv3.4.3 mingw32编译
- 基于Opencv3.0生成的dll
- opencv3 图片外接矩形框
- 车牌识别完整版 基于OPENCV3 完全实现
- opencv视觉导航小车寻迹
- opencv3.2交叉编译出来的库文件
- 安装opencv342缺失的boostdesc_bgm.i系列文
- opencv3.0 鱼眼标定校正 改进
- opencv3.3 自编译需要的xfeatures2d
- opencv3.2交叉编译移植笔记
- 电路板直线、圆、缺陷检测
- VS2015编译好的opencv3.4.1+contrib3.4.1
评论
共有 条评论