Page 1 of 1

Count unique colors for image

Posted: 2020-03-04T02:25:48-07:00
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.

Re: Count unique colors for image

Posted: 2020-03-04T03:07:46-07:00
by snibgo
I don't know what "Docker v4tech" is. Perhaps you need to escape the % sign. In Windows BAT, you would need "%%k".

Re: Count unique colors for image

Posted: 2020-03-04T03:46:39-07:00
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.

Re: Count unique colors for image

Posted: 2020-03-04T10:24:36-07:00
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

Re: Count unique colors for image

Posted: 2020-03-04T12:06:03-07:00
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

Re: Count unique colors for image

Posted: 2020-03-04T22:52:28-07:00
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.