Opencv canny 边缘检测在 Android 应用程序上崩溃 [英] Opencv canny edge detection crashes on Android app

查看:28
本文介绍了Opencv canny 边缘检测在 Android 应用程序上崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 OpenCV 的新手,当我尝试应用 Canny 检测器时,我的应用程序崩溃了.我看过另一篇关于相同的帖子,但我试过了,但没有......

I'm new using OpenCV and when I try to apply the Canny detector, my App crashes. I've seen another post about the same but I tried it and nothing...

我使用的是 Android Studio 3.0、OpenCV 3.2.0,我的手机是 Android 6.0.1.

I use Android Studio 3.0, OpenCV 3.2.0 and my phone is Android 6.0.1.

这是我的代码.我尝试了很多组合,但在尝试应用 Canny 检测器时总是失败.请注意最后一个方法 onCameraFrame...它适用于 Rgba 和 Gray 图像,但不适用于 Canny:

This is my code. I tried lots of combinations and always fails when I try to apply the Canny detector. Please pay attention at the last method onCameraFrame...It works perfect with both Rgba and Gray image, but not with the Canny:

public class MainActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2{


    private static String TAG = "MainActivity";
    JavaCameraView javaCameraView;
    Mat mRgba, imgGray, imgCanny, wide;
    BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status){
            switch(status){
                case BaseLoaderCallback.SUCCESS:{
                    javaCameraView.enableView();
                    break;
                }
                default:{
                    super.onManagerConnected(status);
                    break;
                }
            }
        }
    };

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        javaCameraView = (JavaCameraView) findViewById(R.id.java_camera_view);
        javaCameraView.setVisibility(SurfaceView.VISIBLE);
        javaCameraView.setCvCameraViewListener(this);
    }

    @Override
    protected void onPause(){
        super.onPause();
        if(javaCameraView!=null)
            javaCameraView.disableView();
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        if(javaCameraView!=null)
            javaCameraView.disableView();
    }

    @Override
    protected void onResume(){
        super.onResume();
        if(OpenCVLoader.initDebug()){
            Log.i(TAG, "Opencv loaded succesfully");
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        } else {
            Log.i(TAG, "Opencv not loaded");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_2_0, this, mLoaderCallback);
        }
    }


    @Override
    public void onCameraViewStarted(int width, int height) {
        mRgba = new Mat (height, width, CvType.CV_8UC4);
        imgGray = new Mat (height, width, CvType.CV_8UC1);
        imgCanny = new Mat (height, width, CvType.CV_8UC1);
    }

    @Override
    public void onCameraViewStopped() {
        mRgba.release();
    }

    @Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        imgGray = inputFrame.gray();
        //mRgba = inputFrame.rgba();

        //Imgproc.cvtColor(mRgba, imgGray, Imgproc.COLOR_RGB2GRAY);
        Imgproc.Canny(imgGray, imgCanny, 50, 150); //detector canny

        return imgCanny;
    }

}

我也想知道是否可以捕捉图像并将其保存到图库中.也许使用 onTouch 方法?非常感谢!

I also like to know if it's possible to catch the image and save it into the gallery. Maybe with the onTouch method? Thank you very much!

推荐答案

我认为应用在遇到 Canny 检测器时会崩溃,因为您的设备上安装了错误类型的 OpenCV 管理器,无论是版本号还是中央处理器指令放.检查正确的版本应该很简单.只需转到 OpenCV-android-sdk\apk 目录并检查 OpenCV_

I believe the app is crashing when it hits the Canny detector because of the wrong type of OpenCV Manager installed on your device, be it version number or central processor instruction set. Checking the correct version should be straightforward. Just go to the OpenCV-android-sdk\apk directory and check for the 3 (x.y.z) numbers after OpenCV_

设备在执行 Imgproc.Canny 函数调用时应该会崩溃.

The device should be crashing when it executes the Imgproc.Canny function call.

检查适用于 Windows 的 Android 设备指令集

要检查设备的指令集,请导航到通常位于以下位置的 adb(android 调试桥)目录:

To check the instruction set of your device, navigate to the adb (android debug bridge) directory commonly located at:

C:\Users\<'你的用户名'>\AppData\Local\Android\Sdk\platform-tools运行命令:

C:\Users\<'your username'>\AppData\Local\Android\Sdk\platform-tools Run the command:

./adb.exe shell cat /proc/cpuinfo

获得正确的指令集后,导航回 OpenCV-android-sdk\apk 并找到要安装在您的 android 设备上的正确 apk 版本和指令集.

After getting the correct instruction set, navigate back to the OpenCV-android-sdk\apk and locate the correct apk version and instruction set to be installed on your android device.

然后您可以将 apk 传输到您的设备并进行安装.我发现有用的另一种方法是导航到 adb.exe 目录并运行命令:

You can then transfer the apk to your device and install it. Another way I find useful is to navigate to the adb.exe directory and run the command:

./adb.exe install <path to OpenCV-android-sdk>/apk/OpenCV_x.y.z_Manager_x.yz_<platform instruction set>.apk

除上述步骤外,请确保您没有任何其他使用其他类型的 OpenCV 管理器的环境变量,例如在 Application.mk 或 build.gradle 文件中声明不同的环境变量.

Apart from the steps above, make sure that you do not have any other environment variables that use other types of OpenCV Manager such as stating a different one in the Application.mk or build.gradle files.

完成上述步骤后,您的 Canny 检测器应该能够在您的设备上运行而不会崩溃.

After the steps above, your Canny detector should be able to run on your device without crashing.

关于你的第二部分关于将图片保存到图库的问题,请参考Android开发者Camera/Camera2 API的抓图部分:https://developer.android.com/guide/topics/media/相机#capture-picture

As for the second part of your question on saving the image to the gallery, please refer to the capturing pictures part of the Camera/Camera2 API for android developers: https://developer.android.com/guide/topics/media/camera#capture-picture

快乐发展:)

这篇关于Opencv canny 边缘检测在 Android 应用程序上崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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