FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   how many colors in an image?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: how many colors in an image?  (Read 431 times)
lunetta

7200475272004752 WWW Email
how many colors in an image?
« on: Dec 17th, 2004, 11:16am »

Hello all
 
I need some suggestion, advice or idea:
I need to develop a piece of code that with the input of a image, it outputs an object array, each object having 2 properties: a unique color value, and its incidence.
 
So, my main challenge so far is: How can I count how many unique colors I have in an image?
 
 
I'm really grateful for any light in the shadow void of algorithms and data structures inside lunetta's mind.
« Last Edit: Dec 17th, 2004, 11:32am by lunetta »  
mattgilbert

tangramkid WWW Email
Re: how many colors in an image?
« Reply #1 on: Dec 17th, 2004, 1:13pm »

this thread should help:
 
http://processing.org/discourse/yabb/board_Programs_action_disp_lay_num_1086857409.html
 
 
toxi

WWW
Re: how many colors in an image?
« Reply #2 on: Dec 17th, 2004, 3:19pm »

here's one i made earlier/ages ago... should do pretty muc what you want. an Hashtable is an easy-to-use data structure for those type of problems:
 
Code:
BImage img;
Hashtable uniqueCols;
 
void setup() {
  size(200,200);
  img=loadImage("logo.gif");
  // find & count occurance of unique colours
  uniqueCols=countUniqueColours(img);
   
  // get all colour names (RGB hexvalue)
  Enumeration colNames=uniqueCols.keys();
   
  // compute contribution for each colour
  while(colNames.hasMoreElements()) {
    String currCol=(String)colNames.nextElement();
    int currColCount=((IntCounter)uniqueCols.get(currCol)).getValue();
    // display result
    print(currCol+": ");
    print(nf(currColCount*100.0/img.pixels.length,1,1));
    println("% ("+currColCount+" pixels)");
  }
   
  //display image
  image(img,0,0);
}
 
public Hashtable countUniqueColours(BImage img) {
  Hashtable index=new Hashtable();
  for(int i=0; i<img.pixels.length; i++) {
    // use hex value of colour as hash code (unique ID)
    String col=Integer.toHexString(img.pixels[i]&0xffffff);
    if (!index.containsKey(col)) {
 // create new entry, if new colour
 index.put(col,new IntCounter(1));
    } else {
 // or increase count for existing colour
 IntCounter counter=(IntCounter)index.get(col);
 counter.increment();
    }
  }
  return index;
}
 
// counter/helper class
// used by countUniqueColours() method
class IntCounter {
  int value;
  IntCounter(int i) {
    value=i;
  }
  void increment() {
    value++;
  }
  int getValue() {
    return value;
  }
}
« Last Edit: Dec 17th, 2004, 3:23pm by toxi »  

http://toxi.co.uk/
lunetta

7200475272004752 WWW Email
Re: how many colors in an image?
« Reply #3 on: Dec 17th, 2004, 6:16pm »

thanks, I'll study both programs.
 
I coded a counter last night, and 8 hours laters processing was still counting a 50x50 pixels image. That's slooooooooooow (It worked for a 10x10). So I guess my approach wasn't very good. Imagine a 1280x1024...
 
thanks again for the feedback
 
Pages: 1 

« Previous topic | Next topic »