Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove straight lines from a Matlab image?

I have a matlab image from a matrix named Two_dim as shown in the figure below.

I would like to remove all 3 of the bottom straight horizontal lines from the image. I looked up on Stackoverflow to use regionprops to eliminate horizontal lines and obtained this code. But that doesn't seem to remove the lines.

rp = regionprops(Two_dim, 'PixelIdxList', 'Eccentricity', 'Orientation');
rp = rp([rp.Eccentricity]>0.95 & (abs([rp.Orientation])<2 | abs([rp.Orientation])>89));
Two_dim(vertcat(rp.PixelIdxList)) = false;

Noisy Image

like image 917
Sack11 Avatar asked Jul 09 '17 10:07

Sack11


People also ask

Can you delete lines in MATLAB?

You can use any point to the right of that point along the same x-axis to delete the line.

How do I get rid of vertical lines in photos?

One approach is to rotate the image 90 deg, then apply your same code, then rotate back 90 deg. Another approach is to change your horizontal kernel to a vertical kernel in your morphology.

How do I delete all lines in MATLAB?

% Erases all lines from the axes control with handle "h".


2 Answers

Here is an answer using Hough transform approach. I will add some more explanations to the code below later:

% I crop only the intresting part for illustration:
BW = edge(Two_dim(1:1000,:),'canny');
subplot 131
imagesc(Two_dim(1:1000,:))
title('Original image')
axis xy
[H,theta,rho] = hough(BW); % preform Hough transform
subplot 132
P = houghpeaks(H,10,'NHoodSize',[1 1]); % find the peaks in the transformation
lines_found = houghlines(BW,theta,rho,P,...
    'FillGap',50,'MinLength',1); % convert the peaks to line objects
imagesc(Two_dim(1:1000,:)), hold on
result = Two_dim(1:1000,:);
for k = 1:length(lines_found)
   % extract one line:
   xy = [lines_found(k).point1; lines_found(k).point2];
   % Plot the detected lines:
   plot(xy(:,1),xy(:,2),'LineWidth',1,'Color','green');
   % remove the lines from the image:
   % note that I take a buffer of 3 to the 'width' of the line
   result(xy(1,2):xy(1,2)+3,xy(1,1):xy(2,1)) = 0;
end
title('Detected lines')
axis xy
subplot 133
imagesc(result)
title('Corrected image')
axis xy

The output:

find a line

like image 105
EBH Avatar answered Oct 13 '22 21:10

EBH


You could look at the row intensity sums. They will stand out as long as the lines stay horizontal.

grayI = rgb2gray(I);

rowSums = sum(grayI,2);

plot(rowSums);

Plot of row sums

filterRows = rowSums > 1*10^5

I(filterRows,:,:) = 255;

enter image description here

like image 31
Tapio Avatar answered Oct 13 '22 22:10

Tapio