# Removing lines of certain length from an image in MATLAB

I want to remove the portion of the line (highlighted by the yellow circles) where pixels form a straight vertical line segment.

If I use the strel

SE = strel('line', 5, 90); edgesR = imerode(In_R,SE);

then the remaining lines are also being erased.

Is there any other approach here?

• Think the other way: create a structuring element that preserves those lines, and removes everything else (you’ll need to use an opening, not an erosion). The difference between the input and the opened image is everything except those lines. So make your SE shorter and use imopen. – Cris Luengo Oct 20 '18 at 16:38

This is a program which removes the line segments in a single column of length which is more than the threshold

function [ret_im] = remove_vertical(in_image)

[NZ_r, NZ_c] = find(in_image);

ori = in_image;

%scan through each row of location of non-zero pixels
% If three (threshold) or more pixels are placed in a single column, turn them to zero
thresh = 3;
for i=1:1:size(NZ_r,1)-1

probe = NZ_c(i); % Set the initial search element
len = length(find(NZ_c(:)==probe));
if( len > thresh)
for j=1:1:len
if((NZ_r(i+1) - NZ_r(i)) == 1)
in_image(NZ_r(i),NZ_c(i)) = 0;
end
end
end
end

ret_im = in_image;

end