Sorry, here is the code.
I created this program with the help of some talented users of this forum.
It permits to select photographies with the body, by moving left or right in front of the webcam.
When I start the program, I first have to click on the frame of the program and than click on Space.
If I don't do this, the program doesn't do anything when I press Space.
I created this program with the help of some talented users of this forum.
It permits to select photographies with the body, by moving left or right in front of the webcam.
When I start the program, I first have to click on the frame of the program and than click on Space.
If I don't do this, the program doesn't do anything when I press Space.
- import processing.video.*;
int numPixels;
int[] backgroundPixels;
int[] hist;
int[] histTop;
Capture video;
int pressed=0;
float presenceSum=0;
//Dati relativi al gioco
PImage bigImage;
PImage big1;
PImage big2;
PImage big3;
PImage big4;
PImage big5;
PImage big6;
PImage small1;
PImage small2;
PImage small3;
PImage small4;
PImage small5;
PImage small6;
PImage cursor;
void setup()
{
size(800, 600, P2D);
// Change size to 320 x 240 if too slow at 640 x 480
video = new Capture(this, width, height);
video.start();
numPixels = video.width * video.height;
// Create array to store the background image
backgroundPixels = new int[numPixels];
// Make the pixels[] array available for direct manipulation
loadPixels();
big1 = loadImage("0big.jpg");
big2 = loadImage("1big.jpg");
big3 = loadImage("2big.jpg");
big4 = loadImage("3big.jpg");
big5 = loadImage("4big.jpg");
big6 = loadImage("5big.jpg");
small1 = loadImage("0small.jpg");
small2 = loadImage("1small.jpg");
small3 = loadImage("2small.jpg");
small4 = loadImage("3small.jpg");
small5 = loadImage("4small.jpg");
small6 = loadImage("5small.jpg");
cursor = loadImage("cursor.png");
}
void draw()
{
if (video.available()) {
video.read(); // Read a new video frame
video.loadPixels(); // Make the pixels of video available
// Difference between the current frame and the stored background
hist = new int[width];
float maxval = 0;
int maxposx=0;
int maxposy=0;
for (int i = 0; i < numPixels; i++) { // For each pixel in the video frame...
// Fetch the current color in that location, and also the color
// of the background in that spot
color currColor = video.pixels[i];
color bkgdColor = backgroundPixels[i];
// Extract the red, green, and blue components of the current pixel’s color
float currR = red(currColor);
float currG = green(currColor);
float currB = blue(currColor);
// Extract the red, green, and blue components of the background pixel’s color
// esegue la stessa operaizone di prima ma in maniera più veloce
float bkgdR = red(bkgdColor);
float bkgdG = green(bkgdColor);
float bkgdB = blue(bkgdColor);
// Compute the difference of the red, green, and blue values
float diffR = abs(currR - bkgdR);
float diffG = abs(currG - bkgdG);
float diffB = abs(currB - bkgdB);
// Add these differences to the running tally
// Render the difference image to the screen
if (diffR>20 && diffG>20 && diffB>20 && pressed==1) {
pixels[i] = color(255, 255, 255);
presenceSum = presenceSum + 1;
}
else {
{
pixels[i] = color(0, 0, 0);
}
}
}
updatePixels(); // Notify that the pixels[] array has changed
textSize(30);
fill (255);
text("Assicuratevi che non ci sia nessuno davanti la webcam.", 5, 100);
text("Premere il tasto SPAZIO per far avviare il programma.", 5, 140);
filter(ERODE);
if (pressed==1 && presenceSum>width*height/100) {
// Calculate the histogram
for (int i=0; i<width; i++) {
for (int j=0; j<height; j++) {
if ( int(red(get(i, j))) == 255 && int(green(get(i, j)))==255 && int(blue(get(i, j)))==255 )
hist[i]=hist[i]+1;
}
}
// Find the largest value in the histogram
for (int i=0; i<width; i++) {
if (hist[i] > maxval) {
maxval = hist[i];
maxposx = i;
}
}
maxposy=height-int(maxval);
if (key==' ') {
background(0);
image (cursor, width-maxposx-50, 40);
image (small1, 100, 20);
image (small2, 200, 20);
image (small3, 300, 20);
image (small4, 400, 20);
image (small5, 500, 20);
image (small6, 600, 20);
if (bigImage != null)
image (bigImage, 60, 120);
if (width-maxposx>100&&width-maxposx<180)
{
bigImage = big1;
}
else
if (width-maxposx>200&&width-maxposx<280)
{
bigImage = big2;
}
else
if (width-maxposx>300&&width-maxposx<380)
{
bigImage = big3;
}
else
if (width-maxposx>400&&width-maxposx<480)
{
bigImage = big4;
}
else
if (width-maxposx>500&&width-maxposx<580)
{
bigImage = big5;
}
else
if (width-maxposx>600&&width-maxposx<680)
{
bigImage = big6;
}
else
{
bigImage = null;
}
}
if (key=='v') {
for (int i = 1; i < width; i++) {
for (int j = 0; j < hist[i]; j++) {
color c = color(255, 0, 0);
set(i, j, c);
}
}
for (int i = maxposx-3; i < maxposx+4; i++) {
for (int j = 0; j < height; j++) {
color c = color(0, 255, 0);
set(i, j, c);
}
}
for (int j = height-int(maxval)-3; j < height-int(maxval)+4; j++) {
for (int i = 0; i < width; i++) {
color c = color(0, 255, 0);
set(i, j, c);
}
}
}
}
}
}
void keyPressed() {
if (key==' ') {
video.loadPixels();
arraycopy(video.pixels, backgroundPixels);
pressed=1;
}
}