forked from atduskgreg/opencv-processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlow.java
More file actions
139 lines (110 loc) · 2.83 KB
/
Copy pathFlow.java
File metadata and controls
139 lines (110 loc) · 2.83 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package gab.opencv;
import processing.core.*;
import java.awt.Rectangle;
import org.opencv.video.Video;
import org.opencv.core.Mat;
import org.opencv.core.CvType;
import org.opencv.core.Core;
import org.opencv.core.Scalar;
public class Flow {
private Mat prev;
private Mat flow;
private boolean hasFlow = false;
private double pyramidScale = 0.5;
private int nLevels = 4;
private int windowSize = 8;
private int nIterations = 2;
private int polyN = 7;
private double polySigma = 1.5;
private int runningFlags = Video.OPTFLOW_FARNEBACK_GAUSSIAN;
private PApplet parent;
public Flow(PApplet parent) {
flow = new Mat();
this.parent = parent;
}
public int width(){
return flow.width();
}
public int height(){
return flow.height();
}
public boolean hasFlow(){
return hasFlow;
}
public Mat getFlowMat(){
return flow;
}
public void calculateOpticalFlow(Mat m) {
int flags = runningFlags;
if (!hasFlow) {
prev = m.clone();
flags = Video.OPTFLOW_USE_INITIAL_FLOW;
hasFlow = true;
}
Video.calcOpticalFlowFarneback(prev, m, flow, pyramidScale, nLevels, windowSize, nIterations, polyN, polySigma, flags);
prev = m.clone();
}
public PVector getTotalFlowInRegion(int x, int y, int w, int h) {
Mat region = flow.submat(y, y+h, x, x+w);
Scalar total = Core.sumElems(region);
return new PVector((float)total.val[0], (float)total.val[1]);
}
public PVector getAverageFlowInRegion(int x, int y, int w, int h) {
PVector total = getTotalFlowInRegion(x, y, w, h);
return new PVector(total.x/(w*h), total.y/(w*h));
}
public PVector getTotalFlow() {
return getTotalFlowInRegion(0, 0, flow.width(), flow.height());
}
public PVector getAverageFlow() {
return getAverageFlowInRegion(0, 0, flow.width(), flow.height());
}
public PVector getFlowAt(int x, int y){
return new PVector((float)flow.get(y, x)[0], (float)flow.get(y, x)[1]);
}
public void draw() {
int stepSize = 4;
for (int y = 0; y < flow.height(); y+=stepSize) {
for (int x = 0; x < flow.width(); x+=stepSize) {
PVector flowVec = getFlowAt(x,y);
parent.line(x, y, x+flowVec.x, y+flowVec.y);
}
}
}
public void setPyramidScale(double v){
pyramidScale = v;
}
public double getPyramidScale(){
return pyramidScale;
}
public void setLevels(int n){
nLevels = n;
}
public int getLevels(){
return nLevels;
}
public void setWindowSize(int s){
windowSize = s;
}
public int getWindowSize(){
return windowSize;
}
public void setIterations(int i){
nIterations = i;
}
public int getIterations(){
return nIterations;
}
public void setPolyN(int n){
polyN = n;
}
public int getPolyN(){
return polyN;
}
public void setPolySigma(double s){
polySigma = s;
}
public double getPolySigma(){
return polySigma;
}
}