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 & HelpSyntax Questions › Arg, help.  Pixel arrays and detection...
Page Index Toggle Pages: 1
Arg, help.  Pixel arrays and detection... (Read 199 times)
Arg, help.  Pixel arrays and detection...
Feb 12th, 2009, 6:39pm
 
Hallo.
So I've been playing around with OpenCV and wrote a bit of a thingy based off of a pair of tutorials I've found...
Anyway, I am having some serious issues with my lil' code.  Trying to debug it, nothing is really working right...
Mayhap some help?

general goal
1. Through OpenCV, data from webcam is processed and displayed.
2. The ball class is set up to read (specifically moveUpdate()) through the area surround the ball's current x/y coordinates, and then test to see if the brightness within that area re: the "move" image variable falls within a certain threshold, and then if the variable identifying the number of times this occurs in a cycle again reaches another threshold, the moveUpdate variable returns a boolean value of true.
3.  Within the main draw class, the positive or negative value of the moveUpdate function is tested, and if positive, then variables within the ball class are reset to force the lil' ellipse to move about the screen...
4. Repeat, etc...


My code is very messy and has a few cut/pasted parts from the tutorials I mentioned, but yeah.  I'm trying to wrap my head around the a) structure and b) methods by which you can detect and work with these kinds of things.
Unfortunately, it isn't working.

Also, the movementAmount variable within moveUpdate() consistently returns a value of 9801... why is this?

Anyway, thank you!


Code:

import hypermedia.video.*;
OpenCV opencv;
Ball ball;

PImage move;
int moveMe = 0;

void setup(){
size(320,240);
opencv = new OpenCV( this );
opencv.capture(width,height);
//move = new PImage( this );
ball = new Ball(0,0);
}

void draw() {
opencv.read();
opencv.flip(OpenCV.FLIP_HORIZONTAL);
// image( opencv.image(), 0, 0);
opencv.absDiff();
opencv.convert(OpenCV.GRAY);
opencv.blur(OpenCV.BLUR, 3);
opencv.threshold(20);
move = opencv.image();

image( opencv.image(), 0, 0);
//ball.update();
if(ball.moveUpdate() == true){
ball.draw(1);
}
else{ ball.draw(0); }

opencv.remember(OpenCV.SOURCE, OpenCV.FLIP_HORIZONTAL);
}



class Ball {
float beginX, beginY;
float endX, endY;
float distX, distY;
int ballSz = 100;
float exponent = 3.0;
float x,y = 0;
float step = 0.01;
float pct = 1.0;
int movementAmount = 0;

Ball(float begx, float begy) {
beginX = endX = x = begx; beginY = endY = y = begy;
pct = 1.0;
//endX = random(0,width); endY = random(0,height);
}

void update(){
//beginX = x; beginY = y;
//endX = beginX; endY = beginY;
//pct = 0.0;
}


boolean moveUpdate() {
boolean moveMe = false;
movementAmount = 0;

for( float py = y; py < (y + (ballSz-1)); py++ ){
for( float px = x; px < (x + (ballSz-1)); px++ ){
//if ( px < width && px > 0 && py > height && py > 0 ){
if (brightness(move.pixels[ceil(px) + (ceil(py) * width)]) == 127);
{
movementAmount++;
//println(py + "-" + px); //debug
}
}
// }
}
if(movementAmount >= 5) {
moveMe = true;
}
return moveMe;
}

void draw(int move) {
if(move == 1) {
if(moveMe == 1 && pct == 1.0) {endX = random(0,width); endY = random(0,height); };
if (x != endX && y != endY) {
pct += step;
distX = endX - beginX;
distY = endY - beginY;
x = beginX + (pow(pct, exponent) * distX);
y = beginY + (pct * distY);
}
}
else{
beginX = endX;
beginY = endY;
pct = 1.0;
}

pushMatrix();
translate(x,y);
ellipseMode(CORNER);
ellipse(0,0,ballSz,ballSz);
popMatrix();
println(movementAmount + " - " + moveMe + " - " + pct + " - " + x + ":" + y);
}



}
Page Index Toggle Pages: 1