Page 1 of 1
How to darken only pixels that meet a certain criteria?
Posted: 2014-02-24T14:13:38-07:00
by IM.nut
Admittedly new to IM, but already having success with it. This challenge, though, has me stumped (after several more hours of research) so asking here.
Dealing with
scans of 100-year-old, pencil-written notes in which the paper has darkened and the pencil lightened so contrast and readability have both diminished, the goal is to
preserve page pixels (unmarked by pencil) as-is while
only darkening the pencil ones. (Otherwise, one could just tinker with brightness/contrast, which is easy.)
- * Since the pencil pixels are gray, it would be great if there were an adjustment to set the allowable deviation from true gray for pixel selection, R±x = G = B±x ('x' = allowable deviation).
* To be selected, a pixel would also have to pass below a maximum threshhold, avg(RGB) < t ('t' = upper threshhold. Note no lower threshhold needed, though if present, could be set at 0.)
* And the last parameter would be the amount of darkening (or could be a negative brightening) to apply to selected pixels.
So what I'm looking for should operate something like:
Code: Select all
For each pixel:
If within gray color tolerance AND below an upper limit of brightness
Darken the pixel by selected amount
Otherwise
Do nothing
So is this pie-in-the-sky, or is there a ready solution?
Good community here; hope someone can help.
Re: How to darken only pixels that meet a certain criteria?
Posted: 2014-02-24T14:49:01-07:00
by fmw42
What version of Imagemagick? What platform? Can you post a link to an example image? You can upload to some free hosting service such as dropbox and put a link here. Best to have an example to test with.
Re: How to darken only pixels that meet a certain criteria?
Posted: 2014-02-24T16:52:47-07:00
by IM.nut
fmw42 wrote:What version of Imagemagick? What platform? Can you post a link to an example image? You can upload to some free hosting service such as dropbox and put a link here. Best to have an example to test with.
Apologies, Fred (and thanks for your excellent IM scripts page), meant to include that in the OP.
It's "6.6.9-7 2012-08-17 Q16" on Linux (and my GIMP is v2.6.12.).
Example image is here:
http://www.xup.in/dl,14041127/100-yr-snippet.png/. Preparing it made me see that (contrary to the OP), a lower darkness threshhold would indeed be useful: so that the cracks between pages don't get even darker.
Re: How to darken only pixels that meet a certain criteria?
Posted: 2014-02-24T18:33:54-07:00
by fmw42
Here is something that kind of works. But you will have to fine tune with the thresholds, etc. The sensitive one seems to be the 67.5
#line1 - read the input and turn off any alpha channel
#line2 - clone the image and make 20% darker
#line3 - make all pixels with 40% of midgray into white and the rest black (mask1)
#line4 - make all pixels above 67.5% white and the rest black, then negate it, so above 67.5% is now black and excluded (mask2)
#line5 - make all pixels above 30% white, so that any pixel darker than that is excluded (mask3)
#line6 - copy each of the masks and multiply them all together (AND operation)
#line7 - remove the 3 individual masks, then composite the darkened image over the original using the mask image to control which one shows
Code: Select all
convert 100-YR-SNIPPET.PNG -alpha off \
\( -clone 0 -modulate 80,100,100 \) \
\( -clone 0 -colorspace gray -write show: -fuzz 40% -fill white -opaque "gray50" -fuzz 0 -fill black +opaque white -write show: \) \
\( -clone 0 -colorspace gray -threshold 67.5% -negate -write show: \) \
\( -clone 0 -colorspace gray -threshold 30% -write show: \) \
\( -clone 2 -clone 3 -clone 4 -evaluate-sequence multiply -write show: \) \
-delete 2,3,4 -compose over -composite 100-YR-SNIPPET_darken20.png
see
http://www.imagemagick.org/Usage/compose/#compose
http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/basics/#clone
http://www.imagemagick.org/script/comma ... e-sequence
Re: How to darken only pixels that meet a certain criteria?
Posted: 2014-02-24T19:00:41-07:00
by fmw42
If you just want to get the text with a white background, you can try my textcleaner script if on Unix.
Code: Select all
textcleaner -f 100 -o 5 100-YR-SNIPPET.PNG 100-YR-SNIPPET_textcleaner_f100_o5.png
or
Code: Select all
textcleaner -f 100 -o 5 -b gray50 100-YR-SNIPPET.PNG 100-YR-SNIPPET_textcleaner_f100_o5_g50.png
You can also try this as some pre-processing:
Code: Select all
convert 100-YR-SNIPPET.PNG -morphology open octagon:4 100-YR-SNIPPET2.PNG
or
Code: Select all
convert 100-YR-SNIPPET.PNG -statistic minimum 5x5 00-YR-SNIPPET3.PNG
Re: How to darken only pixels that meet a certain criteria?
Posted: 2014-02-25T06:19:57-07:00
by IM.nut
That is amazing.
So it appears IM really can do everything. And if this kind of support is typical, it's certainly tops in the open source world.
Let me play with this to check the extremes, then apply to the whole batch. Will report back on results and list code that finally worked.
Many thanks.
Re: How to darken only pixels that meet a certain criteria?
Posted: 2014-02-25T11:19:08-07:00
by fmw42