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;
You can use any point to the right of that point along the same x-axis to delete the line.
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.
% Erases all lines from the axes control with handle "h".
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:
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);
filterRows = rowSums > 1*10^5
I(filterRows,:,:) = 255;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With