首页 文章

OpenCV(Android) - 删除绘制的轮廓

提问于
浏览
1

应用程序检测框架上的特定颜色并使用OpenCV绘制轮廓 . 当单击捕获按钮时,帧图像将用于进行一些图像处理,而绘制的轮廓也是用帧捕获的,这不是我想要的 . 我的问题是如何删除单击捕获按钮时绘制的轮廓 . 或者有没有任何方法来获得没有绘制轮廓的框架?

我试过的方法:

  • 锁定onCapture()直到调用onCameraFrame并在调用drawContour()之前返回mRbg .

  • 将mRgba克隆到新Mat并使用新Mat作为subColor的参数

但他们两个都没有用 .

我想暂停onCapture()直到onCameraFrame调用并多次跳过绘制轮廓线以确保框架上没有绘制任何内容 . 但我不知道如何处理两个synchronized() .

public boolean onTouch(View v, MotionEvent event) {
    lock = true;
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        //do something
    } else if (event.getAction() == MotionEvent.ACTION_UP) {
        //do something
        //↓to make sure onCameraFrame is pause before the finger left the screen
        lock = false;
        synchronized (locker) { locker.notify(); }
    }

    return true;
} 

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    //Pause until onTouch() is done
    commandLocker();

    //Detect the contour
    mDetector.setHsvColor(txtHsv);
    if (area) {
        nRgba = mRgba.submat(ey, sy, sx, ex);
        mDetector.process(nRgba);
    } else {
        mDetector.process(mRgba);
    }

    //Skip this when onCapture is called
    //Draw the contour on the frame
    if (!capture) {
        List<MatOfPoint> contours = mDetector.getContours();
        if (nRgba != null && area) {
            Imgproc.rectangle(mRgba, new Point(sx, sy), new Point(ex, ey), areaColor, 3);
            Imgproc.drawContours(nRgba, contours, -1, contourColor);
        } else
            Imgproc.drawContours(mRgba, contours, -1, contourColor);
    }

    return mRgba;
}

public void onCapture(View view) throws IOException {
    capture = true;
    //Pause until onCameraFrame() done
    if (!area)
        subColor(mRgba);
    else
        subColor(nRgba);
}

public void subColor (Mat src) throws IOException {
    //do something
}

private void commandLocker() {
    synchronized (locker) {
        while (lock) {
            try {
                locker.wait();
            } catch (InterruptedException e) {
                Log.e("Exception", "InterruptedException");
            }
        }
    }
}

Frame captured without processing

Processed image

1 回答

  • 0

    在你的 onCameraFrame(CvCameraViewFrame inputFrame) 函数中,如果条件:

    if (nRgba != null)

    不满意,你做 Imgproc.drawContours(mRgba, contours, -1, contourColor); 覆盖原来的垫子 mRgba

    那是你的问题吗?

相关问题