Count unique colors for image

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?".
Locked
DavidLiang
Posts: 2
Joined: 2020-03-04T02:17:15-07:00
Authentication code: 1152

Count unique colors for image

Post by DavidLiang »

I have read https://www.imagemagick.org/discourse-s ... hp?t=12599 for unique color counting, but my result was weird.

IM Command:

Code: Select all

identify  -format "%k" 1.png
Output :
k

IM version
Docker v4tech/imagemagick
version: 6.9.6

Any help would be appreciated.

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

Re: Count unique colors for image

Post by snibgo »

I don't know what "Docker v4tech" is. Perhaps you need to escape the % sign. In Windows BAT, you would need "%%k".
snibgo's IM pages: im.snibgo.com

DavidLiang
Posts: 2
Joined: 2020-03-04T02:17:15-07:00
Authentication code: 1152

Re: Count unique colors for image

Post by DavidLiang »

snibgo wrote:
2020-03-04T03:07:46-07:00
I don't know what "Docker v4tech" is. Perhaps you need to escape the % sign. In Windows BAT, you would need "%%k".
v4tech/imagemagick
https://hub.docker.com/r/v4tech/imagemagick

Thank you, it worked.

I got another question about color counting. Is that possible to merge similar colors?

I ran IM command for the image below:

Code: Select all

identify -format "%k" c1.png
Image

I got result 1586 colors.

How do I get expected result like 9 colors(Counted with human eyes). Thank you.

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Count unique colors for image

Post by fmw42 »

Your image has alpha channel and also is antialiased. So all the anti-aliasing produces extra colors. Convert your image to fewer colors such as:

Code: Select all

convert dog.png +dither -colors 17 new_dog.png
or use -remap using a color table image to the exact colors that you want. See https://imagemagick.org/Usage/quantize/#remap

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

Re: Count unique colors for image

Post by snibgo »

DavidLiang wrote:I got another question about color counting. Is that possible to merge similar colors?
Your dog image has areas of flat (constant) colours, with anti-aliasing between them. We might assume that any pixel detected by a Sobel edge-detector may be an anti-aliased pixel, so is not one of the unique colours.

The following Windows BAT command counts the unique colours, and writes them to u.png, and the count to stdout, and the remapped image to r.png.

It assumes the image contains white.

There are 11 unique colours.

Code: Select all

magick ^
  %SRC% ^
  -alpha off ^
  +write mpr:INP ^
  -define convolve:scale="50%%^!" -bias 50%% ^
  -define morphology:compose=Lighten ^
  -morphology Convolve "Sobel:>"  ^
  -solarize 50%% -level 50,0%% ^
  -negate ^
  -fill Black +opaque White ^
  +write e.png ^
  mpr:INP ^
  +swap ^
  -alpha off ^
  -compose CopyAlpha -composite ^
  +write c0.png ^
  -background White ^
  -compose Over ^
  -layers Flatten ^
  +write f.png ^
  -unique-colors +write mpr:UNIQ ^
  +write u.png ^
  -format "Unique: %%w\n" +write info: ^
  +delete ^
  mpr:INP ^
  -remap mpr:UNIQ ^
  r.png
snibgo's IM pages: im.snibgo.com

Peggy
Posts: 33
Joined: 2020-03-02T22:31:47-07:00
Authentication code: 1152

Re: Count unique colors for image

Post by Peggy »

snibgo wrote:
2020-03-04T12:06:03-07:00
DavidLiang wrote:I got another question about color counting. Is that possible to merge similar colors?
Your dog image has areas of flat (constant) colours, with anti-aliasing between them. We might assume that any pixel detected by a Sobel edge-detector may be an anti-aliased pixel, so is not one of the unique colours.

The following Windows BAT command counts the unique colours, and writes them to u.png, and the count to stdout, and the remapped image to r.png.

It assumes the image contains white.

There are 11 unique colours.

Code: Select all

magick ^
  %SRC% ^
  -alpha off ^
  +write mpr:INP ^
  -define convolve:scale="50%%^!" -bias 50%% ^
  -define morphology:compose=Lighten ^
  -morphology Convolve "Sobel:>"  ^
  -solarize 50%% -level 50,0%% ^
  -negate ^
  -fill Black +opaque White ^
  +write e.png ^
  mpr:INP ^
  +swap ^
  -alpha off ^
  -compose CopyAlpha -composite ^
  +write c0.png ^
  -background White ^
  -compose Over ^
  -layers Flatten ^
  +write f.png ^
  -unique-colors +write mpr:UNIQ ^
  +write u.png ^
  -format "Unique: %%w\n" +write info: ^
  +delete ^
  mpr:INP ^
  -remap mpr:UNIQ ^
  r.png
I think I need this for my case https://imagemagick.org/discourse-serve ... =1&t=37636. This is kinda amazing and complicated....the result is exactly what I want, but I don't know how to excute it, could I excute commands on linux server? Is this suitable for any type of images? Thank you for taking time to do this.

Locked