Codes i like to share. Image Zoom, video processing, cradle, and more
in
Share your Work
•
4 months ago
import controlP5.*;
import processing.video.*;
Movie myMovie;
PImage prev_frame;
PVector blockDim;
float dist_threshold = 20;
//UI
ControlP5 controlP5;
Slider s;
void setup() {
size(200, 200);
myMovie = new Movie(this, "station.mov");
myMovie.loop();
controlP5 = new ControlP5(this);
s = controlP5.addSlider("dist_threshold", 10, 100, dist_threshold, 0, height - 10, width, 10);
blockDim = new PVector(5, 5);
myMovie.read();
prev_frame = myMovie.get();
}
void movieEvent(Movie myMovie) {
myMovie.read();
}
void draw() {
background(0);
image(myMovie, 0, 0);
compareAndUpdate(prev_frame, myMovie, blockDim);
prev_frame = myMovie.get();
}
void compareAndUpdate(PImage prev_frame, PImage curr_frame, PVector blockDim) {
dist_threshold = s.value();
for(int i = 0; i < prev_frame.height; i += blockDim.y) {
for(int j = 0; j < prev_frame.width; j += blockDim.x) {
float total_dist = 0;
for(int ii = 0; ii < blockDim.y; ii++) {
for(int jj = 0; jj < blockDim.x; jj++) {
int x = constrain(j + jj, 0, prev_frame.width - 1);
int y = constrain(i + ii, 0, prev_frame.height - 1);
int loc = x + y * prev_frame.width;
float r1 = red(prev_frame.pixels[loc]), r2 = red(curr_frame.pixels[loc]);
float g1 = green(prev_frame.pixels[loc]), g2 = green(curr_frame.pixels[loc]);
float b1 = blue(prev_frame.pixels[loc]), b2 = blue(curr_frame.pixels[loc]);
total_dist += dist(r1,g1,b1,r2,g2,b2);
}
}
total_dist /= blockDim.x * blockDim.y;
if(total_dist > dist_threshold) {
noFill();
stroke(255, 0, 0);
rect(j, i, blockDim.x, blockDim.y);
}
}
}
}
import processing.video.*;
Movie mov;
PImage back, cur;
void setup()
{
mov = new Movie(this,"video.mov");
mov.read();
mov.loop();
size(600,500,P2D);
back = loadImage("bg.jpg");
}
void draw()
{
cur = changeBack(mov, back);
cur.filter(BLUR);
image(cur,0,0);
}
PImage changeBack(PImage orig, PImage back)
{
PImage mod = new PImage(orig.width, orig.height);
for(int i = 0;i < orig.height; i++)
for(int j = 0;j < orig.width; j++)
{
int loc = i*orig.width + j;
color c = orig.pixels[loc];
//the values are found by trail and error& by Dani :)
if (green(c) >= 150 && red(c)<100 && blue(c)<100 )
mod.pixels[loc] = back.pixels[loc];
else
mod.pixels[loc] = orig.pixels[loc];
}
return mod;
}
PImage img;
float ratio;
void setup()
{
img = loadImage("carc.jpg");
size(img.width, img.height);
noFill();
ratio = 1;
}
void draw()
{
//background(0);~~NO NEED~~
loadPixels();
for(int i = 0; i< img.height; i++)
for(int j = 0; j < img.width; j++)
{
float x = map(j,0,img.width-1,0,1);
float y = map(i,0,img.height-1,0,1);
PVector p = new PVector(x,y);
float mx = map(mouseX,0,img.width-1,0,1);
float my = map(mouseY,0,img.height-1,0,1);
PVector m = new PVector(mx,my);
float dis = p.dist(m);
//This value is found experimentaly by Trail and Error!
float r = 0.178;
if(dis > r)
pixels[i*img.width+j] = img.pixels[i*img.width+j];
else
{
float dmag = pow(dis,1-ratio);
PVector dirVect = new PVector(p.x-m.x,p.y-m.y);
dirVect.mult(dmag);
dirVect.add(m);
x = map(dirVect.x,0,1,0,width-1);
y = map(dirVect.y,0,1,0,height-1);
x = constrain(x,0,width-1);
y = constrain(y,0,height-1);
pixels[i*width + j] = img.pixels[(int)(y)*width+(int)x];
}
}
updatePixels();
ellipse(mouseX,mouseY,250,250);
text("Magnifying Ratio: " + (1-ratio)+", FINALLY IT WORKED :)", 20,20);
}
void keyPressed()
{
if (key == 'w' && ratio < 1.75)
ratio = ratio+0.01;
else if (key == 's' && ratio > -0.75)
ratio = ratio-0.01;
else if (key == 'r')
ratio = 1;
}
float per;
int len;
float amp;
void setup()
{
size(700,400);
per = 2;
len = 200;
amp = 0.4;
}
void draw()
{
background(255);
fill(255,0,0);
noStroke();
rect(50,20,600,60);
//intermediate Balls
for(int i = 2; i<=8; i++)
{
stroke(0,255,0);
line(100+i*50,80,100+i*50,80+len);
noStroke();
fill(0,0,255);
ellipse(100+i*50,80+len,50,50);
}
float time = millis()%(((int)per)*1000);
time = time/1000;
float ang = calcAngle(time);
PVector ball;
PVector l = new PVector();
if( time >= 0.25*per && time<=0.75*per)
{
l.x = 550;
l.y = 80;
ball = update(l, ang);
stroke(255,0,255);
line(l.x,l.y,ball.x,ball.y);
noStroke();
fill(255,255,0);
ellipse(ball.x,ball.y,50,50);
stroke(255,255,0);
line(150,80,150,80+len);
noStroke();
fill(255,0,255);
ellipse(150,80+len,50,50);
}
else
{
l.x = 150;
l.y = 80;
ball = update(l, ang);
stroke(255,0,255);
line(l.x,l.y,ball.x,ball.y);
noStroke();
fill(255,255,0);
ellipse(ball.x,ball.y,50,50);
stroke(255,255,0);
line(550,80,550,80+len);
noStroke();
fill(255,0,255);
ellipse(550,80+len,50,50);
}
}
float calcAngle(float time)
{
return amp*cos(TWO_PI*time/per);
}
PVector update(PVector ver, float angle)
{
PVector b = new PVector();
b.x = ver.x - len*sin(angle);
b.y = ver.y + len*cos(angle);
return b;
}
void setup(){
size(300,300);
P0=new PVector(width/2,height/2);
V = new PVector(0,0);
P=new PVector(width/2,height/2);
r=30;
dampp=0.1;
dampv=0.05;
}
PVector P,P0,V;
boolean locked=false;
boolean overBox=false;
int r;
float dampp;
float dampv;
void draw(){
background(0,255,0);
if((sq(mouseX-P.x) + sq(mouseY-P.y))<sq(r))
{
fill(255);
overBox=true;
}
else{
stroke(0);
overBox =false;
fill(255,0,0);
}
if(released){
updateP();
updateV();
}
if(P.x==P0.x&&P.y==P0.y){ V.x=0; V.y=0;}
ellipse(P.x,P.y,r,r);
line(0,0,P.x-r/4,P.y-r/4);
line(width,width,P.x+r/4,P.y+r/4);
}
void updateP(){
P.x+=V.x;
P.y+=V.y;
P0.x=width/2;
P0.y=height/2;
}
void updateV(){
V.x+=(P0.x-P.x)*dampp-(V.x*dampv);
V.y+=(P0.y-P.y)*dampp-(V.y*dampv);
}
void mousePressed()
{
if(overBox){
locked=true;
}
else locked=false;
}
void mouseDragged(){
if(locked)
{
P.x=mouseX;
P.y=mouseY;
}
}
boolean released=false;
void mouseReleased(){
locked = false;
released=true;
}