如何设置的OpenCV的摄像头,显示preVIEW在纵向方向和全屏 [英] How to set OpenCV's camera to display preview in both portrait orientation and full screen

查看:4545
本文介绍了如何设置的OpenCV的摄像头,显示preVIEW在纵向方向和全屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Android应用程序。这显示在任何时候都照相机preVIEW在纵向模式,并执行一些沉重的图像处理操作的(一些),它的帧。因此,我使用的OpenCV(两者是OpenCV4Android和原生的C / C ++组件)。的问题是,使用CameraBridgeViewBase或JavaCameraView类时,它是由OnCameraFrame返回的帧是在风景模式。

I'm trying to create an Android app. which displays a camera preview in portrait mode at all times and performs some heavy image processing operations on (some of) it's frames. Hence, I'm using OpenCV (both of it's OpenCV4Android and native C/C++ components). The problem is that when using CameraBridgeViewBase or JavaCameraView classes, the frame which is returned by OnCameraFrame is in Landscape mode.

现在,如果活动被定义为使用风景模式(就像OpenCV的的示例应用程序),在preVIEW看起来很好,但任何额外的用户界面视图倾斜90度(而如前所述,设备应运行我应用在人像模式)。

Now, if the Activity is defined to use Landscape mode (just like OpenCV's sample apps), the preview looks fine but any additional UI views are tilted by 90 degrees (and as mentioned before, the device should run my App in Portrait mode).

如果该活动被设置为肖像模式,UI观点显然看起来正确,但摄像头preVIEW将90度倾斜。

If the Activity is set to portrait mode, UI views will obviously look right, but camera preview will be tilted by 90 degrees.

如果我试图通过操纵图像矩阵上OnCameraFrame像这样旋转每一帧:

If I try to rotate each frame by manipulating the image Matrix on OnCameraFrame like this:

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
  mRgba = inputFrame.rgba();
  Core.transpose(mRgba,mRgbaT);
  Core.flip(mRgbaT, mRgbaT, -1);
  Imgproc.resize(mRgbaT, mRgbaT, mRgba.size());

  return mRgbaT;
}

然后每个相机的帧填充设备的宽度,但不它的高度(并且因此,它看起来streched),再加 - 它大大减慢了帧速率。试图调整图像到全屏(或比原来的帧大小任何规模大小不同)的结果在所有(黑屏)显示无图像及以下异常被抛出:

then each camera's frame fills device's width but not it's height (and as a result, it looks streched), plus - it considerably slows down the frame rate. Trying to resize the image to fullscreen (or any size different than the original frame size) results in displaying no image at all (black screen) and the following exception is thrown:

E/CameraBridge(11183): Utils.matToBitmap() throws an exception: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/java/generator/src/cpp/utils.cpp:97: error: (-215) src.dims == 2 && info.height == (uint32_t)src.rows && info.width == (uint32_t)src.cols in function void Java_org_opencv_android_Utils_nMatToBitmap2(JNIEnv*, jclass, jlong, jobject, jboolean)

所以,我的问题是:如何显示的OpenCV的摄像头preVIEW在纵向模式和全屏

So, my question is: How can I display OpenCV's camera preview in both portrait mode and full screen?

不幸的是,由于我初学SO名声我不能附加屏幕截图。此外,我意识到这一点有点类似问题,所以在被要求这样的事实,但没有答案似乎彻底解决问题。

Unfortunately, due to my beginner SO reputation I can't attach screen-shots. Also, I'm aware of the fact that somewhat similar questions were asked on SO before, but none of the answers seem to solve the problem completely.

推荐答案

我已经找到了解决办法: 创建从CameraBridgeViewBase在JavaCameraView从它延伸以类似的方式扩展(大部分地区甚至相同),但实现内部JavaCameraFrame类时,请更换它返回一个垫目标,以这样的方法自定义相机类:

I've found a solution: Create a custom camera class which extends from CameraBridgeViewBase in a similar way that JavaCameraView extends from it (most parts are even identical), but when implementing the inner JavaCameraFrame class, replace the method which returns a Mat object to something like this:

    public Mat rgba() {
        Imgproc.cvtColor(mYuvFrameData, mRgba, Imgproc.COLOR_YUV2BGR_NV12, 4);
        if (mRotated != null)
            mRotated.release();
        mRotated = mRgba.t();
        Core.flip(mRotated, mRotated, -1);
        return mRotated;
    }

有一个伟大示例的这里(第一个答案,礼貌 Zarokka )。有一些性能降级,但它不是远程严重的旋转,输出垫在OnCameraFrame(不完全解决问题反正)时。我用它在一个640 * 480分辨率preVIEW帧尺寸(全屏幕),它看起来不错,工程进展顺利,甚至不会不那么新的设备。

There is a great example here (The first answer, courtesy of Zarokka ). There is some downgrade in performance, but it's remotely not as serious as when rotating the output Mat in OnCameraFrame (which doesn't fully solve the problem anyway). I use it on a 640*480 resolution preview frame size (full screen) and it looks good and works smoothly even not not-so new devices.

这篇关于如何设置的OpenCV的摄像头,显示preVIEW在纵向方向和全屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆