Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove lines of particular size from image

Tags:

imagemagick

I have used the following imagemagick command for the image below:

convert img.png -define morphology:compose=darken -morphology Thinning Rectangle:17x1+0+0\< tmp.png

This removes ALL lines from the image, but I just want to remove the small horizontal and vertical lines on the right and bottom of the number in top left corner of each block. I want to preserve the main column and row lines. Can anyone tell me how to do it? enter image description here

This is what I get (notice the long lines dividing the image content into columns and rows are also gone. I want those line to stay):enter image description here

like image 268
Naveed Avatar asked Jan 18 '26 10:01

Naveed


1 Answers

I notice the script is finer/thinner and less regular than the lines so it is more susceptible to eroding techniques. With that in mind we can ditch the text like this:

convert vcards.png -colorspace gray -threshold 50% -morphology erode disk:1.5 +repage z1.png

enter image description here

That's a good start, but if we use that as a mask, we will lose the long horizontal lines in your original image. So, we can find all those by projecting all the rows into a single-pixel wide tall column and thresholding all the rows that are more than 80% white. Then widen the image back out to its original width.

convert z1.png -colorspace gray -resize 1x\! +repage -threshold 80% -scale 810x1518\! +repage z2.png

enter image description here

Now combine the two masks so they only do the lower and right sides of your little title box things.

convert z1.png \( z2.png -negate \) -compose darken -composite z3.png

enter image description here

Finally, fatten that mask up a bit because it may have shifted around during previous processing, and apply it to your original image.

convert vcards.png \( z3.png -morphology dilate disk:2 -negate \) -compose darken -composite result.png

enter image description here

It could all be combined into a single command, but I won't do that, because some aspects may not work for all your images and while they are all individually implemented and documented, they are simpler to improve or correct individually.

like image 147
Mark Setchell Avatar answered Jan 21 '26 08:01

Mark Setchell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!