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

时间:2015-10-20 02:54:56

标签: opencv image-processing computer-vision

我在检测这些图像上的划痕时遇到问题。实际上,人眼很容易看到。但是,在应用某些算法时,会产生很多噪音,我无法仅提取划痕。

以下是这些图片: enter image description here

enter image description here

目前,我尝试了一些滤波器(平滑,平均,中值,高斯滤波器或Sobel边缘检测器)来消除噪声并检测划痕,但它们并没有多大帮助。 你能提出一些想法吗? 我应该考虑哪些工具或算法?

3 个答案:

答案 0 :(得分:9)

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

clc

clear all

close all

  1. 读取两个图像并将其下采样(用于快速计算),系数为2。
  2. im1 = imresize(imread('scratch.jpg'),0.5);

    Scratch 1 enter image description here

    1. 将它们转换为灰度。
    2. gray = rgb2gray(im);

      GrayImage enter image description here

      1. 应用尺寸为15 X 15的高斯滤镜。
      2. gSize = 15;

        gray = imfilter(gray,fspecial('gaussian',[gSize,gSize],gSize/2),'replicate');

        enter image description here enter image description here

        1. 使用Sobel蒙版查找图像的渐变幅度。
        2. [~,~,mg,~] = ImageFeatures.Gradients(gray);

          enter image description here enter image description here

          1. 阈值梯度幅度,阈值为最大值的30百分位。
          2. `mgBw = mg> 0.3 * MAX(毫克(:));

            enter image description here enter image description here

            1. 应用morpholgical操作通过3 X 3的磁盘掩码关闭二进制图像。
            2. mgBw = imclose(mgBw,strel('disk',1));

              enter image description here enter image description here

              1. 应用粒子分析(CCL)。
              2. mgBw = bwareaopen(mgBw,500);

                enter image description here enter image description here

                1. 再次关闭一起加入Line的图片。
                2. mgBw = imclose(mgBw,strel('disk',2));

                  enter image description here enter image description here

                  1. 在图像中填充孔。
                  2. mgBw = imfill(mgBw,'holes');

                    enter image description here enter image description here

                    1. 最终注释:
                    2. enter image description here enter image description here

                      在图片上尝试以上程序希望它能够正常工作

                      谢谢

                      高斯蒙版的值在下面给出我刚刚复制的情况下,你只能使用小数点后4位的值和卷积前的另一个值,将图像值缩放到0和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:

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

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

                      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
                      

答案 1 :(得分:2)

我尝试了以下检测程序。输出看起来很温和,但我还是想分享。

  • 下采样彩色图像。
  • 应用具有不同窗口大小的中值模糊,然后采用绝对差异:我这样做是为了增强划痕并同时实现照明展平。下面显示的是以这种方式获得的差异图像。 sample difference image sample difference image 2

  • 使用基于高斯混合的背景/前景分割来分割差异图像中的划痕。这里的想法是,我们可以从这个图像和火车中提取m x n个窗口。由于划痕不会占据差异图像中的大面积,我们可以认为学习的背景应该接近划痕外的区域。对于差异图像,该方法比对差异图像应用阈值更好。当我直接输入下采样图像时,此方法无法正常工作。我认为这是由于区域中像素颜色值的不均匀性。所以我使用了照明平坦的差异图像。以下是分段图像。此过程很慢,因为它会检查图像中每个可能的m x n窗口。 segmented segmented 2

  • 使用概率Hough变换来检测分割图像中的线条。使用区域中的线密度或使用线条的形态滤波,我认为可以合理地猜测划痕的位置。 hough lones hough lines 2

这是代码

后台细分代码:

Mat threshold_mog(Mat& im, Size window)
{
    BackgroundSubtractorMOG2 bgModel;
    Mat fgMask;
    Mat output = Mat::ones(im.rows, im.cols, CV_8U);

    for (int r = 0; r < im.rows - window.height; r++)
    {
        for (int c = 0; c < im.cols - window.width; c++)
        {
            bgModel.operator()(im(Rect(c, r, window.width, window.height)), fgMask);
        }
    }

    for (int r = 0; r < im.rows - window.height; r++)
    {
        for (int c = 0; c < im.cols - window.width; c++)
        {
            Mat region = im(Rect(c, r, window.width, window.height));
        bgModel.operator()(region, fgMask, 0);
            fgMask.copyTo(output(Rect(c, r, window.width, window.height)));
        }
    }

    return output;
}

主:

Mat rgb = imread("scratch_2.png.jpg");

pyrDown(rgb, rgb);

Mat med, med2, dif, bw;

medianBlur(rgb, med, 3);
medianBlur(rgb, med2, 21);

absdiff(med2, med, dif);

bw = threshold_mog(dif, Size(15, 15));

Mat dst = bw.clone();
vector<Vec4i> lines;
HoughLinesP(dst, lines, 1, CV_PI/180, 8, 10, 20);
for( size_t i = 0; i < lines.size(); i++ )
{
    Vec4i l = lines[i];
    line(rgb, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 1, CV_AA);
}

答案 2 :(得分:0)

我正在遵循Ankit Dixit的程序来检测划痕并遇到一些问题。 首先,因为我使用C ++而不是MATLAB,所以我想检查是否有 对算法的误解。

目前,我只使用高斯滤波器5x5,因为它可用。 您选择窗口大小为15x15而不是5x5的原因是什么? 其次,我在索贝尔面具之后的形象似乎不如你的。 索贝尔在Matlab中有什么特殊/不同的东西吗?

以下是图片: 灰色: http://s15.postimg.org/q8s5y2sjf/Gray_Image.png 高斯滤波器5x5: http://s1.postimg.org/80r9x20tr/Gaussian_Filter_5x5.png 让索贝尔: http://s21.postimg.org/vxwyju58n/Sobel_Image.png