Page 1 of 1

How to extract dominant colours from an image?

Posted: 2016-01-11T05:21:50-07:00
by joshuafinny
I tried using
convert tree.gif -unique-colors -depth 16 txt:-

Here's the situation:
I have a list of images with a background(textured or plain) and a product on it. The product colour is what I want to extract.
I want only the top 5 dominant colours, so that I can eliminate the first hex as the background colour and use the rest.The above script takes my image into a loop and creates about 50000+ hex codes. Also, I want this process to run in batch for all images in a folder not just one image.

Please help.

Re: How to extract dominant colours from an image?

Posted: 2016-01-11T05:39:19-07:00
by Bonzo
You could reduce the amount of colours in your image first with -colors 5
Set the preferred number of colors in the image.

The actual number of colors in the image may be less than your request, but never more. Note that this a color reduction option. Images with fewer unique colors than specified by value will have any duplicate or unused colors removed. The ordering of an existing color palette may be altered. When converting an image from color to grayscale, it is more efficient to convert the image to the gray colorspace before reducing the number of colors. Refer to the color reduction algorithm for more details.

Re: How to extract dominant colours from an image?

Posted: 2016-01-11T11:00:36-07:00
by fmw42
Can you post and example image so we can see and test with it? You can post to some free service such as dropbox.com and put the URL here.

Always best to provide your exact command line, images, IM version and platform when asking questions, since syntax may differ.

See viewtopic.php?f=1&t=9620

Re: How to extract dominant colours from an image?

Posted: 2016-01-14T09:10:00-07:00
by joshuafinny

Re: How to extract dominant colours from an image?

Posted: 2016-01-14T09:24:45-07:00
by snibgo

Code: Select all

convert 1_18.jpg +dither -colors 5 x.png
x.png is a simplified verson of the image, reduced to no more than 5 colours. "identify -verbose x.png" tells you the colours and their frequency. The most common is near white. The next is a darkish grayish red.

Re: How to extract dominant colours from an image?

Posted: 2016-01-16T06:29:33-07:00
by joshuafinny
snibgo wrote:

Code: Select all

convert 1_18.jpg +dither -colors 5 x.png
x.png is a simplified verson of the image, reduced to no more than 5 colours. "identify -verbose x.png" tells you the colours and their frequency. The most common is near white. The next is a darkish grayish red.
I want the same 5 colours to be extracted to a text file as hex values.

Please help.

Re: How to extract dominant colours from an image?

Posted: 2016-01-16T06:52:42-07:00
by snibgo

Code: Select all

convert 1_18.jpg +dither -colors 5 -unique-colors txt:
Then a script can extract the hex digits after "#".

Re: How to extract dominant colours from an image?

Posted: 2016-01-16T21:47:45-07:00
by joshuafinny
snibgo wrote:

Code: Select all

convert 1_18.jpg +dither -colors 5 -unique-colors txt:
Then a script can extract the hex digits after "#".
I think this is what I need. Can I include identify -verbose in this script to sort the colours in the order of frequency(high to low)? How do i run this in batch for all images in a folder?

Re: How to extract dominant colours from an image?

Posted: 2016-01-17T09:19:55-07:00
by snibgo
If you want the counts as well as the colours:

Code: Select all

f:\web\im>"%IM%convert" 1_18.jpg +dither -colors 5 -define histogram:unique-colors=true -format "%c" histogram:info:

     88596: ( 84, 75, 86) #544B56 srgb(84,75,86)
    142039: (178, 41, 56) #B22938 srgb(178,41,56)
     72690: (193, 56, 71) #C13847 srgb(193,56,71)
    128940: (213,172,165) #D5ACA5 srgb(213,172,165)
    377735: (241,239,236) #F1EFEC srgb(241,239,236)
Sorting will give you these in ascending or descending order of the count. To do all the files in a directory, I would always use a script with FOR, but perhaps mogrify can do the job.

Re: How to extract dominant colours from an image?

Posted: 2016-01-24T00:56:24-07:00
by joshuafinny
snibgo wrote:If you want the counts as well as the colours:

Code: Select all

f:\web\im>"%IM%convert" 1_18.jpg +dither -colors 5 -define histogram:unique-colors=true -format "%c" histogram:info:

     88596: ( 84, 75, 86) #544B56 srgb(84,75,86)
    142039: (178, 41, 56) #B22938 srgb(178,41,56)
     72690: (193, 56, 71) #C13847 srgb(193,56,71)
    128940: (213,172,165) #D5ACA5 srgb(213,172,165)
    377735: (241,239,236) #F1EFEC srgb(241,239,236)
Sorting will give you these in ascending or descending order of the count. To do all the files in a directory, I would always use a script with FOR, but perhaps mogrify can do the job.
convert *.jpg +dither -colors 5 -define histogram:unique-colors=true -format "%c" histogram:info: >hex.txt
Thanks! Did the trick for me! But I get all hex codes one after the other, so I don't know which image it corresponds to. Is there a way to get the image name and corresponding hex codes on the file?

Re: How to extract dominant colours from an image?

Posted: 2016-01-24T10:08:14-07:00
by snibgo
See http://www.imagemagick.org/script/escape.php

For example, you might use a format of:

Code: Select all

%f\n%c\n

Re: How to extract dominant colours from an image?

Posted: 2016-02-05T11:52:14-07:00
by joshuafinny
Great worked for me! Thanks a lot!