从信号图像(RGB)去除背景“噪声” [英] removing background 'noise' from signal images (RGB)

查看:645
本文介绍了从信号图像(RGB)去除背景“噪声”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些信号图像:



其中一些包含颜色信号,有些只是灰色/黑色信号。
我的任务是仅提取具有白色背景的纯信号



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



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



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



我还考虑了在保持水平和垂直连续性的情况下提取信号。老实说,我不知道如何编写代码。



我没有要求任何代码解决方案来应对这一挑战。
我想对如何成功提取原始信号有不同的看法。



我期待着您的想法,见解和消息来源。谢谢



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

解决方案

您可以使用



删除行后的结果:



最终结果:



如您所见,仍然有剩余。

我对上述结果应用了第二次迭代(相同的代码)。



结果得到改善:



您可以尝试使用形态学运算删除残差。

如果不删除虚线图,将很困难。






迭代所有PNG图像文件:




  • 将代码放在 m 文件(MATLAB脚本文件)中。

  • m 文件放在PNG图像文件的同一文件夹中。



以下是代码:

 %ExtractSignals.m 

关闭所有
清除

%列出工作目录中的所有PNG文件(其中ExtractSignals.m放置)。
imagefiles = dir(’*。png’);
nfiles = length(imagefiles);

result_images = cell(1,nfiles); %分配用于存储输出图像

的单元格数组ii = 1:nfiles
currentfilename = imagefiles(ii).name; %PNG文件名

origI = imread(currentfilename); %读取图像

%验证origI是RGB格式(以防万一...)
if(size(origI,3)〜= 3)
错误([当前文件名,'不是RGB图像格式!']);
end

I = imbinarize(rgb2gray(origI)); %转换为二进制
I =〜I; %反转-线条颜色应为白色。

%应用霍夫变换:查找角度非常接近0度且角度接近90度的线。
[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]); %使用低阈值
lines = houghlines(I,theta,rho,P,'FillGap',25,'MinLength',200); %填补大的空白并仅保留长行。

%绘制要调试的线,并通过在其上绘制黑线来擦除它们。
J = im2uint8(I);
%figure,imshow(I),保持
为k = 1:长度(行)
xy = [lines(k).point1; lines(k).point2];
%plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');

%绘制行的起点和终点
%plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color', '黄色');
%plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');

%在每行上绘制黑色线条。
J = insertShape(J,'Line',[xy(1,1),xy(1,2),xy(2,1),xy(2,2)],'颜色','黑色');
end

%将J图像转换为二进制图像(因为MATLAB函数insertShape返回RGB输出)。
J = imbinarize(rgb2gray(J));
%figure,imshow(J)

%颜色遮罩:1,其中颜色不是黑色或白色。
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)

%构建一个结合线条蒙版和彩色蒙版的蒙版。
Mask = J | C;
Mask = cat(3,面具,面具,面具);

%输入白色,其中蒙版值为0。
K = origI;
K(〜Mask)= 255;

%figure,imshow(K)

%第二次迭代-通过上述代码的复制和粘贴应用(建议改用函数)。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
origI = K;将origI设置为第一次迭代的结果

I = imbinarize(rgb2gray(origI)); %转换为二进制
I =〜I; %反转-线条颜色应为白色。

%应用霍夫变换:查找角度非常接近0度且角度接近90度的线。
[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]); %使用低阈值
lines = houghlines(I,theta,rho,P,'FillGap',25,'MinLength',200); %填补大的空白并仅保留长行。

%绘制要调试的线,并通过在其上绘制黑线来擦除它们。
J = im2uint8(I);
%figure,imshow(I),保持
为k = 1:长度(行)
xy = [lines(k).point1; lines(k).point2];
%在每一行上画一条黑线。
J = insertShape(J,'Line',[xy(1,1),xy(1,2),xy(2,1),xy(2,2)],'颜色','黑色');
end

%将J图像转换为二进制图像(因为MATLAB函数insertShape返回RGB输出)。
J = imbinarize(rgb2gray(J));
%figure,imshow(J)

%颜色遮罩:1,其中颜色不是黑色或白色。
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)

%构建一个结合线条蒙版和彩色蒙版的蒙版。
Mask = J | C;
Mask = cat(3,面具,面具,面具);

%输入白色,其中蒙版值为0。
K = origI;
K(〜Mask)= 255;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%

%将结果图像存储在单元阵列
中result_images {ii} = K;
end

%显示ii = 1:nfiles
的所有结果图像
;
imshow(result_images {ii});
title([[已处理,imagefiles(ii).name]);
结束


I have some signal images:

As you can inspect, some of them contain color signals and some are just gray/black color signals. My task is to extract pure signal with white background only. That means I need to remove all but signal in the image.

I checked that dash lines, dotted lines, solid lines (top and bottom) have the same RGB value that are close to 0;0;0 (ex: 0;0;0, 2;2;2; or 8;8;8) in terms of RGB.

Therefore, first thing that came to my mind was to access RGB values of each pixel and assign white color if all RGB values are the same. Using this heavy computation I can extract all color signals, because RGB values are never same for colors like red, blue, green (or their shades to some extent).

However, that process would remove signals where signal's pixel values are the same. That happens with mostly black color signals (the first two samples for example).

I also thought of extracting the signal if it keeps its horizontal and some vertical continuity, but to be honest I don't know how to write the code for it.

I am not asking any code solution to this challenge. I would like to have different opinions on how I can successfully extract the original signal.

I am looking forward to having your ideas, insights and sources. Thanks

Note: All of my images (about 3k) are in one folder and I am going to apply one universal algorithm to accomplish the task.

解决方案

You can find the horizontal and vertical lines using Hough transform.
After finding the lines, it's simple to erase them.

Removing the lines is only the first stage, but it looks like a good starting point...
Keeping the colored pixels (as you suggested) is also simple task.

You have mentioned you are not asking any code solution, but I decided to demonstrate my suggestion using MATLAB code:

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:

Final result:

As you can see there are still leftovers.
I Applied a second iteration (same code) over the above result.

Result was improved:

You may try removing the leftovers using morphological operations.
It's going to be difficult without erasing the dashed graph.


Iterating all the PNG image files:

  • Place the code in an m file (MATLAB script file).
  • Place the m file in the same folder of the PNG image files.

Here is the code:

%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

这篇关于从信号图像(RGB)去除背景“噪声”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆