代码之家  ›  专栏  ›  技术社区  ›  bit_scientist

从信号图像(RGB)中去除背景噪声

  •  0
  • bit_scientist  · 技术社区  · 5 年前

    我有一些信号图像: enter image description here

    如您所知,其中一些包含彩色信号,而另一些只是灰色/黑色信号。 只是。这意味着我需要删除图像中除信号以外的所有内容。

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

    但是,该过程将删除信号像素值相同的信号。这种情况主要发生在黑色信号上(例如前两个样本)。

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

    我期待着你的想法,见解和来源。谢谢

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

    0 回复  |  直到 5 年前
        1
  •  2
  •   Rotem    5 年前

    您可以使用 Hough transform .
    找到这些线后,就可以简单地将它们删除。

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

    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)
    

    检测到的行:
    Detected lines

    删除行后的结果:
    Result after deleting lines


    enter image description here

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


    enter image description here

    你可以试着用形态学的方法去除残羹剩饭。


    迭代所有PNG图像文件:

    • 将代码放入 m 文件(MATLAB脚本文件)。
    • 放置 文件位于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