remove all lines from page (mainly horizontal)

Hello community,
i wish to remove horizontal and vertical lines from page (view here full picture), using the Image process toolbox.
i tried turning the pic into B&W and then the Hough method but it didnt really worked for me.
here's the insufficient result
any idea how to implament this ?
TIA :)

 Accepted Answer

Do you need to process this one image, or many varied images?
If all you need is one image, there's little point in overcomplicating it.
A = imread('circ.jpg');
A = rgb2gray(A);
B = A<75;
B(:,860:end) = false;
B(1300:end,:) = false;
imshow(B)

4 Comments

Great thing! thanks !
if i want to multiple files ? what's the implementation ?
also can you please explain what's the role of 4th 5th and 6th lines?
Thank you again
This is a basic thresholding operation to separate the text from the background. It doesn't remove other dark regions like the ring holes or margin line. I just removed those directly by clearing a rectangular region of the output.
%B = A<75; % simple thresholding operation to isolate dark objects
%B(:,860:end) = false; % manually get rid of holes on RHS of image
%B(1300:end,:) = false; % manually get rid of page corner on bottom left
To try doing this automatically would require knowing the similarities and differences between the images. I doubt that trying to use houghlines() is going to be much of a help. If the grid lines were parallel or straight in the image, then you could use it for orienting a filter, but they aren't parallel, and they aren't really straight either.
You might be able to use it to get rid of the margin line if you can rely on the margin line being the dominant line. You might be able to use that information to get rid of the holes if you can rely on there being no text in the margin. Consider the example:
A = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/885360/circ.jpg');
A = rgb2gray(A);
B = A<75; % threshold
B = imclearborder(B); % remove blobs that touch image edges
% display image
imshow(B)
hold on
% find angle of dominant line
[H,T,R] = hough(B);
P = houghpeaks(H,1,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(B,T,R,P,'FillGap',50,'MinLength',7);
% project dominant line beyond image boundaries
% calculate 4 points defining a box with side length
% equal to the image diagonal
sz = size(B);
r = lines(1).rho;
th = -lines(1).theta;
a = th-90;
R = sqrt(sz(1)^2 + sz(2)^2); % image diagonal
p1 = [r*cosd(th) -r*sind(th)]; % NW corner
p2 = [R*cosd(a) -R*sind(a)] + p1; % SW corner
pos = [R*cosd(th) -R*sind(th)]; % offset vector for SE and NE corners
% create polygon ROI, convert to mask
h1 = images.roi.Polygon(gca,'position',[p1; p2; p2+pos; p1+pos]);
The ROI won't actually be visible when running, but for sake of clarity, this is what the ROI would look like:
continuing ...
mask = createMask(h1); % create a binary mask from the ROI
delete(h1) % delete ROI object
% dilate mask
mask = imdilate(mask,strel('disk',10));
% apply mask
B(mask) = false;
% show trimmed image
imshow(B)
This method might be a bit more automatic, but it still relies heavily on the assumption that the dominant line found by houghpeaks is the right one. That's not a general solution.
Rather than that stuff to get the ROI mask, you can simply erae the right-most 10% of the image
column = round(0.9 * size(B, 2)); % Find column 90% of the way across the image.
B(:, column:end) = false; % Erase from that point to the right.
That's basically what the first example did. The second example was only an elaboration based on the indication that a somewhat responsive solution was desired. OP mentioned trying to do it by application of Hough transform tools, so I provided one example.
In either case, the goal was to remove both the edge holes and the margin line, so a wider selection would be required. Considering the likelihood of rotation or perspective effects, it's easy to suspect that a rectangular selection including the margin line will end up running into text in some other yet-unseen image example.
I suppose a lower-impact approach would be simply to use houghlines() to create a line ROI, dilate it a bit, and use that to remove the margin line, then using other methods to remove the edge holes. I figured that would be over-overcomplicating it.
EDIT:
Re: over-overcomplicating it:
A = imread('circ.jpg');
A = rgb2gray(A);
B = A<75; % threshold
B = imclearborder(B); % remove blobs that touch image edges
% display image
imshow(B)
hold on
% find angle of dominant line
[H,T,R] = hough(B);
P = houghpeaks(H,1,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(B,T,R,P,'FillGap',50,'MinLength',7)
% project line beyond image boundaries
sz = size(B);
r = lines(1).rho;
th = -lines(1).theta;
a = th-90;
R = sqrt(sz(1)^2 + sz(2)^2);
p1 = [r*cosd(th) -r*sind(th)];
p2 = [R*cosd(a) -R*sind(a)] + p1;
pos = [R*cosd(th) -R*sind(th)];
% create line ROI, convert to mask
% this only selects the margin line itself
h1 = images.roi.Line(gca,'position',[p1; p2]);
lmask = createMask(h1);
delete(h1) % delete ROI object
% create polygon ROI, convert to mask
% this selects the entire area between the margin line and the page edge
h1 = images.roi.Polygon(gca,'position',[p1; p2; p2+pos; p1+pos]);
pmask = createMask(h1);
delete(h1) % delete ROI object
% dilate line mask
lmask = imdilate(lmask,strel('disk',10));
% apply mask
B(lmask) = false;
% remove remaining holes
B = bwareaopen(B,10);
B1 = bwpropfilt(B,'solidity',[0.7 1]); % select relatively solid blobs
B2 = bwpropfilt(B,'area',[200 300]); % select blobs with an approx area
% remove blobs which match those two constraints and are within margin area
B = B & ~(B1 & B2 & pmask);
% show trimmed image
imshow(B)
Of course, assuming an area/solidity intersection is a risky bet, so this only uses the polygonal ROI to make sure the prop filters don't apply to the inner-page region. This method would avoid deleting text within the margin region.

Sign in to comment.

More Answers (0)

Products

Release

R2020a

Asked:

on 5 Feb 2022

Edited:

DGM
on 6 Feb 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!