Moving OpenCV-generated Blob Shapes
in
Programming Questions
•
3 years ago
Hello,
I am trying to create a system whereby shapes generated from the points in an OpenCV Blobs[] array can be repositioned on the screen by clicking and dragging them. I would like the shapes to remain "active," i.e. to continue to correspond to the dynamic shape of the "blob," while being placed in a different position on the screen.
So far I am able to determine whether the mouse is within the bounds of a blob (in the code the blob turns red when the mouse is over it and orange when it is clicked). What I am having trouble with is figuring out how to structure the program so that I can click and drag a blob-generated shape and have it remember and stay in the new position into which it is dropped.
I am not sure if I should try to add more parameters to the Blob object for storing whether it has been moved, its translation offset, etc., or if I should create some new object from some data from the Blob objects and then do the translating and display in that object.
Any help is very much appreciated.
Here is the code I have so far:
import hypermedia.video.*;
OpenCV opencv;
boolean bover = false;
boolean locked = false;
color defaultColor = color(100,205,180);
color hoverColor = color(200, 100, 100);
color highlightColor = color(255, 180, 100);
void setup() {
size( 640, 480 );
frameRate(15);
smooth();
// open video stream
opencv = new OpenCV( this );
opencv.capture( 640, 480 );
}
void draw() {
// noLoop();
// noStroke();
fill(255, 255, 255);
rect(0, 0, width, height);
opencv.read(); // grab frame from camera
opencv.threshold(200); // set black & white threshold
// find blobs
Blob[] blobs = opencv.blobs( 10, width*height/2, 20, true, OpenCV.MAX_VERTICES );
// draw blob results
for( int i=0; i<blobs.length-1; i++ ) {
int k=blobs[i].points.length-1;
bover=false;
if(locked==false) {
// This determines whether the mouse position is within the bounds of the current blob
for( int j=0; j<blobs[i].points.length-1; j++) {
if (blobs[i].points[j].y<mouseY && blobs[i].points[k].y>=mouseY
|| blobs[i].points[k].y<mouseY && blobs[i].points[j].y>=mouseY) {
if (blobs[i].points[j].x+(mouseY-blobs[i].points[j].y)/(blobs[i].points[k].y-blobs[i].points[j].y)*(blobs[i].points[k].x-blobs[i].points[j].x)<mouseX) {
bover=!bover;
}
}
k=j;
}
}
// Assign hoverColor to blob shape if mouse is over it
// Assign highlightColor to blob shape if mouse is over it and clicked
fill(defaultColor);
if (bover==true) {
fill(hoverColor);
}
if (bover==true && mousePressed==true) {
fill(highlightColor);
locked=true;
} else {
locked=false;
}
beginShape();
for( int m=0; m<blobs[i].points.length-1; m++) {
vertex(blobs[i].points[m].x, blobs[i].points[m].y);
}
endShape();
}
}
I am trying to create a system whereby shapes generated from the points in an OpenCV Blobs[] array can be repositioned on the screen by clicking and dragging them. I would like the shapes to remain "active," i.e. to continue to correspond to the dynamic shape of the "blob," while being placed in a different position on the screen.
So far I am able to determine whether the mouse is within the bounds of a blob (in the code the blob turns red when the mouse is over it and orange when it is clicked). What I am having trouble with is figuring out how to structure the program so that I can click and drag a blob-generated shape and have it remember and stay in the new position into which it is dropped.
I am not sure if I should try to add more parameters to the Blob object for storing whether it has been moved, its translation offset, etc., or if I should create some new object from some data from the Blob objects and then do the translating and display in that object.
Any help is very much appreciated.
Here is the code I have so far:
import hypermedia.video.*;
OpenCV opencv;
boolean bover = false;
boolean locked = false;
color defaultColor = color(100,205,180);
color hoverColor = color(200, 100, 100);
color highlightColor = color(255, 180, 100);
void setup() {
size( 640, 480 );
frameRate(15);
smooth();
// open video stream
opencv = new OpenCV( this );
opencv.capture( 640, 480 );
}
void draw() {
// noLoop();
// noStroke();
fill(255, 255, 255);
rect(0, 0, width, height);
opencv.read(); // grab frame from camera
opencv.threshold(200); // set black & white threshold
// find blobs
Blob[] blobs = opencv.blobs( 10, width*height/2, 20, true, OpenCV.MAX_VERTICES );
// draw blob results
for( int i=0; i<blobs.length-1; i++ ) {
int k=blobs[i].points.length-1;
bover=false;
if(locked==false) {
// This determines whether the mouse position is within the bounds of the current blob
for( int j=0; j<blobs[i].points.length-1; j++) {
if (blobs[i].points[j].y<mouseY && blobs[i].points[k].y>=mouseY
|| blobs[i].points[k].y<mouseY && blobs[i].points[j].y>=mouseY) {
if (blobs[i].points[j].x+(mouseY-blobs[i].points[j].y)/(blobs[i].points[k].y-blobs[i].points[j].y)*(blobs[i].points[k].x-blobs[i].points[j].x)<mouseX) {
bover=!bover;
}
}
k=j;
}
}
// Assign hoverColor to blob shape if mouse is over it
// Assign highlightColor to blob shape if mouse is over it and clicked
fill(defaultColor);
if (bover==true) {
fill(hoverColor);
}
if (bover==true && mousePressed==true) {
fill(highlightColor);
locked=true;
} else {
locked=false;
}
beginShape();
for( int m=0; m<blobs[i].points.length-1; m++) {
vertex(blobs[i].points[m].x, blobs[i].points[m].y);
}
endShape();
}
}
3