从信号图像 (RGB) 中移除背景 'noise'

removing background 'noise' from signal images (RGB)

我有一些信号图片:

如您所见,有些包含颜色信号,有些只是 gray/black 颜色信号。 我的任务是仅提取 具有白色背景 的纯信号。这意味着我需要删除图像中除信号以外的所有内容。

我检查过虚线、虚线、实线(顶部和底部)具有接近 0;0;0 的相同 RGB 值(例如:0;0;0, 2;2;2;或 8;8;8) 在 RGB 方面。

因此,我首先想到的是访问每个像素的 RGB 值,如果所有 RGB 值都相同,则分配白色。使用这种繁重的计算,我可以提取所有颜色信号,因为 RGB 值对于红色、蓝色、绿色(或它们在某种程度上的阴影)等颜色永远不会相同。

但是,该过程会删除信号像素值相同的信号。大多数黑色信号都会发生这种情况(例如前两个样本)。

我也想过提取信号,如果它保持水平和一些垂直连续性,但老实说我不知道​​如何为它编写代码。

我不要求任何代码解决这个挑战。 对于如何成功提取原始信号,我想有不同的看法。

我期待收到您的想法、见解和资源。谢谢

注意:我所有的图片(约3k)都在一个文件夹中,我将应用一种通用算法来完成任务。

您可以使用Hough transform找到水平线和垂直线。
找到线条后,擦除它们很简单。

去除线条只是第一步,但它看起来是一个很好的起点...
保留彩色像素(如您所建议的)也是一项简单的任务。

您提到您不要求任何代码解决方案,但我决定使用 MATLAB 代码演示我的建议:

close all
clear

origI = imread('I.png'); %Read image
I = imbinarize(rgb2gray(origI)); %Convert to binary
I = ~I; %Invert - the line color should be white.

%Apply hough transform: Find lines with angles very close to 0 degrees and with angles close to 90 degrees.
[H,theta,rho] = hough(I, 'RhoResolution', 1, 'Theta', [-0.3:0.02:0.3, -90:0.02:-89.7, 89.7:0.02:89.98]);
P = houghpeaks(H, numel(H), 'Threshold', 0.1, 'NHoodSize', [11, 1]); %Use low thresholds
lines = houghlines(I,theta,rho,P,'FillGap',25,'MinLength',200); %Fill large gaps and keep only the long lines.

%Plot the lines for debugging, and erase them by drawing black lines over them
J = im2uint8(I);
figure, imshow(I), hold on
for k = 1:length(lines)
   xy = [lines(k).point1; lines(k).point2];
   plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');

   % Plot beginnings and ends of lines
   plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
   plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');

   % Draw black line over each line.
   J = insertShape(J, 'Line', [xy(1,1), xy(1,2), xy(2,1), xy(2,2)], 'Color', 'Black');
end

%Covert J image to binary (because MATLAB function insertShape returns RGB output).
J = imbinarize(rgb2gray(J));
figure, imshow(J)

%Color mask: 1 where color is not black or white.
I = double(origI);
C = (abs(I(:,:,1) - I(:,:,2)) > 20) | (abs(I(:,:,1) - I(:,:,3)) > 20) | (abs(I(:,:,2) - I(:,:,3)) > 20);

figure, imshow(C)

%Build a mask that combines "lines" mask and "color" mask.
Mask = J | C;
Mask = cat(3, Mask, Mask, Mask);

%Put white color where mask value is 0.
K = origI;
K(~Mask) = 255;

figure, imshow(K)

检测到的行:

删除行后的结果:

最终结果:

如您所见,还有剩菜。
我对上述结果应用了第二次迭代(相同的代码)。

结果得到改善:

您可以尝试使用形态学操作去除剩余部分。
不擦除虚线图会很困难。


迭代所有 PNG 图像文件:

  • 将代码放在 m 文件(MATLAB 脚本文件)中。
  • m 文件放在 PNG 图像文件的同一文件夹中。

代码如下:

%ExtractSignals.m

close all
clear

%List all PNG files in the working directory (where ExtractSignals.m is placed).
imagefiles = dir('*.png');
nfiles = length(imagefiles);

result_images = cell(1, nfiles); %Allocate cell array for storing output images

for ii = 1:nfiles
    currentfilename = imagefiles(ii).name; %PNG file name

    origI = imread(currentfilename); %Read image

    %Verify origI is in RGB format (just in case...)
    if (size(origI, 3) ~= 3)
        error([currentfilename, ' is not RGB image format!']);
    end

    I = imbinarize(rgb2gray(origI)); %Convert to binary
    I = ~I; %Invert - the line color should be white.

    %Apply hough transform: Find lines with angles very close to 0 degrees and with angles close to 90 degrees.
    [H,theta,rho] = hough(I, 'RhoResolution', 1, 'Theta', [-0.3:0.02:0.3, -90:0.02:-89.7, 89.7:0.02:89.98]);
    P = houghpeaks(H, numel(H), 'Threshold', 0.1, 'NHoodSize', [11, 1]); %Use low thresholds
    lines = houghlines(I,theta,rho,P,'FillGap',25,'MinLength',200); %Fill large gaps and keep only the long lines.

    %Plot the lines for debugging, and erase them by drawing black lines over them
    J = im2uint8(I);
    %figure, imshow(I), hold on
    for k = 1:length(lines)
        xy = [lines(k).point1; lines(k).point2];
        %plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');

        % Plot beginnings and ends of lines
        %plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
        %plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');

        % Draw black line over each line.
        J = insertShape(J, 'Line', [xy(1,1), xy(1,2), xy(2,1), xy(2,2)], 'Color', 'Black');
    end

    %Covert J image to binary (because MATLAB function insertShape returns RGB output).
    J = imbinarize(rgb2gray(J));
    %figure, imshow(J)

    %Color mask: 1 where color is not black or white.
    I = double(origI);
    C = (abs(I(:,:,1) - I(:,:,2)) > 20) | (abs(I(:,:,1) - I(:,:,3)) > 20) | (abs(I(:,:,2) - I(:,:,3)) > 20);

    %figure, imshow(C)

    %Build a mask that combines "lines" mask and "color" mask.
    Mask = J | C;
    Mask = cat(3, Mask, Mask, Mask);

    %Put white color where mask value is 0.
    K = origI;
    K(~Mask) = 255;

    %figure, imshow(K)

    %Second iteration - applied by "copy and paste" of the above code (it is recommended to use a function instead).
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    origI = K; %Set origI to the result of the first iteration

    I = imbinarize(rgb2gray(origI)); %Convert to binary
    I = ~I; %Invert - the line color should be white.

    %Apply hough transform: Find lines with angles very close to 0 degrees and with angles close to 90 degrees.
    [H,theta,rho] = hough(I, 'RhoResolution', 1, 'Theta', [-0.3:0.02:0.3, -90:0.02:-89.7, 89.7:0.02:89.98]);
    P = houghpeaks(H, numel(H), 'Threshold', 0.1, 'NHoodSize', [11, 1]); %Use low thresholds
    lines = houghlines(I,theta,rho,P,'FillGap',25,'MinLength',200); %Fill large gaps and keep only the long lines.

    %Plot the lines for debugging, and erase them by drawing black lines over them
    J = im2uint8(I);
    %figure, imshow(I), hold on
    for k = 1:length(lines)
        xy = [lines(k).point1; lines(k).point2];
        % Draw black line over each line.
        J = insertShape(J, 'Line', [xy(1,1), xy(1,2), xy(2,1), xy(2,2)], 'Color', 'Black');
    end

    %Covert J image to binary (because MATLAB function insertShape returns RGB output).
    J = imbinarize(rgb2gray(J));
    %figure, imshow(J)

    %Color mask: 1 where color is not black or white.
    I = double(origI);
    C = (abs(I(:,:,1) - I(:,:,2)) > 20) | (abs(I(:,:,1) - I(:,:,3)) > 20) | (abs(I(:,:,2) - I(:,:,3)) > 20);

    %figure, imshow(C)

    %Build a mask that combines "lines" mask and "color" mask.
    Mask = J | C;
    Mask = cat(3, Mask, Mask, Mask);

    %Put white color where mask value is 0.
    K = origI;
    K(~Mask) = 255;
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    %Store result image in a cell array
    result_images{ii} = K;
end

%Display all result images
for ii = 1:nfiles
    figure;
    imshow(result_images{ii});
    title(['Processed ', imagefiles(ii).name]);
end