Cleanning B&W scan

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
MrNice
Posts: 3
Joined: 2017-08-27T04:32:03-07:00
Authentication code: 1151

Cleanning B&W scan

Post by MrNice » 2017-08-27T05:07:41-07:00

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

Bonzo
Posts: 2848
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Cleanning B&W scan

Post by Bonzo » 2017-08-27T07:39:13-07:00

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?

MrNice
Posts: 3
Joined: 2017-08-27T04:32:03-07:00
Authentication code: 1151

Re: Cleanning B&W scan

Post by MrNice » 2017-08-27T07:59:07-07:00

Thank you for your answer,
My OS is Linux Fedora 25

Example:
https://ufile.io/gh6r9

User avatar
fmw42
Posts: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Cleanning B&W scan

Post by fmw42 » 2017-08-27T11:30:43-07:00

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.

MrNice
Posts: 3
Joined: 2017-08-27T04:32:03-07:00
Authentication code: 1151

Re: Cleanning B&W scan

Post by MrNice » 2017-08-28T12:00:33-07:00

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 ?

User avatar
fmw42
Posts: 22091
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Cleanning B&W scan

Post by fmw42 » 2017-08-28T12:16:19-07:00

It does not matter. GIF may give a smaller output size. But I have not tested that.

Post Reply