Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Re : Calculate and compare the amount of two colours in a video

$
0
0
Thanks for the response, here is what I have so far

  1. import processing.video.*;

  2. Capture cam;

  3. //colors to look for
  4. color trackColor;

  5. int colorCount=0;
  6. boolean click= false;


  7. void setup() {
  8.   size(640, 480);

  9.   String[] cameras = Capture.list();


  10.   cam = new Capture(this, cameras[0]);
  11.   cam.start();

  12. }

  13. void draw() {

  14.   if (cam.available() == true) {
  15.     cam.read();
  16.   }


  17.   image(cam, 0, 0);
  18.   cam.loadPixels();
  19.   
  20.   if(click){
  21.     
  22.     for(int i = 0; i<cam.width*cam.height;i++){
  23.       
  24.        if( cam.pixels[i]==trackColor)
  25.        {
  26.          
  27.          colorCount++;
  28.          
  29.        }
  30.        
  31.        if(i == (cam.width*cam.height)-1){    
  32.          
  33.         
  34.          println(colorCount);    //get amount of pixels of this colour in the frame
  35.          
  36.          float colorRatio = (colorCount/(cam.width*cam.height))*100;            //get ratio of colour in relation to image: (colorPixels/All the pixels) *100
  37.          println(colorRatio);                                                   // always returns 0, not sure why.
  38.          colorCount = 0;   
  39.        }
  40.     }      
  41.   }

  42.       cam.updatePixels();
  43.     }
  44.      
  45.   void mousePressed(){
  46.     
  47.    color c = get(mouseX, mouseY);
  48.    int loc = mouseX + mouseY*cam.width;
  49.    trackColor = cam.pixels[loc];
  50.    click = true;   
  51.     
  52.   }
What I have managed to do is track the amount of pixels of a given colour in every frame.
When I try to convert this to a ratio of all the pixels in the frame I always get 0, this could be a really rookie mistake on my side or something to do with floating point calculations. I have never really worked with so many decimal places before.

I also don't know if this is the most efficient way to do this whole operation either.

The other thing I could use your input on is how I would go about checking to see if a range of a specific colour is present. 

My final goal is to have a whole bunch of red and green (for example) paddles and be able tell which color is more prominent which would correspond to the amount of paddles present in the image. 

Oh and taking into account distance - i do understand that if some are closer they will take up more space.

really appreciate your help


Viewing all articles
Browse latest Browse all 1768

Trending Articles