且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检测图像上的划痕,噪音很大

更新时间:2023-12-05 16:06:52

这是我的缺陷检测实现,它非常简单而有效的方法,我已经在MATLAB中实现了这个代码,但是在任何语言上都没有任何困难,因为它使用基本的图像处理操作。

This is my implementation for the defect detection, its a very simple yet effective approach, i have implemented this code in MATLAB, but there is not any difficulty to port it on any language because its use basic image processing operations.

clc

全部清除

全部关闭


  1. 读取图像和下采样它们(用于快速计算)系数为2。

im1 = imresize(imread('scratch.jpg '),0.5);



  1. 将它们转换为灰度。

gray = rgb2gray(im);



  1. 应用尺寸为15 X 15的高斯滤波器。

gSize = 15;

gray = imfilter(grey,fspecial('高斯',[gSize,gSize],gSize / 2),'复制');



  1. 使用Sobel掩码找出图像的渐变幅度。

[〜,〜,mg,〜] = ImageFeatures.Gradients(灰色);



  1. 阈值梯度幅度,阈值为最大值的30%。

`mgBw = mg> 0.3 * max(mg(:));

`mgBw = mg > 0.3*max(mg(:));



  1. 应用morpholgical操作关闭二进制图像通过3 X 3的磁盘掩码。

mgBw = imclose(mgBw,strel('disk', 1));



  1. 应用粒子分析(CCL)。

mgBw = bwareaopen(mgBw,500);



  1. 再次关闭连接线的图像。

mgBw = imclose(mgBw,strel('disk',2));



  1. 在图像中填充孔。

mgBw = imfill(mgBw,'holes' );



  1. 最终注释:


尝试上面的图片程序希望它能正常工作

Try Above procedure on your images hope it will work

谢谢

高斯蒙版的值在下面给出我刚刚复制的情况下,你只能使用小数点后4位的值和卷积前的一个更多的东西来缩放0到1之间的图像值:

Values For Gaussian Mask are Given below i have just copied as it is, you can only use values 4 places after decimal and one more thing before convolution scale your image values between 0 and 1:

         0.00253790859361804,0.00284879446220838,0.00314141610419987,0.00340305543986557,0.00362152753952273,0.00378611472031542,0.00388843599983945,0.00392315394879368,0.00388843599983945,0.00378611472031542,0.00362152753952273,0.00340305543986557,0.00314141610419987,0.00284879446220838,0.00253790859361804;
         0.00284879446220838,0.00319776287779517,0.00352622975612324,0.00381991909245893,0.00406515334132644,0.00424990193722614,0.00436475725361032,0.00440372804277458,0.00436475725361032,0.00424990193722614,0.00406515334132644,0.00381991909245893,0.00352622975612324,0.00319776287779517,0.00284879446220838;
         0.00314141610419987,0.00352622975612324,0.00388843599983945,0.00421229243210782,0.00448271658130972,0.00468644212981339,0.00481309512122034,0.00485606890058492,0.00481309512122034,0.00468644212981339,0.00448271658130972,0.00421229243210782,0.00388843599983945,0.00352622975612324,0.00314141610419987;
         0.00340305543986557,0.00381991909245893,0.00421229243210782,0.00456312191696750,0.00485606890058492,0.00507676215263394,0.00521396370030743,0.00526051663974220,0.00521396370030743,0.00507676215263394,0.00485606890058492,0.00456312191696750,0.00421229243210782,0.00381991909245893,0.00340305543986557;
         0.00362152753952273,0.00406515334132644,0.00448271658130972,0.00485606890058492,0.00516782273108746,0.00540268422664802,0.00554869395001131,0.00559823553262373,0.00554869395001131,0.00540268422664802,0.00516782273108746,0.00485606890058492,0.00448271658130972,0.00406515334132644,0.00362152753952273;
         0.00378611472031542,0.00424990193722614,0.00468644212981339,0.00507676215263394,0.00540268422664802,0.00564821944786971,0.00580086485975791,0.00585265795345929,0.00580086485975791,0.00564821944786971,0.00540268422664802,0.00507676215263394,0.00468644212981339,0.00424990193722614,0.00378611472031542;
         0.00388843599983945,0.00436475725361032,0.00481309512122034,0.00521396370030743,0.00554869395001131,0.00580086485975791,0.00595763557555571,0.00601082839853353,0.00595763557555571,0.00580086485975791,0.00554869395001131,0.00521396370030743,0.00481309512122034,0.00436475725361032,0.00388843599983945;
         0.00392315394879368,0.00440372804277458,0.00485606890058492,0.00526051663974220,0.00559823553262373,0.00585265795345929,0.00601082839853353,0.00606449615428972,0.00601082839853353,0.00585265795345929,0.00559823553262373,0.00526051663974220,0.00485606890058492,0.00440372804277458,0.00392315394879368;
         0.00388843599983945,0.00436475725361032,0.00481309512122034,0.00521396370030743,0.00554869395001131,0.00580086485975791,0.00595763557555571,0.00601082839853353,0.00595763557555571,0.00580086485975791,0.00554869395001131,0.00521396370030743,0.00481309512122034,0.00436475725361032,0.00388843599983945;
         0.00378611472031542,0.00424990193722614,0.00468644212981339,0.00507676215263394,0.00540268422664802,0.00564821944786971,0.00580086485975791,0.00585265795345929,0.00580086485975791,0.00564821944786971,0.00540268422664802,0.00507676215263394,0.00468644212981339,0.00424990193722614,0.00378611472031542;
         0.00362152753952273,0.00406515334132644,0.00448271658130972,0.00485606890058492,0.00516782273108746,0.00540268422664802,0.00554869395001131,0.00559823553262373,0.00554869395001131,0.00540268422664802,0.00516782273108746,0.00485606890058492,0.00448271658130972,0.00406515334132644,0.00362152753952273;
         0.00340305543986557,0.00381991909245893,0.00421229243210782,0.00456312191696750,0.00485606890058492,0.00507676215263394,0.00521396370030743,0.00526051663974220,0.00521396370030743,0.00507676215263394,0.00485606890058492,0.00456312191696750,0.00421229243210782,0.00381991909245893,0.00340305543986557;
         0.00314141610419987,0.00352622975612324,0.00388843599983945,0.00421229243210782,0.00448271658130972,0.00468644212981339,0.00481309512122034,0.00485606890058492,0.00481309512122034,0.00468644212981339,0.00448271658130972,0.00421229243210782,0.00388843599983945,0.00352622975612324,0.00314141610419987;
         0.00284879446220838,0.00319776287779517,0.00352622975612324,0.00381991909245893,0.00406515334132644,0.00424990193722614,0.00436475725361032,0.00440372804277458,0.00436475725361032,0.00424990193722614,0.00406515334132644,0.00381991909245893,0.00352622975612324,0.00319776287779517,0.00284879446220838;
         0.00253790859361804,0.00284879446220838,0.00314141610419987,0.00340305543986557,0.00362152753952273,0.00378611472031542,0.00388843599983945,0.00392315394879368,0.00388843599983945,0.00378611472031542,0.00362152753952273,0.00340305543986557,0.00314141610419987,0.00284879446220838,0.00253790859361804;

Sobel Mask:

Sobel Mask:

 1, 2, 1;
 0, 0, 0;
-1,-2, 1;

 1, 0,-1;
 2, 0,-2;
 1, 0,-1;

Sobel Gradient Magnitude Code(ImageFeatures.Gradient):

Sobel Gradient Magnitude Code(ImageFeatures.Gradient):

function [gx,gy,mag,phi] = Gradients(gray)
    gray = double(gray);
    horzmask = fspecial('sobel');
  %  vertmask = horzmask';

    gx = imfilter(gray,horzmask,'replicate');
    gy = imfilter(gray,horzmask','replicate');

    phi = (atan2((gy),(gx)));

    mag = mat2gray(sqrt(gx.^2+gy.^2));
end