Fast method of finding min(R,G,B)

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
tom_dl
Posts: 43
Joined: 2015-02-26T08:25:44-07:00
Authentication code: 6789

Fast method of finding min(R,G,B)

Post by tom_dl »

I would like to find min(R,G,B) for an image. None of the -colorspace options provide this, so I've constructed a line to generate the desired results:

Code: Select all

convert input.png -separate \( -clone 0,1 -compose Darken -composite \) \( -clone 2,3 -compose Darken -composite \) -delete 0-3 output.png
but it is very slow. I had an idea that using the CMYK conversion's method for K (i.e. min(C,M,Y)) could work by fooling IM into taking the inverse of CMY as input, but couldn't get that to work.

Any ideas very much appreciated!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Fast method of finding min(R,G,B)

Post by fmw42 »

try

Code: Select all

convert input.png -separate -evaluate-sequence min output.png
tom_dl
Posts: 43
Joined: 2015-02-26T08:25:44-07:00
Authentication code: 6789

Re: Fast method of finding min(R,G,B)

Post by tom_dl »

Can't believe I overlooked that! Thanks, that's 1.5x faster on my machine :-)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Fast method of finding min(R,G,B)

Post by snibgo »

Note that if the input has an alpha channel, in v7 "magick", "-separate" will give four outputs, where v6 gives three.

So, if you may upgrade to v7 in the future, I suggest you put "-channel RGB" before "-separate", and "+channel" after it.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Fast method of finding min(R,G,B)

Post by fmw42 »

snibgo wrote:Note that if the input has an alpha channel, in v7 "magick", "-separate" will give four outputs, where v6 gives three.

So, if you may upgrade to v7 in the future, I suggest you put "-channel RGB" before "-separate", and "+channel" after it.

Good point. Or if the alpha channel is pure opaque, just add -alpha off. However, if the image might be CMYK, the you may want to convert to RGB first (preferably using profiles). So some checks and conditionals may be needed.
Post Reply