-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebcamFlow.pde
More file actions
119 lines (104 loc) · 3.39 KB
/
WebcamFlow.pde
File metadata and controls
119 lines (104 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import gab.opencv.*;
import processing.video.*;
// Params --------------------
int camWidth = 640; // we'll use a smaller camera resolution, since
int camHeight = 360; // HD video might bog down our computer
PVector cameraSize = new PVector(camWidth,camHeight);
PVector windowSize = new PVector(width,height);
int gridTileSize = 4; // The camera image is further divided into regions to measure flow
// and we get the average movement in each
boolean doGridBilinearInterpolation = true; // Whether to smooth the grid by sampling using bilinear interpolation
float distortionStrength = 1;
int cameraDelayMs = int(10*1000);
int mode = 0; // Different modes for visualizing the flowmap
static int modeCount = 3;
boolean debug = false; // Debug mode
// Main --------------------
OpenCV cv;
Capture webcam;
PShader distortionMapShader;
String cameraName;
void setup()
{
// size(1280, 720, P2D);
fullScreen(P2D);
windowSize = new PVector(width,height);
// Start the webcam
String[] inputs = Capture.list();
printArray(inputs);
if (inputs.length == 0) {
println("Couldn't detect any webcams connected!");
exit();
}
cameraName = inputs[0];
webcam = new Capture(this, camWidth,camHeight, cameraName);
webcam.start();
cv = new OpenCV(this, camWidth,camHeight); // Create an instance of the OpenCV library
initializeFlowMap(); // Initialize the PImage used to store the flow map
cameraBuffer = new PImage(camWidth,camHeight);
// Intialize the shader
distortionMapShader = loadShader("DistortionMap.glsl");
distortionMapShader.set("u_resolution", windowSize.x,windowSize.y);
// distortionMapShader.set("u_cameraResolution", cameraSize.x,cameraSize.y);
// distortionMapShader.set("u_flowmapResolution", gridSize.x,gridSize.y);
distortionMapShader.set("u_flowmap", flowmap);
distortionMapShader.set("u_camera", cameraBuffer);
distortionMapShader.set("u_distortionStrength", distortionStrength);
}
static int ui_x = 4, ui_y = 12, fontSize = 12;
void draw()
{
if (webcam.available())
{
webcam.read();
cv.loadImage(webcam);
cv.calculateOpticalFlow(); // Initialize OpenCV and calculate optical flow
updateFlowMap(1.); // Sample the optical flow on a grid and store to flowmap image
displayFlow(webcam);
if (debug)
{
int quarterWidth = width/4;
int quarterHeight = height/4;
fill(color(0,255,0));
textSize(fontSize);
image(flowmap,0,0,quarterWidth,quarterHeight);
text("Flow map", ui_x,ui_y);
text(String.format("res: %dx%d", gridWidth,gridHeight), ui_x,ui_y+fontSize);
text(String.format("linger: %.2f", flowLinger), ui_x,ui_y+2*fontSize);
image(webcam,0,quarterHeight,quarterWidth,quarterHeight);
text(cameraName, ui_x,ui_y+quarterHeight);
text(String.format("display mode: %d", mode), ui_x+quarterWidth-10*fontSize,ui_y);
}
}
}
void keyPressed()
{
if (key == 'd')
{
debug = !debug;
modeCount += debug ? 1 : -1;
} else if (key == 'm')
{
mode = (mode + 1) % modeCount;
} else if (key == 'r')
{
initializeFlowMap();
background(vectorToColor(HalfColor));
lastCameraUpdateTime = CameraUpdateResetValue;
} else if (key == 'f')
{
flipHorizontal = !flipHorizontal;
} else if (key == 'b')
{
doGridBilinearInterpolation = !doGridBilinearInterpolation;
} else if (key == CODED)
{
if (keyCode == LEFT) {
flowLinger -= 0.1;
} else if (keyCode == RIGHT) {
flowLinger += 0.1;
} else if (keyCode == DOWN) {
flowLinger = 0.5;
}
}
}