Page 1 of 1

Cleanning B&W scan

Posted: 2017-08-27T05:07:41-07:00
by MrNice
Hi forum people!

I am a newbie with IM so, thank you for your patience.
I spend 2 days to understand the principle reading doc and trying but this is hard and I struggle to reach my target.
Version: ImageMagick 6.9.3-0 Q16 x86_64 2017-03-11
I need to clean B&W scan (music sheet), I mean remove (change to white) all black shapes too big.
I want to remove (change to white) all shapes with size equal or smaller than 5x5. That means all shapes bigger than 5 pix in 1 dimension should be kept as it is.
Good if the size (5x5) is a parameter that I can change.

So far I did the following, not the easy way but it works for some shapes. It treats so far only to 3x3 black shape. However I don't know how to remove 3x3 square shapes and some other.

Could you help me, in a clear way, I mean I need the command.

Many thanks

Code: Select all

convert in.png -verbose \
-morphology Thicken '5x5:1,1,1,1,1, 1,0,0,0,1, 1,0,0,0,1, 1,0,0,0,1, 1,1,1,1,1' \
-morphology Thicken '5x5>:1,1,1,1,1, 1,0,1,1,1, 1,1,0,1,1, 1,1,1,0,1, 1,1,1,1,1' \
-morphology Thicken '5x5>:1,1,1,1,1, 1,0,1,0,1, 1,1,1,1,1, 1,0,1,0,1, 1,1,1,1,1' \
-morphology Thicken '5x5>:1,1,1,1,1, 1,0,1,1,1, 1,0,0,1,1, 1,1,1,0,1, 1,1,1,1,1' \
-morphology Thicken '5x5>:1,1,1,1,1, 1,0,1,0,1, 1,1,0,1,1, 1,0,1,0,1, 1,1,1,1,1' \
-morphology Thicken '5x5>:1,1,1,1,1, 1,0,0,0,1, 1,1,0,0,1, 1,1,0,0,1, 1,1,1,1,1' \
-morphology Thicken '5x5>:1,1,1,1,1, 1,0,0,0,1, 1,1,0,0,1, 1,1,1,1,1, 1,1,1,1,1' \
-morphology Thicken '5x5>:1,1,1,1,1, 1,0,1,1,1, 1,0,1,1,1, 1,1,0,1,1, 1,1,1,1,1' \
-morphology Thicken '5x5>:1,1,1,1,1, 1,0,0,1,1, 1,0,1,1,1, 1,0,1,1,1, 1,1,1,1,1' \
-morphology Thicken '5x5>:1,1,1,1,1, 1,0,1,1,1, 1,1,0,1,1, 1,0,1,1,1, 1,1,1,1,1' \
-morphology Thicken '5x5>:1,1,1,1,1, 1,1,0,1,1, 1,1,0,1,1, 1,1,1,0,1, 1,1,1,1,1' \
-morphology Thicken '5x5>:1,1,1,1,1, 1,1,0,1,1, 1,1,0,1,1, 1,0,1,1,1, 1,1,1,1,1' \
-morphology Thicken '5x5>:1,1,1,1,1, 1,1,0,1,1, 1,1,0,1,1, 1,0,1,0,1, 1,1,1,1,1' \
-morphology Thicken '5x5>:1,1,1,1,1, 1,1,0,1,1, 1,1,0,1,1, 1,0,0,0,1, 1,1,1,1,1' \
-morphology Thicken '5x5>:1,1,1,1,1, 1,0,1,1,1, 1,0,1,1,1, 1,0,0,0,1, 1,1,1,1,1' \
-morphology Thicken '3x5:1,1,1, 1,0,1, 1,0,1, 1,0,1, 1,1,1' \
-morphology Thicken '5x3:1,1,1,1,1, 1,0,0,0,1, 1,1,1,1,1' \
-morphology Thicken '3x4:1,1,1, 1,0,1, 1,0,1, 1,1,1' \
-morphology Thicken '4x3:1,1,1,1, 1,0,0,1, 1,1,1,1' \
-morphology Thicken '4x4:1,1,1,1, 1,0,0,1, 1,0,0,1, 1,1,1,1' \
-morphology Thicken '4x4>:1,1,1,1, 1,0,0,1, 1,0,1,1, 1,1,1,1' \
-morphology Thicken '4x4>:1,1,1,1, 1,0,1,1, 1,1,0,1, 1,1,1,1' \
-morphology Thicken '3x3:1,1,1, 1,0,1, 1,1,1' \
out.png

Re: Cleanning B&W scan

Posted: 2017-08-27T07:39:13-07:00
by Bonzo
It is always a good idea to link to an example - you can not save the image on the forum.

Also what operating system are you using as it may make a differnce?

Re: Cleanning B&W scan

Posted: 2017-08-27T07:59:07-07:00
by MrNice
Thank you for your answer,
My OS is Linux Fedora 25

Example:
https://ufile.io/gh6r9

Re: Cleanning B&W scan

Posted: 2017-08-27T11:30:43-07:00
by fmw42
try this

Code: Select all

convert example.png -colorspace gray -depth 8 -type bilevel \
-define connected-components:verbose=true \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=20 \
-connected-components 8 result.gif
Adjust the area-threshold as desired. Also best to chop off the left side black.

Re: Cleanning B&W scan

Posted: 2017-08-28T12:00:33-07:00
by MrNice
Thank you very much fmw42 for your help. This is exactly what I need.
I could not expect better.
I could never find this command. Well done !

Just one last question; Why do you transcode from .png to .gif?
After cleaning I'll transcode the folder to a .pdf file. Should I keep .png or change to .gif ?

Re: Cleanning B&W scan

Posted: 2017-08-28T12:16:19-07:00
by fmw42
It does not matter. GIF may give a smaller output size. But I have not tested that.