检测具有大量噪声的图像上的划痕

Posted

技术标签:

【中文标题】检测具有大量噪声的图像上的划痕【英文标题】:Detecting scratch on image with much noise 【发布时间】:2015-10-20 02:54:55 【问题描述】:

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

以下是这些图片:

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

【问题讨论】:

我只是看了你的图片,我不知道什么是划痕,什么是正常特征。三个平行的大斜线是划痕,还是左边斜线的小横线? 嗨,划痕是三个平行的大斜线。 使用重复帖子中的想法。重复的帖子走得更远,并删除了图像中的划痕。只需使用票数最高的答案的前半部分。 这绝对不是所列问题的重复。这个问题是关于自动检测划痕,另一个问题是关于去除划痕(当你知道它在哪里时)。我建议重新打开。 @DanivanderMeer 我的错。你是对的。我应该更好地阅读这篇文章。重新开放。 【参考方案1】:

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

clc

clear all

close all

    读取两个图像并将它们下采样(用于快速计算)2 倍。

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

    将它们转换为灰度。

gray = rgb2gray(im);

    应用大小为 15 X 15 的高斯滤波器。

gSize = 15;

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

    使用 Sobel 蒙版找出图像的梯度幅度。

[~,~,mg,~] = ImageFeatures.Gradients(gray);

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

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

    应用形态运算通过 3 X 3 的磁盘掩码关闭二进制图像。

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

    应用粒子分析 (CCL)。

mgBw = bwareaopen(mgBw,500);

    再次关闭图像以将线条连接在一起。

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

    填充图像中的孔。

mgBw = imfill(mgBw,'holes');

    最终注释:

在您的图像上尝试上述过程,希望它会起作用

谢谢

下面给出了高斯蒙版的值,我只是按原样复制的,您只能使用小数点后 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;

索贝尔面具:

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

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

Sobel 梯度幅度代码(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

【讨论】:

嗯,非常令人印象深刻!我将在 C++ 中尝试此过程,并在完成后发布结果。 参数太多.. 我很肯定它不适用于稍微不同的条件 试试吧老兄...在示例中只有两个图像...算法总是根据数据可用性设计的。 这种技术的来源是什么?我的意思是,你是如何/在哪里找到它的?任何文章/书籍/引文/任何东西? 经验是技术的来源:)【参考方案2】:

我尝试了以下检测程序。输出看起来适中,但我还是想到了分享。

对彩色图像进行下采样。

应用不同窗口大小的中值模糊,然后取绝对差异:我这样做是为了增强划痕,同时实现照明平坦化。下面显示的是通过这种方式获得的差异图像。

使用基于高斯混合的背景/前景分割来分割差异图像中的划痕。这里的想法是,我们可以从该图像中提取 m x n 个窗口并进行训练。由于划痕在差异图像中所占区域不大,我们可以认为学习到的背景应该近似于划痕之外的区域。这种方法对两个差异图像都比对差异图像应用阈值效果更好。当我直接输入下采样图像时,此方法效果不佳。我认为这是由于区域中像素颜色值的不均匀性。所以我使用了照明扁平化的差异图像。下面是分段的图像。这个过程很慢,因为它检查图像中每个可能的 m x n 窗口。

使用概率霍夫变换来检测分割图像中的线条。使用区域中的线密度或使用线的形态过滤,我认为可以对划痕的位置进行合理的猜测。

这是代码

背景分割代码:

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);

【讨论】:

谢谢大家。似乎已经足够好了。我现在正在写霍夫变换。 @anhnha 你能用 C++ 完成它吗?能发下源代码供参考吗? @user1220497 已经很久了,现在我在另一个领域工作

以上是关于检测具有大量噪声的图像上的划痕的主要内容,如果未能解决你的问题,请参考以下文章

检测图像中的噪声/伪影

OpenCV探索之路:图像修复技术

斑点检测

使用opencv去除噪声像素

验证码

MATLAB 图象去噪 边缘检测