how to remove horizontal and vertical lines from image?
Show older comments
Hi, i'am working on hand written numbers OCR, to do so i need to segment each digit of the number (4 to 7 digits) and then feed these numbers to neural to classify number, every thing is ok but the segmentation due to the connection between box lines (that contain the number) and the number as in the image.

So, is there possible way to remove lines?
1 Comment
mutasam hazzah
on 20 Aug 2021
Accepted Answer
More Answers (3)
I'll throw in my own. This uses hough() and houghpeaks() to rectify the image so that rectangular filters will align with the box edges. The rest is just regular filtering. Parameters of interest are the binarization sensitivity, the filter widths and the areas specified in the bwareaopen() calls.
A more robust solution may make better use of hough() on a restricted angle range for better identification of box edges with less reliance on the filtering to segregate the box from the text. That's just an idea, not a recommendation with any certainty.
inpict = imread('ocrbox.jpg');
inpict = rgb2gray(cropborder(inpict,[NaN NaN NaN NaN])); % cropborder is from MIMT on FEX
inpict = ~imbinarize(inpict,'adaptive','foregroundpolarity','dark','sensitivity',0.15);
% find angle of dominant line, rectify
[H,T,R] = hough(inpict);
P = houghpeaks(H,1,'threshold',ceil(0.3*max(H(:))));
th0 = 90-T(P(1,2));
inpict = imrotate(double(inpict),-th0,'bilinear','crop');
w = 100;
linemask = medfilt2(inpict,[1 w]) + medfilt2(inpict,[w 1]);
linemask = bwareaopen(linemask>0.5,500);
linemask = imdilate(linemask,strel('disk',5));
inpict = bwareaopen(inpict & ~linemask,200);
imshow(inpict)

As the comment states, cropborder() is from MIMT on the File Exchange. That was only necessary because the provided image was a saved figure with unnecessary padding. Since you have the original image, you shouldn't need to crop anything.
Don't save images by saving the figure. Save the actual image instead of what amounts to a screenshot of the image.
Image Analyst
on 20 Aug 2021
0 votes
See
You might also try a morphological opening to erase the lines, though it might affect your numbers as well.
Or you could try hough() or houghlines() to see if you can find the lines and erase only along the lines.
An algorithm that might work on this particular image might fail on some other image.
Good luck.
1 Comment
mutasam hazzah
on 21 Aug 2021
Here is an attempt. Use bwlabel then to separate each region and analyze
I0 = imread('image.jpeg');
I1 = im2bw(I0,0.2);
I2 = imdilate(I1,ones(2)); %imopen(I1,ones(3));
imshow(I2)
1 Comment
mutasam hazzah
on 21 Aug 2021
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

