Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android OpenCV:光流计算优化_Android_Opencv_Optimization_Opticalflow - Fatal编程技术网

Android OpenCV:光流计算优化

Android OpenCV:光流计算优化,android,opencv,optimization,opticalflow,Android,Opencv,Optimization,Opticalflow,我开发了一个使用OpenCV光流检测头部姿势的应用程序。我想优化我的计算方法。因为现在速度很慢。你能给我建议一个更好、更快、更有效的方法吗 目前我正在比较两帧之间每个特征点的X和Y坐标,以确定光流方向。我想减少要检查的特征数量,以找到光流方向。选择最能代表要素集的最小要素点 这是我的代码: @Override public Mat onCameraFrame(Mat inputFrame) { up.value = 0; down.value =

我开发了一个使用OpenCV光流检测头部姿势的应用程序。我想优化我的计算方法。因为现在速度很慢。你能给我建议一个更好、更快、更有效的方法吗

目前我正在比较两帧之间每个特征点的X和Y坐标,以确定光流方向。我想减少要检查的特征数量,以找到光流方向。选择最能代表要素集的最小要素点

这是我的代码:

    @Override
    public Mat onCameraFrame(Mat inputFrame) {
        up.value = 0;
        down.value = 0;
        left.value = 0;
        right.value = 0;
        pq.clear();

        // start the timing counter to put the frame rate on screen
        // and make sure the start time is up to date, do
        // a reset every 10 seconds
        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) {

                // Log.d("Baz", "First time opflow");
                // first time through the loop so we need prev and this mats
                // plus prev points
                // get this mat
                Imgproc.cvtColor(mRgba, matOpFlowThis, Imgproc.COLOR_RGBA2GRAY);

                // copy that to prev mat
                matOpFlowThis.copyTo(matOpFlowPrev);

                // get prev corners
                Imgproc.goodFeaturesToTrack(matOpFlowPrev, MOPcorners, iGFFTMax, 0.05, 20);
                mMOP2fptsPrev.fromArray(MOPcorners.toArray());

                // get safe copy of this corners
                mMOP2fptsPrev.copyTo(mMOP2fptsSafe);
            } else {
                // Log.d("Baz", "Opflow");
                // we've been through before so
                // this mat is valid. Copy it to prev mat
                matOpFlowThis.copyTo(matOpFlowPrev);

                // get this mat
                Imgproc.cvtColor(mRgba, matOpFlowThis, Imgproc.COLOR_RGBA2GRAY);

                // get the corners for this mat
                Imgproc.goodFeaturesToTrack(matOpFlowThis, MOPcorners, iGFFTMax, 0.05, 20);
                mMOP2fptsThis.fromArray(MOPcorners.toArray());

                // retrieve the corners from the prev mat
                // (saves calculating them again)
                mMOP2fptsSafe.copyTo(mMOP2fptsPrev);

                // and save this corners for next time through
                mMOP2fptsThis.copyTo(mMOP2fptsSafe);
            }

            /*
             * Parameters: prevImg first 8-bit input image nextImg second input
             * image prevPts vector of 2D points for which the flow needs to be
             * found; point coordinates must be single-precision floating-point
             * numbers. nextPts output vector of 2D points (with
             * single-precision floating-point coordinates) containing the
             * calculated new positions of input features in the second image;
             * when OPTFLOW_USE_INITIAL_FLOW flag is passed, the vector must
             * have the same size as in the input. status output status vector
             * (of unsigned chars); each element of the vector is set to 1 if
             * the flow for the corresponding features has been found,
             * otherwise, it is set to 0. err output vector of errors; each
             * element of the vector is set to an error for the corresponding
             * feature, type of the error measure can be set in flags parameter;
             * if the flow wasn't found then the error is not defined (use the
             * status parameter to find such cases).
             */
            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);
                }
            }//end of for

            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);
            }

            //Log.d("Mukcay",pq.poll().name );
            // Log.d("Baz", "Opflow feature count: "+x);
            if (bDisplayTitle)
                showTitle("Optical Flow", 1, colorGreen);
                break;
        }

        // get the time now in every frame
        lMilliNow = System.currentTimeMillis();

        // update the frame counter
        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;
    }
@覆盖
公共Mat onCameraFrame(Mat inputFrame){
up.value=0;
down.value=0;
left.value=0;
右值=0;
pq.clear();
//启动定时计数器,将帧速率显示在屏幕上
//确保开始时间是最新的,是吗
//每10秒重置一次
如果(lMilliStart==0)
lMilliStart=System.currentTimeMillis();
如果((lmillNow-lmillStart)>10000){
lMilliStart=System.currentTimeMillis();
lFrameCount=0;
}
inputFrame.copyTo(mRgba);
sMatSize.width=mRgba.width();
sMatSize.height=mRgba.height();
开关(查看模式){
案例视图\模式\操作流程:
if(mMOP2fptsPrev.rows()==0){
//Log.d(“Baz”,“首次opflow”);
//第一次穿过环路,所以我们需要prev和这个垫子
//加上以前的分数
//拿这个垫子
Imgproc.cvtColor(mRgba、matOpFlowThis、Imgproc.COLOR_RGBA2GRAY);
//把它复制到上一张纸上
matOpFlowThis.copyTo(matOpFlowPrev);
//获取上一个角
Imgproc.良好特性跟踪(matOpFlowPrev,MOPcorners,iGFFTMax,0.05,20);
mMOP2fptsPrev.fromArray(MOPcorners.toArray());
//拿到这本书的安全副本
mMOP2fptsPrev.copyTo(mMOP2fptsSafe);
}否则{
//Log.d(“Baz”、“Opflow”);
//我们以前也经历过这样的事情
//此mat有效。请将其复制到上一个mat
matOpFlowThis.copyTo(matOpFlowPrev);
//拿这个垫子
Imgproc.cvtColor(mRgba、matOpFlowThis、Imgproc.COLOR_RGBA2GRAY);
//把这个垫子的角弄到手
Imgproc.良好特性跟踪(matOpFlowThis,MOPcorners,iGFFTMax,0.05,20);
mMOP2fptsThis.fromArray(MOPcorners.toArray());
//从上一张垫子中检索角点
//(保存再次计算)
mMOP2fptsSafe.copyTo(mMOP2fptsPrev);
//把这些角落留到下次用
mMOP2fptsThis.copyTo(mMOP2fptsSafe);
}
/*
*参数:上一个8位输入图像下一个下一个第二个输入
*需要计算流的2D点的图像矢量
*已找到;点坐标必须为单精度浮点
*数字。2D点的nextPts输出向量(带
*单精度浮点坐标),包含
*计算第二图像中输入特征的新位置;
*当传递OPTFLOW\u USE\u INITIAL\u FLOW标志时,向量必须
*与input.status输出状态向量中的大小相同
*(指无符号字符);如果
*已找到相应特征的流程,
*否则,将其设置为0。错误的输出向量;每个
*向量的元素被设置为对应的
*可以在flags参数中设置错误度量的特征、类型;
*如果未找到流,则未定义错误(使用
*状态参数以查找此类案例)。
*/
calcOpticalFlowPyrLK(matOpFlowPrev、matOpFlowThis、mMOP2fptsPrev、mMOP2fptsThis、mMOBStatus、mMOFerr);
cornersPrev=mMOP2fptsPrev.toList();
cornersThis=mMOP2fptsThis.toList();
byteStatus=mMOBStatus.toList();
y=byteStatus.size()-1;
对于(x=0;x1)
up.value++;
其他的
对.value++;
else if(pt.xpt.y)
如果(m>1)
down.value++;
其他的
对.value++;
else if(pt.x==pt2.x&&pt2.y>pt.y)
down.value++;
否则如果(pt.x>pt2.x&&pt2.y>pt.y)
如果(m>1)
down.value++;
其他的
左。值++;
else if(pt.x>pt2.x&&pt2.y==pt.y)
左。值++;
否则如果(pt.x>pt2.x&&pt2.y1)
up.value++;
其他的
左。值++;
else if(pt.x==pt2.x&&pt2.y