Setting pixels to a new colour, if a specific channel value is dominant

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
Erik
Posts: 12
Joined: 2016-04-30T00:18:15-07:00
Authentication code: 1151

Setting pixels to a new colour, if a specific channel value is dominant

Post by Erik »

Hello,

I process scanned pages of text. Some of these pages show blue pen notes, which I want to replace with another colour.

Every pixel within a blue pen note shows a higher value in the Blue channel, compared to Red and Green channels. I'd like to use this ratio as a pixel selector, as this is the common denominator with respect to the whole page.

I already tried alternatives like below, but at some point, parts of the black text become filled with red.

I figure I need to separate the channels and then compare the values via fx, but I am not sure how to do that.

Code: Select all

convert image.jpg -fuzz 45% -fill red -opaque 'srgb(0,0,255)' 0672.jpg
Test Image:

https://drive.google.com/file/d/0BxivpP ... sp=sharing
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Setting pixels to a new colour, if a specific channel value is dominant

Post by snibgo »

You can do things like this:

Code: Select all

convert blue-pen-note.png -separate -delete 1 -compose MinusDst -composite -threshold 0% m.png
This creates a mask:
1. Separate input into R,G,B.
2. Delete image number 1, which leaves images just the R and B channels.
3. Subtract Dst from Src, ie find (B-R).
4. Where this is grater than 0%, make the result white. A different threshold may work better.

Use the mask to replace pixels with "red":

Code: Select all

convert blue-pen-note.png ( +clone -fill Red -colorize 100 ) m.png -compose Over -composite c.png
snibgo's IM pages: im.snibgo.com
Post Reply