Page 1 of 1

Change Pixel Colors Within Yellow-Highlighted Areas

Posted: 2018-09-10T17:28:47-07:00
by rockydata
I have many handwritten documents that have been scanned.
I am trying to redact (blackout) image areas that have been yellow-highlighted (with a marker pen before scanning).
BOTH the yellow-pixels and the non-yellow-pixels within the highlighted areas need blackening.
I’m able to blacken the yellow-pixels, but the other non-yellow-pixels stay the same.

This is my code so far:
convert ImageIN -fuzz 33% -fill black -opaque yellow ImageOUT

I'm running IM v7.0.8-10 on Windows 10 Pro.
Any help or advice is appreciated.

Re: Change Pixel Colors Within Yellow-Highlighted Areas

Posted: 2018-09-10T17:42:01-07:00
by snibgo
You need to show a sample image. (You can create one for this purpose.)

You will need to define "within the highlighted areas", so a command or script can decide which pixels qualify. By "non-yellow", do you mean the parts that have ink from the handwriting, or does some un-inked paper qualify?

Re: Change Pixel Colors Within Yellow-Highlighted Areas

Posted: 2018-09-10T17:54:17-07:00
by fmw42
I suspect it is antialiasing that is left behind. If the text were black and the yellow mixed with the black, then the fuzz value might not catch enough. Perhaps some morphology or -connected-components processing might help. But as snibgo said, we would need to see an example. This forum does not allow uploading of images. So you should post one to some free hosting service and put the URL here.

Re: Change Pixel Colors Within Yellow-Highlighted Areas

Posted: 2018-09-12T22:02:34-07:00
by rockydata
Here's a link to a visual description of what I'm trying to accomplish:
http://rockydata.com/Share/ImageMagicK_ ... amming.jpg
Thanks for your help.

Re: Change Pixel Colors Within Yellow-Highlighted Areas

Posted: 2018-09-12T22:32:58-07:00
by fmw42
I cannot get to that web site. My browser says it is a High Risk web site and blocks it. Can you post somewhere else?

Re: Change Pixel Colors Within Yellow-Highlighted Areas

Posted: 2018-09-13T17:46:59-07:00
by rockydata
Sorry, here's a SECURE link to a visual description of what I'm trying to accomplish:
https://www.dropbox.com/s/rvdvbsd69p72k ... g.jpg?dl=0
Thanks

Re: Change Pixel Colors Within Yellow-Highlighted Areas

Posted: 2018-09-13T19:28:41-07:00
by fmw42
Here is one way.

It would have been easier if your JPG was an originally processed PNG so that the colors were constant.

But you have JPG compression in the white area that causes white not to be pure white. So I have to use a fuzz change of near white to pure white first. Then I create a fully black image. Then I convert your image to hue and do a fuzz change of colors to make a mask. I use red as a temporary color so that I do not get either unwanted white or black mixed with the making of the binary mask. Then I do a masked composite between the input and black image using the mask to decide which image to use where.

Input:
Image

Code: Select all

convert yellow.jpg -fuzz 10% -fill white -opaque white \
\( -clone 0 -fill black -colorize 100 \) \
\( -clone 0 -colorspace HSV -channel red -separate +channel \
-fuzz 5% -fill red -opaque "gray(48)" -fill black +opaque red -fill white -opaque red \) \
-compose over -composite \
yellow_black.jpg
Image


The above is Unix syntax using ImageMagick 6.9.10.11 Q16 Mac OSX Sierra.

Re: Change Pixel Colors Within Yellow-Highlighted Areas

Posted: 2018-09-14T17:35:57-07:00
by rockydata
I appreciate your help on this.
The handwriting may come in all sorts of colors, or even mixed.
I'll digest what you've said and give it a try.
Thanks!

Re: Change Pixel Colors Within Yellow-Highlighted Areas

Posted: 2018-09-14T20:45:15-07:00
by rockydata
Sorry, I tried, but I'm just not familiar enough with the various parameters to get your code to work for me.
I'm having a hard time understanding the "clone", "compose" and "channel" parameters.
I'm trying to run it from a Windows-10 batch command file.
So I tried to break the code into these steps to understand better, but I'm obviously missing some concepts.

convert ORIGINAL(YELLOW).jpg -fuzz 10%% -fill white -opaque white STEP1(WHITENED).jpg
convert STEP1(WHITENED).jpg -fill black -colorize 100 STEP2(ALL-BLACK).jpg
convert STEP1(WHITENED).jpg -colorspace HSV -channel red -separate +channel STEP3(GRAY-LETTERING).jpg
convert STEP3(GRAY-LETTERING).jpg -fuzz 10% -fill red -opaque "gray(48)" STEP4(NO-CHANGE).jpg
convert STEP4(NO-CHANGE).jpg -fill black +opaque red -fill white -opaque red STEP5(NO-CHANGE).jpg
convert STEP5(NO-CHANGE).jpg -compose over -composite STEP6(NOT-CREATED).jpg

I was able to get the "STEP3(GRAY-LETTERING).jpg" image, but nothing changed after that.
The "STEP3(GRAY-LETTERING).jpg" is actually lots of different gray shades for the writing, and the background completely black.
Any help is very appreciated.

Re: Change Pixel Colors Within Yellow-Highlighted Areas

Posted: 2018-09-14T21:40:27-07:00
by fmw42
I am not a Windows user. Nevertheless, this is the Windows equivalent. If you put it in a bat file, you need to double the % to %%. The ^ is an new line character, so leave it as multiline or remove the ^ an make this into one long command line.

line1: replace near white with pure white for anything within 10%
line2: copy (clone) the line1 result result and make it totally black
line3: copy the first line, convert to HSV colorspace and extract the Hue channel
line4: replace anything with 5% of gray(48) [hues that cover the yellow and green] with red, then change anything not red to black and change anything red with white
line5: use the line3&4 result as a mask to mix the first line with the the second line
line6: write the result as the output

Code: Select all

convert yellow.jpg -fuzz 10% -fill white -opaque white ^
( -clone 0 -fill black -colorize 100 ) ^
( -clone 0 -colorspace HSV -channel red -separate +channel ^
-fuzz 5% -fill red -opaque "gray(48)" -fill black +opaque red -fill white -opaque red ) ^
-compose over -composite ^
yellow_black.jpg
See
https://www.imagemagick.org/Usage/windows/
https://www.imagemagick.org/script/comm ... ptions.php
https://www.imagemagick.org/Usage/compose/#compose
https://www.imagemagick.org/Usage/basics/#clone
https://www.imagemagick.org/Usage/basics/#parenthesis
https://www.imagemagick.org/Usage/color ... /#channels

Basically, I am use the Hue values to pick out the yellow and green and make them white and the rest black. Then I use that mask to use part of the original image and part of the black image to achieve black where you had yellow and green and the rest unchanged.

Note that hue at red wraps around. That is hue=0=red=360. And black is also hue=0. So this does not work well for near red, unless you rotate the hue values by 180.

Re: Change Pixel Colors Within Yellow-Highlighted Areas

Posted: 2018-09-15T19:59:32-07:00
by fmw42
Here is an alternate method. You need to know the color of the blue text.

Code: Select all

Line1: read the image
Line2: clone the image to make a mask image and set a fuzz value for changing colors
Line3: make near white in the clone into full white via the fuzz value
Line4: make blue in the clone into white
Line5: make anything not white in the clone into black
Line6: use morphology in the clone to remove small regions of black where there were antialiasing remnants of the text
Line7: use morphology in the clone to extend the large black region to get rid of some outline white
Line8: multiply the mask with the input image
Line9: write the output

Code: Select all

convert yellow.jpg \
\( -clone 0 -fuzz 10% \
-fill white -opaque white \
-fill white -opaque "rgb(109,128,196)" \
-fill black +opaque white \
-morphology close octagon:2 \
-morphology erode octagon:2 \) \
-compose multiply -composite \
yellow_black2.png
Image

Re: Change Pixel Colors Within Yellow-Highlighted Areas

Posted: 2018-09-24T14:39:40-07:00
by rockydata
I'm still playing with it to get it to work, but you've given me more than enough for me to figure it out.
I appreciate so much Fred for your taking time to help me with this.
Thanks!