combining code
in
Contributed Library Questions
•
2 years ago
i have these two codes, and i want to put them together so that the more movement there is the faster the word moves.
thanks (:
code one:
import hypermedia.video.*; // Imports the OpenCV library
OpenCV opencv; // Creates a new OpenCV Object
PImage trailsImg; // Image to hold the trails
int hCycle; // A variable to hold the hue of the image tint
int counter = 0;
void setup()
{
size( 640,480 );
opencv = new OpenCV( this ); // Initialises the OpenCV object
opencv.capture( 640,480 ); // Opens a video capture stream
trailsImg = new PImage( 640,480 ); // Initialises trailsImg
hCycle = 0; // Initialise hCycle
}
void draw()
{
counter ++;
if (counter >=20) {
loadPixels();
for (int i = 0; i < pixels.length; i++) {
pixels[i] = color(int(random(255)));
}
updatePixels();
counter = 0;
}
println(counter);
opencv.read(); // Grabs a frame from the camera
PImage camImage; // Creates an image and
camImage = opencv.image(); // stores the unprocessed camera frame in it
opencv.absDiff(); // Calculates the absolute difference
opencv.convert( OpenCV.GRAY ); // Converts the difference image to greyscale
opencv.blur( OpenCV.BLUR, 3 ); // I like to blur before taking the difference image to reduce camera noise
opencv.threshold( 20 );
trailsImg.blend( opencv.image(), 0, 0, 640,480, 0, 0, 640,480, SCREEN ); // Blends the movement image with the trails image
colorMode(HSB); // Changes the colour mode to HSB so that we can change the hue
tint(color(hCycle, 255,255)); // Sets the tint so that the hue is equal to hcycle and the saturation and brightness are at 100%
image( trailsImg, 0, 0 ); // Display the blended difference image
noTint(); // Turns tint off
colorMode(RGB); // Changes the colour mode back to the default
blend( camImage, 0, 0, 640,480, 0, 0, 640,480, SCREEN ); // Blends the original image with the trails image
opencv.copy( trailsImg ); // Copies trailsImg into OpenCV buffer so we can put some effects on it
opencv.blur( OpenCV.BLUR, 4 ); // Blurs the trails image
opencv.brightness( -20 ); // Sets the brightness of the trails image to -20 so it will fade out
trailsImg = opencv.image(); // Puts the modified image from the buffer back into trailsImg
opencv.remember(); // Remembers the current frame
hCycle++; // Increments the hCycle variable by 1 so that the hue changes each frame
if (hCycle < 255) hCycle = 0; // If hCycle is greater than 255 (the maximum value for a hue) then make it equal to 0
}
---------------
code 2:
import hypermedia.video.*;
OpenCV opencv;
PFont f;
float x = 100; // x location of square
float y = 0; // y location of square
float speed = 0; // speed of square
float gravity = 0.1;
void setup() {
size(640,480);
opencv = new OpenCV(this);
opencv.capture(width,height);
f = loadFont("AmericanTypewriter-20.vlw");
}
void draw() {
opencv.read();
opencv.flip( OpenCV.FLIP_HORIZONTAL );
opencv.convert(OpenCV.GRAY);
background( opencv.image());
textFont(f,20);
fill(255);
text("stupid",random(20,580),y);
y= y+speed;
speed = speed+gravity;
constrain(x, 20,580);
constrain(y,20,580);
// If square reaches the bottom
// Reverse speed
if (y > height) {
// Multiplying by -0.95 instead of -1 slows the square down each time it bounces (by decreasing speed).
// This is known as a "dampening" effect and is a more realistic simulation of the real world (without it, a ball would bounce forever).
speed = speed * -0.95;
}
}
1