简体   繁体   English

使用OpenCV for Android将小图像复制到相机框架中

[英]Copying a small image into the camera frame with OpenCV for Android

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: 使用Android OpenCV,您可以使用以下功能来处理帧:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

      Mat rgba = inputFrame.rgba();

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

      return rgba;
}

Where the Mat rgba then gets displayed on the screen. Mat rgba然后显示在屏幕上。 My Detector should now process the frame rgba (change it). 我的探测器现在应该处理帧rgba(改变它)。 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); 
}

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). 我要做的是将调整大小的mComparatorImage版本复制到mOutputFrame(mOutputFrame = rgba)引用的相机框架中。

So I tried doing (3). 所以我尝试了(3)。 FYI: mResizedImageClone is of type Mat and is initialized as a new Mat() 仅供参考:mResizedImageClone的类型为Mat,并初始化为新的Mat()

Doing (3) doesn't change the mOutputFrame. 执行(3)不会更改mOutputFrame。

(2) Then I tried copying the whole mComparatorImage (type Mat and same size as mOutputFrame) to mOutputFrame. (2)然后我尝试将整个mComparatorImage(类型为Mat,与mOutputFrame大小相同)复制到mOutputFrame。 This worked suprisingly. 这令人惊讶。

(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. (1)然后我认为问题必须是submat,因为复制大图像有效,但是将它的一个小版本复制到mOutputFrame中并不行。 So I tried copying a little black image into mOutputFrame. 所以我尝试将一个小黑图复制到mOutputFrame中。 This doesn't work either, although I followed other answers here. 这也不起作用,虽然我在这里遵循其他答案。

What could be the problem? 可能是什么问题呢? There is no error, but the camera frame stays the same in (1) and (3) 没有错误,但相机框架在(1)和(3)中保持不变

If you need any additional info, let me know. 如果您需要任何其他信息,请告诉我们。

Isa 伊萨

Okay, I've found it, it was a little bit tricky. 好的,我发现它,这有点棘手。

The copyTo function using submatrices works only properly if the src and the dest Mat are of the same type. 如果src和dest Mat属于同一类型,则使用子矩阵的copyTo函数只能正常工作。 Otherwise, it just does ... nothing. 否则,它只是...没有。 (It rather should complain!) (它应该抱怨!)

Instead of using rect, I used submat with parameters (row_start, row_end, col_start, col_end) 我没有使用rect,而是使用带参数的submat(row_start,row_end,col_start,col_end)

Also be aware that the dimensions of the submat (#cols and #rows) have to exactly match the src image that is used in copyTo. 另请注意,submat(#cols和#rows)的尺寸必须与copyTo中使用的src图像完全匹配。

So here is my solution for (1): 所以这是我对(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);

And my solution for (3): 我的解决方案(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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM