How can I remove straight lines from a Matlab image?

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

I would like to remove all 3 bottom straight horizontal lines from the image. I looked on Stackoverflow to use regionprops

to eliminate horizontal lines and got this code. But that doesn't seem to delete 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

+3


source to share


3 answers


Here is the answer using Hugh's transform approach. I'll add some more explanations to the following code:

% 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

      



Output:

find string

+2


source


You can look at the sums of the intensity of the lines. They will stand out as long as the lines remain horizontal.

grayI = rgb2gray(I);

rowSums = sum(grayI,2);

plot(rowSums);

      

The plot of the sum of lines



filterRows = rowSums > 1*10^5

I(filterRows,:,:) = 255;

      

enter image description here

+2


source


regionprops

a binary image is required, with each pixel being one of two values.

You can do for example

Two_dim(Two_dim<0.5) = 0;
Two_dim(Two_dim>=0.5) = 1; # the actual value doesn't matter

      

or

Two_dim = logical(Two_dim);

      

0


source







All Articles