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 › combining processing and hypermedia codes together
Page Index Toggle Pages: 1
combining processing and hypermedia codes together (Read 1463 times)
combining processing and hypermedia codes together
Jun 5th, 2010, 10:28pm
 
Hi there,
I dunno if this the correct place to post this, but I'm having trouble figuring out the codes I'm using. For a Uni assignment, we are supposed to create a glitch. my aim is a threshold camera viewing it as real reflection, and there supposed to be a bug walking around. The bug has this detection, and once you touch it, a mirrored camera will appear but will constantly cross back and forth rapidly between the threshold camera and the real colour mirrored camera making it a glitch.

I managed to make the glitch in a processing.video.* format but with a keyPressed, but want the bug to have the detection which is in the hypermedia.video.* format... is there a way to combine it together?

the processing.video.* format with the bug and the glitchy camera
Code:
float x;
float y;
float xa;
float ya;
float xz = 100;
float yz = 100;
float angle1 = 0.0;
float segLength = 10;

import processing.video.*;


color black = color(0);
color white = color(255);
int numPixels;
Capture video;

PImage displayFrame;
boolean mirror;

void setup() {
size (640, 480);
strokeWeight(20.0);
stroke(0, 255);
video = new Capture (this, width, height, 24);
numPixels = video.width * video.height;
noCursor();
smooth();
displayFrame = new PImage(width, height);
mirror = false;
}

void draw() {



displayFrame.copy(video,0, 0, width, height, 0, 0, width, height);

if(mirror)
translate(width, 0);
scale(-1, 1);

image(displayFrame, 0, 0);


if (video.available()) {
video.read();

video.loadPixels();
int threshold = 150;
float pixelBrightness;

loadPixels();
for (int i = 0; i < numPixels; i++) {
pixelBrightness = brightness(video.pixels[i]);
if (pixelBrightness > threshold) {
pixels[i] = white;
} else {
pixels[i] = black;
}
}
updatePixels();

int testValue = get (mouseX, mouseY);
float testBrightness = brightness(testValue);
if (testBrightness > threshold) {
fill (black);
} else {
fill (white);
}
}


// drawing shapes

float dx = x - xz;
float dy = y - yz;
angle1 = atan2(dy, dx);
xz = x - (cos(angle1) * segLength);
yz = y - (sin(angle1) * segLength);

translate(width/2,height/2);
segment(xz, yz, angle1);


// movement
xa=xa+random(-0.1,0.1);
ya=ya+random(-0.1,0.1);
x=x+xa;
y=y+ya;
// walls
if(x>=width/2-2){
x=x-0.1;
xa=-xa*0.5;
}if(x<=-width/2+2){
x=x+0.1;
xa=-xa*0.5;
}if(y>=height/2-2){
y=y-0.1;
ya=-ya*0.5;
}if(y<=-height/2+2){
y=y+0.1;
ya=-ya*0.5;
}
// speed limiter
if(xa>=2){
xa=xa-1;
}if(xa<=-2){
xa=xa+1;
}if(ya>=2){
ya=ya-1;
}if(ya<=-2){
ya=ya+1;
}
}


void segment(float xz, float yz, float a) {
pushMatrix();
translate(xz, yz);
rotate(a);

strokeWeight(20.0);
stroke(50, 255);
line(0, 0, segLength, 0);
strokeWeight(1.0);
stroke(255);
fill(255);
ellipse(15,3,1,1);//
ellipse(15,-3,1,1);
fill(255,0,0);
stroke(255,0,0);
ellipse(0,-2,2,2);
ellipse(0,2,2,2);
ellipse(-5,-4,2,2);
ellipse(-5,4,2,2);
ellipse(5,-4,2,2);
ellipse(5,4,2,2);
popMatrix();

}




void keyPressed () {
mirror = !mirror;
}

Re: combining processing and hypermedia codes together
Reply #1 - Jun 5th, 2010, 10:29pm
 
the second code which has the detection, which i want the bug to be.
Code:
import hypermedia.video.*;

OpenCV opencv;
int threshold = 80;
PImage testingFrame;


void setup() {
size(640,480);
opencv = new OpenCV(this);
opencv.capture(width,height);
testingFrame = new PImage(width, height);

noFill();
stroke(0,255,0);
}

void draw() {
//read the video input
opencv.read();
//make the difference between the current image and the image in memory
//this is applied to the current opencv image so it changes!
opencv.absDiff();
//apply a threshold
//this is applied to the current opencv image so it changes!
opencv.threshold(threshold);
//flip the imae
//this is applied to the current opencv image so it changes!
opencv.flip( OpenCV.FLIP_HORIZONTAL );
//convert it to grayscale
opencv.convert(OpenCV.GRAY);
//draw the modified image to the screen

//if we want to compare frames continually turn this on, note you may need to play with
//the threshold (click and drag)
//opencv.remember();
//copy the current image in openCV into our tesing frame
testingFrame.copy(opencv.image(), 0, 0, width, height, 0, 0, width, height);
image( testingFrame, 0, 0 );
//will print out the true/false this function returns
println(testAreaMovement(500,380,20,20));
}



//we are returning a value in case you want to use if up the top ie. if (testAreaMovement(.....)) do this
boolean testAreaMovement(int tX, int tY, int tW, int tH) {
/*Basically what we are going here is going through each pixel in the area we
are testing. If the pixels is not black either movement or a change has happened
there. We want to test how many of the pixels in the area are not black, if there is
a certain amount then we have a "hit"*/
//hold a value whethere there is a 'hit' or not, assume from the beginning flase
boolean frameHit = false;
//how many pixeks are we testing
float totalNumPixels = tW*tH;
//how many pixels are not black
float numNonBlack = 0;
//create a new image
PImage test;
//make it the width and the height of the area we are testing
test = new PImage(tW,tH);
//copy the area from testing frame into test
test.copy(testingFrame,tX,tY,tW,tH, 0,0, tW,tH);
//load tests pixels
test.loadPixels();
//use a for loop to go through each pixel in test's image
for (int i = 0; i < test.pixels.length; i++) {
color currentColor = test.pixels[i];
//split it up into rgb
float r1,g1,b1;
r1 = red(currentColor);
g1 = green(currentColor);
b1 = blue(currentColor);
//if the r,g,b are all greater than 15 then assume not black, add that value on to numNotBlack
if ((r1 > 15) && (g1 > 15) && (b1 > 15)) {
numNonBlack++;
}
// }
}

// updatePixels();
//this is a threshold we can change, if we lower it we are looking for less not-black pixels
//higher, we are looking for more
//in this case if 25% of the pixels are not black
float percHitThreshold = .25;
//here we find out what percentage of pixels are not-black we divide the number of not-black
//pixels we counted by the total number of pixels we tested
float percHitInSquare = numNonBlack/totalNumPixels;
//print it out so we can see
println(percHitInSquare);
//if ther percentage of pixels that are not black in our square is higher than out threshold
//then its a hit
if (percHitInSquare > percHitThreshold) {
//so set frameHit to true
frameHit = true;
//make the stroke red
stroke(255,0,0);
} else {
//make the stroke green
stroke(0,255,0);
}
//draw a rect

rect(tX,tY,tW,tH);
return frameHit;
}


//if we want to compare from a "plate" frame
void keyPressed() {
if ( key==' ' ) opencv.remember();
}

//change the threshold
void mouseDragged() {
threshold = int( map(mouseX,0,width,0,255) );
}

public void stop() {
opencv.stop();
super.stop();
}
Page Index Toggle Pages: 1