I developed an application for detecting head gestures using the OpenCV optical stream. I want to optimize my calculation method. Because now he is very slow. Could you offer me the best, fast and efficient way to do this?
I am currently comparing the X and Y coordinates for each function point between two frames to determine the direction of the optical stream. I want to reduce the number of functions to check the direction of the optical stream. Choose the minimum points that best represent the feature set.
Here is my code:
@Override
public Mat onCameraFrame(Mat inputFrame) {
up.value = 0;
down.value = 0;
left.value = 0;
right.value = 0;
pq.clear();
if (lMilliStart == 0)
lMilliStart = System.currentTimeMillis();
if ((lMilliNow - lMilliStart) > 10000) {
lMilliStart = System.currentTimeMillis();
lFrameCount = 0;
}
inputFrame.copyTo(mRgba);
sMatSize.width = mRgba.width();
sMatSize.height = mRgba.height();
switch (viewMode) {
case VIEW_MODE_OPFLOW:
if (mMOP2fptsPrev.rows() == 0) {
Imgproc.cvtColor(mRgba, matOpFlowThis, Imgproc.COLOR_RGBA2GRAY);
matOpFlowThis.copyTo(matOpFlowPrev);
Imgproc.goodFeaturesToTrack(matOpFlowPrev, MOPcorners, iGFFTMax, 0.05, 20);
mMOP2fptsPrev.fromArray(MOPcorners.toArray());
mMOP2fptsPrev.copyTo(mMOP2fptsSafe);
} else {
matOpFlowThis.copyTo(matOpFlowPrev);
Imgproc.cvtColor(mRgba, matOpFlowThis, Imgproc.COLOR_RGBA2GRAY);
Imgproc.goodFeaturesToTrack(matOpFlowThis, MOPcorners, iGFFTMax, 0.05, 20);
mMOP2fptsThis.fromArray(MOPcorners.toArray());
mMOP2fptsSafe.copyTo(mMOP2fptsPrev);
mMOP2fptsThis.copyTo(mMOP2fptsSafe);
}
Video.calcOpticalFlowPyrLK(matOpFlowPrev, matOpFlowThis, mMOP2fptsPrev, mMOP2fptsThis, mMOBStatus, mMOFerr);
cornersPrev = mMOP2fptsPrev.toList();
cornersThis = mMOP2fptsThis.toList();
byteStatus = mMOBStatus.toList();
y = byteStatus.size() - 1;
for (x = 0; x < y; x++) {
if (byteStatus.get(x) == 1) {
pt = cornersThis.get(x);
pt2 = cornersPrev.get(x);
double m = Math.abs(pt2.y - pt.y ) / Math.abs(pt2.x - pt.x);
double distance= Math.sqrt(Math.pow((pt.x - pt2.x),2) + Math.pow((pt.y - pt2.y),2));
if(distance < NOISE)
continue;
if (pt.x < pt2.x && pt2.y < pt.y)
if (m > 1)
up.value++;
else
right.value++;
else if (pt.x < pt2.x && pt2.y == pt.y)
right.value++;
else if (pt.x < pt2.x && pt2.y > pt.y)
if (m > 1)
down.value++;
else
right.value++;
else if (pt.x == pt2.x && pt2.y > pt.y)
down.value++;
else if (pt.x > pt2.x && pt2.y > pt.y)
if (m > 1)
down.value++;
else
left.value++;
else if (pt.x > pt2.x && pt2.y == pt.y)
left.value++;
else if (pt.x > pt2.x && pt2.y < pt.y)
if (m > 1)
up.value++;
else
left.value++;
else if (pt.x == pt2.x && pt2.y < pt.y)
up.value++;
Core.circle(mRgba, pt, 5, colorRed, iLineThickness - 1);
Core.line(mRgba, pt, pt2, colorRed, iLineThickness);
}
}
Direction r1, r2, r3;
if(up.value == 0 && left.value == 0 && right.value == 0 && down.value == 0) {
string = String.format("Direction: ---");
showTitle(string, 3, colorRed);
}else{
if (left.value < right.value) {
r1 = right;
} else r1 = left;
if (up.value < down.value) {
r2 = down;
} else r2 = up;
if (r1.value < r2.value) {
r3 = r2;
} else r3 = r1;
string = String.format("Direction: %s", r3.name);
for (HeadGestureListener listener : listeners) {
listener.onHeadGestureDetected(r3.name);
}
showTitle(string, 3, colorRed);
}
if (bDisplayTitle)
showTitle("Optical Flow", 1, colorGreen);
break;
}
lMilliNow = System.currentTimeMillis();
lFrameCount++;
if (bDisplayTitle) {
string = String.format("FPS: %2.1f", (float) (lFrameCount * 1000) / (float) (lMilliNow - lMilliStart));
showTitle(string, 2, colorGreen);
}
if (System.currentTimeMillis() - lMilliShotTime < 1500)
showTitle(sShotText, 3, colorRed);
return mRgba;
}