Need to count how many pixels are MORE blue than red

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
dgpeters
Posts: 19
Joined: 2013-09-10T15:47:00-07:00
Authentication code: 6789

Need to count how many pixels are MORE blue than red

Post by dgpeters » 2014-12-28T15:29:19-07:00

I need to count how many pixels in an image (I would prefer the answer reduced to a RATIO from 0 to 1) are "MORE blue than yellow." I have a rectangle PNG file. So for example if the file contained 50% blue pixels and 50% green pixels, the result should be 0.5 since green is an equal amount of blue and yellow, but I only want to count when a pixel is MORE blue than yellow.

I know some of you will think "how about just looking at the total amount of blue and yellow in the picture and make a ratio from that?" That is not going to work for me at all. I need the ratio determined from a pixel-by-pixel comparison for each pixel in the image.

I did look though some of the examples pages on this website. I couldn't find anything that solved my problem.

Also, I have a brand new iMac (5K), and the ImageMagick build that I just downloaded doesn't do anything. Here's what happens: (looks like how I used to do it on my Mac Pro)

Code: Select all

$ export MAGICK_HOME="$HOME/Applications/ImageMagick-6.9.0"
$ export PATH="$MAGICK_HOME/bin:$PATH"
$ export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib"
$ ./convert
dyld: Library not loaded: /ImageMagick-6.9.0/lib/libMagickCore-6.Q16.2.dylib
  Referenced from: /Applications/ImageMagick-6.9.0/bin/./convert
  Reason: image not found
Trace/BPT trap: 5
$ 

snibgo
Posts: 12431
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Need to count how many pixels are MORE blue than red

Post by snibgo » 2014-12-28T15:50:57-07:00

For the blue/yellow problem, I would make two grayscale images, each the size of the original. The first would contain "blueness", so is probably just a copy of the blue channel. The second contains "yellowness", but I don't know how you would define that. It could be the negated yellow channel of a CMY version.

Take "blueness" minus "yellowness". Then make any pixels that are not black, white ("-fill white +opaque black"). Take the mean of the resulting image. If 1.0, all the pixels were more blue than yellow.

This can all be done in a single "convert" command.

Sorry, I can't help with the Mac problem. (When you have two unrelated problems, it is best to make two posts.)
snibgo's IM pages: im.snibgo.com

snibgo
Posts: 12431
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Need to count how many pixels are MORE blue than red

Post by snibgo » 2014-12-28T16:19:08-07:00

A different approach uses the fact that blue and yellow are complementary. Their hues are 180 degrees apart.

We make a sample input. Convert to HSL and regard only the first (Hue) channel. Add 1/12, so pure blue is now 75% and pure yellow is 25%. Pixels above 50% were "more blue than yellow".

Windows BAT syntax:

Code: Select all

%IM%convert xc:blue xc:yellow xc:lime xc:gray +append sample.png

%IM%convert ^
  sample.png ^
  -colorspace HSL -channel R -separate +channel ^
  -evaluate addmodulus 8.33333333%% ^
  -threshold 50%% ^
  -format %%[fx:mean] info:
In this example, greys (that have no hue) work correctly.
snibgo's IM pages: im.snibgo.com

Post Reply