We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpVideo Capture,  Movie Playback,  Vision Libraries › help with method to find color of a blob
Page Index Toggle Pages: 1
help with method to find color of a blob (Read 648 times)
help with method to find color of a blob
Aug 7th, 2009, 3:54pm
 
hi,

im writing code to check if objects have been placed on a surface that a camera is mounted underneath. i need to method that returns a value if the colour has been found.

i wrote some code, using an example from Daniel Shiffmans book and have edited it to return a boolean if the the color that has been passed to the function if found or not found. i need a return type to use this function as part of other functions.

import processing.video.*;

Capture video;
color colToFind;

boolean findColor(color t) {
 colToFind = t;
 float closestCol = 500;
 int closestX = 0;
 int closestY = 0;
 for(int x = 0; x < video.width; x++) {
   for (int y = 0; y < video.height; y++) {
     int loc = x + y*video.width;
     color currentColor = video.pixels[loc];
     float r1 = red(currentColor);
     float g1 = green(currentColor);
     float b1 = blue(currentColor);
     float r2 = red(colToFind);
     float g2 = green(colToFind);
     float b2 = blue(colToFind);
     float d = dist(r1,g1,b1,r2,g2,b2);
     
     if(d < closestCol) {
       closestCol = d;
       closestX = x;
       closestY = y;
     }
   }
 }
 if (closestCol < 10) {
   return true;
 }else{
   return false;
 }
}


i had also experimented with jmyrons trackColor but i dont think has a return type. it just simple tracks that given color. im trying to think of a way to implement this function as it may make it easier.

when i try to run the above code gives an error unexpected token: boolean. if i change this to a void and take out the last bit of code, the if statement then it works fine.

does anyone know where im going wrong or have any suggestions of a better way to do this?

thanks in advance!
Re: help with method to find color of a blob
Reply #1 - Aug 8th, 2009, 4:31am
 
Processing sketches are converted to Java before being compiled and executed. I guess you could call it a domain-specific language but enough buzzwords. While converting the sketch to Java, the Processing core makes some assumptions as to what you want to do. Specifically, if your sketch does not have a draw() and/or a setup() method, Processing will assume that you are in a terrible hurry or that you are a first time user, and the resulting Java code will be different, look for "static mode sketch" in the documentation.

Just add a void draw() or a void setup() method (you are going to need them anyway) and it will compile.

I can't tell you if it works though, because I'm on Linux, no Quicktime here.
Page Index Toggle Pages: 1