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 › Combining Scripts
Page Index Toggle Pages: 1
Combining Scripts (Read 1000 times)
Combining Scripts
Feb 14th, 2010, 6:14pm
 
Hey,

I've two different processing scripts in two different documents. One simply takes an pic of what's on the web cam and saves it once the mouse is clicked. The other takes an image and applies a mask and effect to it.

I am looking to combine these so when you do the first one mentioned—and take a picture, then you can click again and import that picture so it can be used with the second part mentioned above. I hope that wasn't confusing.

Been struggling with this for a few hours.  Angry

Here's the two scripts
First:
Code:
import processing.video.*;

Capture cam;

float x = 0;
float numFrames = 1;

void setup() {
size(640, 480);
cam = new Capture(this, 640, 480);
}

void draw() {
if (cam.available() == true) {
cam.read();
image(cam, 0, 0);
if (mouseButton == LEFT)
saveFrame("1.jpg");
}
}




Second:
Code:
// Start Processing Eye
void setup() {
size(640, 480);
}



void draw() {
background(204);
if (mouseButton == LEFT) {
// Get Image, Crop and Copy
PImage cam;
cam = loadImage("1.jpg");
image(cam, 0, 0);
copy(140, 100, 340, 200, 380, 220, 340, 200);

// Mask
PImage snapMask = loadImage("1mask.jpg");
cam.mask(snapMask);
image(cam, 0, 0);
} else {
fill(0);
}
}


Not looking for anyone to do this—just some help.
Re: Combining Scripts
Reply #1 - Feb 14th, 2010, 7:39pm
 
Quote:
Not looking for anyone to do this—just some help.

Aww...

Ok, so, you should have a boolean that tracks if a frame has been saved. When mousePressed() is called, check that boolean to see if there is a saved frame. If there isn't one, save one and set the boolean to true. If there is one, apply the mask stuff and set the boolean to false.

I also suggest that your PImage variable in the second program be renamed to something other than cam, since that's the name of the camera from the first one. It could get confusing if they are named the same thing.

Posting what you have of a combined version of the code might also be quite useful.
Re: Combining Scripts
Reply #2 - Feb 14th, 2010, 10:00pm
 
Hey tfGuy, renamed the "cam" for the PImage.

I don't have a version combined code and can you explain how I'd do a boolean for this? I just started with Processing today.
Re: Combining Scripts
Reply #3 - Feb 15th, 2010, 1:12am
 
First thing is to create global variables for the image etc otherwise they will only be available inside the method they are created e.g.

Code:
Capture cam;
PImage img, snapMask;
int status = 0;

void setup(){
  ...
}


Load the snapMask image in setup so you are not repeating this 60 times per second.

Then add the mousePressed method
Code:
void mousePressed(){

}


Inside this method you want the following logic

if the staus is 0
  capture and save the image (img)
  set status to 1
if the status is 1
  apply the mask to img
  set staus to 2
if status is 3
  set status to 0

In the draw method you want the following logic

if the status is 0
  display video output
if the status is 1
  display the captured image
if the status is 3
  display the image after the mask has been applied.


This should get you started. Smiley
Re: Combining Scripts
Reply #4 - Feb 15th, 2010, 8:45am
 
It's not working. I get cannot find class or type named "Capture". Did I not apply global variables to things properly—or at all?
Code:
Capture cam;
PImage img, snapMask;
int status = 0;

 void setup()  
 {
 size(640, 480);
 cam = new Capture(this, 640, 480);
 PImage snapMask = loadImage("1mask.jpg");
}

void mousePressed()  
 {
 if (status == 0)
 saveFrame("1.jpg");
 set (status == 1);
}
 {
 if (status == 1)
 PImage snapMask = loadImage("1mask.jpg");
 set (status == 2);
 snap1.mask(snapMask);
 image(snap1, 0, 0);
}
}
 if (status == 3)
 set (status == 0);
}

void draw()
 {
if (status == 0)
 cam.read();
 image(cam, 0, 0);
if (status == 1)
 PImage snap1;
 snap1 = loadImage("1.jpg");
 image(snap1, 0, 0);
if (status == 3)
 PImage snapMask = loadImage("1mask.jpg");
 snap1.mask(snapMask);
 image(snap1, 0, 0);
}

Re: Combining Scripts
Reply #5 - Feb 15th, 2010, 9:34am
 
Brodie wrote on Feb 15th, 2010, 8:45am:
cannot find class or type named "Capture"

Have you QuickTime properly installed
Re: Combining Scripts
Reply #6 - Feb 15th, 2010, 11:24am
 
I think the problem is more to do with the missing video library...
Did you include this line?
Quote:
import processing.video.*;
Re: Combining Scripts
Reply #7 - Feb 15th, 2010, 12:07pm
 
Thanks tf, that's resolved and some other issues. But now I get The method set (int, int, int) in the type PApplet is not applicable for the arguments (boolean). I've marked the line it highlights below.
Code:
import processing.video.*;

Capture cam;
PImage img, snapMask;
int status = 0;

 void setup()  
 {
 size(640, 480);
 cam = new Capture(this, 640, 480);
 PImage snapMask = loadImage("1mask.jpg");
}

void mousePressed()  
 {
 if (status == 0);
 saveFrame("1.jpg");
 set (status == 1);  //  It highlights this line
 
 if (status == 1);
 PImage snapMask = loadImage("1mask.jpg");
 set (status == 2);
 snap1.mask(snapMask);
 image(snap1, 0, 0);
}
}
 if (status == 3)
 set (status == 0);
}

void draw()
 {
if (status == 0)
 cam.read();
 image(cam, 0, 0);
if (status == 1)
 PImage snap1;
 snap1 = loadImage("1.jpg");
 image(snap1, 0, 0);
if (status == 3)
 PImage snapMask = loadImage("1mask.jpg");
 snap1.mask(snapMask);
 image(snap1, 0, 0);
}
Re: Combining Scripts
Reply #8 - Feb 15th, 2010, 1:01pm
 
Brodie said
Quote:
Not looking for anyone to do this—just some help.


I took you at your word so rather than providing the actual code I stated the problem in structured English and expected you to convert to Java unfortunately you have loads of syntax errors.

The code below is syntactically correct and I suggest that you compare this with your code and see if you cam see where your code is incorrect. I cannot test the code to see if it does exactly what you want as I don't have a video source.

Have to go now let us know how you get on. Smiley

Code:
import processing.video.*;

Capture cam;
PImage snap1, snapMask;
int status = 0;

void setup()  
{
 size(640, 480);
 cam = new Capture(this, 640, 480);
 snapMask = loadImage("1mask.jpg");
}

void mousePressed()  
{
 if (status == 0){
   saveFrame("1.jpg");
   status = 1;  //  It highlights this line
 }
 if (status == 1){
   snapMask = loadImage("1mask.jpg");
   status = 2;
 }
 snap1.mask(snapMask);
 image(snap1, 0, 0);

 if (status == 3)
   status = 0;
}

void draw()
{
 if (status == 0){
   cam.read();
   image(cam, 0, 0);
 }
 if (status == 1){
   PImage snap1;
   snap1 = loadImage("1.jpg");
   image(snap1, 0, 0);
 }
 if (status == 3){
   snapMask = loadImage("1mask.jpg");
   snap1.mask(snapMask);
 }
 if(snap1 != null){
   image(snap1, 0, 0);
 }
}


Re: Combining Scripts
Reply #9 - Feb 15th, 2010, 1:25pm
 
It didn't work.

I've picked up Processing: A Programming Handbook for Visual Designers and Artists and will try to figure this out. Don't want any more help.

Thanks for your time Quark and tfGuy. I'll post anything later if it works.
Page Index Toggle Pages: 1