本文介绍了使用OpenCV for Android将小图像复制到相机框架中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个应该在相机框架上绘制一个小图像的程序。使用Android OpenCV,您可以使用以下函数来处理框架:

I'm currently working on a program that should draw a small image onto the camera frame. With Android OpenCV, you do have the following function to process a frame:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

      Mat rgba = inputFrame.rgba();

      mDetector.setFrame(rgba);
      mDetector.processFrame();

      return rgba;
}

然后Mat rgba显示在屏幕上。我的探测器现在应该处理帧rgba(改变它)。
以下是相关代码:

Where the Mat rgba then gets displayed on the screen. My Detector should now process the frame rgba (change it).Here is the relevant code:

public void processFrame() {

    // (1) Doesn't work
    Rect roi = new Rect(0, 0, 100, 100);
    Mat submat = mOutputFrame.submat(roi);
    Mat image =  new Mat(100, 100, CvType.CV_8UC3, new Scalar(0,0,0));
    image.copyTo(submat);

    // (2) Does work
    // --- mComparatorImage is the same size as mOutputFrame.
    // --- mComparatorImage is 8bit greyscale, mOutputFrame is the rgba CameraFrame
    mComparatorImage = mComparatorHolder.getCurrentImage();
    mComparatorImage.copyTo(mOutputFrame);

    // (3) Should work (but doesn't)
    Imgproc.resize(mComparatorImage, mResizedImageClone, new Size (200, 100));
    Mat bSubmat = mOutputFrame.submat(new Rect(0, 0, 200, 100));
    mResizedImageClone.copyTo(bSubmat);
}

我要做的是将调整大小的mComparatorImage版本复制到由mOutputFrame引用的相机框架(mOutputFrame = rgba)。

What I'm trying to do is to copy a resized version of mComparatorImage into the camera frame that is referenced by mOutputFrame (mOutputFrame = rgba).

所以我尝试了(3)。
FYI:mResizedImageClone的类型为Mat,并初始化为新的Mat()

So I tried doing (3).FYI: mResizedImageClone is of type Mat and is initialized as a new Mat()

执行(3)不会更改mOutputFrame。

Doing (3) doesn't change the mOutputFrame.

(2)然后我尝试将整个mComparatorImage(类型为Mat,大小与mOutputFrame相同)复制到mOutputFrame。这真是令人惊讶。

(2) Then I tried copying the whole mComparatorImage (type Mat and same size as mOutputFrame) to mOutputFrame. This worked suprisingly.

(1)然后我认为问题必须与submat有关,因为复制大图像有效,但将其小版本复制到mOutputFrame犯规。所以我尝试将一个小黑图复制到mOutputFrame中。这也不起作用,虽然我在这里遵循其他答案。

(1) Then I thought the problem has to be something with submat, because copying the big image works, but copying a small version of it into mOutputFrame doesnt. So I tried copying a little black image into mOutputFrame. This doesn't work either, although I followed other answers here.

可能是什么问题?没有错误,但相机框架在(1)和(3)中保持不变

What could be the problem? There is no error, but the camera frame stays the same in (1) and (3)

如果您需要任何其他信息,请告诉我。

If you need any additional info, let me know.

Isa

推荐答案

好的,我找到了它,它有一点点很难。

Okay, I've found it, it was a little bit tricky.

如果src和dest Mat属于同一类型,使用子矩阵的copyTo函数只能正常工作。否则,它只是...没有。 (它应该抱怨!)

The copyTo function using submatrices works only properly if the src and the dest Mat are of the same type. Otherwise, it just does ... nothing. (It rather should complain!)

我没有使用rect,而是使用带参数的submat(row_start,row_end,col_start,col_end)

Instead of using rect, I used submat with parameters (row_start, row_end, col_start, col_end)

另请注意,submat(#cols和#rows)的尺寸必须与copyTo中使用的src图像完全匹配。

Also be aware that the dimensions of the submat (#cols and #rows) have to exactly match the src image that is used in copyTo.

所以这是我对(1)的解决方案:

So here is my solution for (1):

// (1) Inserting a little black rect into the camera frame:
Mat submat = mOutputFrame.submat(0, 100, 0, 100);
Mat image =  new Mat(100, 100, mOutputFrame.type(), new Scalar(0,0,0));
image.copyTo(submat);

我的(3)解决方案:

// (3) Resizing and inserting an arbitrary grey image into the rgba camera frame
Imgproc.resize(mComparatorImage, mResizedImageClone, new Size (200, 100));
Imgproc.cvtColor(mResizedImageClone, mResizedImageClone, Imgproc.COLOR_GRAY2RGBA);
Mat submat = mOutputFrame.submat(0, 100, 0, 200);
mResizedImageClone.copyTo(submat);

这篇关于使用OpenCV for Android将小图像复制到相机框架中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:38