首页 > 解决方案 > 如何使用 Android 有条件地启动 Opencv 实时图像处理?

问题描述

我正在使用 Opencv 开发一个 Android 应用程序来执行一些繁重的图像处理,包括检测最大轮廓、裁剪检测到的轮廓、应用分割逻辑以及将每个分割轮廓的相似性与参考对象数组匹配。

我以 3 的 fps 实时完成处理逻辑,平均处理时间为 0.4 秒,这在我的情况下很好。

问题是该项目将用于一个行业,并且我想只有在产品在相机视野中时才开始处理框架。

我已经进行了某种运动检测来检测是否有一些轮廓移动,然后启动算法,但工业机器地毯也在移动,所以这种方法行不通。

这是运动检测部分的代码:

   @Override
   public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    long e1 = Core.getTickCount();
    contours.clear();
    //gray frame because it requires less resource to process
    mGray = inputFrame.gray();

    //this function converts the gray frame into the correct RGB format for the BackgroundSubtractorMOG apply function
    Imgproc.cvtColor(mGray, mRgb, Imgproc.COLOR_GRAY2RGB);

    //apply detects objects moving and produces a foreground mask
    //the lRate updates dynamically dependent upon seekbar changes
    sub.apply(mRgb, mFGMask, lRate);

    //erode and dilate are used  to remove noise from the foreground mask
    Imgproc.erode(mFGMask, mFGMask, new Mat());
    Imgproc.dilate(mFGMask, mFGMask, new Mat());

    //drawing contours around the objects by first called findContours and then calling drawContours
    //RETR_EXTERNAL retrieves only external contours
    //CHAIN_APPROX_NONE detects all pixels for each contour
    Imgproc.findContours(mFGMask, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);

    //draws all the contours in red with thickness of 2
    Imgproc.drawContours(mRgb, contours, -1, new Scalar(255, 0, 0), 2);

    long e2 = Core.getTickCount();
    long e = e2 - e1;
    double time = e / Core.getTickFrequency();

    Log.d("timeTAG", "" + contours.size());


    return mRgb;
}

你有什么建议作为这个问题的解决方案?

标签: androidopencvimage-processingcomputer-visionopencv4android

解决方案


推荐阅读