detectMultiScale函数一加入就会崩溃,程序自动停止但不报错,求解! 40C

根据教程制作了人脸识别的程序,但是detectMultiScale无法正常运行,一旦加入就会使程序停止,求大神你帮帮我,很急!
初始化模型

public void initializeOpenCVDependencies() {
        try {
            // Copy the resource into a temp file so OpenCV can load it
                 InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface_improved);
                 File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
                 File mCascadeFile = new File(cascadeDir.getAbsoluteFile(), "lbpcascade_frontalface_improved.xml");
                 FileOutputStream os = new FileOutputStream(mCascadeFile);
                 byte[] buffer = new byte[4096];
                 int bytesRead=0;
                 while ((bytesRead = is.read(buffer)) != -1) {
                     os.write(buffer, 0, bytesRead);
                 }
                 is.close();
                 os.close();
                 // Load the cascade classifier
            CascadeClassifier detectface = new CascadeClassifier(mCascadeFile.getAbsolutePath());
           // detectface.load("lbpcascade_frontalface_improved.xml");
        } catch (Exception e) {
            Log.e("OpenCVActivity", "Error loading cascade", e);
        } 

检测段

public void procSrc2Gray(){
        Mat rgbMat = new Mat();
        Mat grayMat = new Mat();
        Mat guess= new Mat();
        Mat canny=new Mat();
        Mat lines=new Mat();

        MatOfRect car=new MatOfRect();







        srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);
        grayBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.RGB_565);
        Utils.bitmapToMat(srcBitmap, rgbMat);//convert original bitmap to Mat, R G B.

        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY);//rgbMat to gray grayMat
        MatOfRect cars=new MatOfRect();
     detectface.detectMultiScale(grayMat,car,1.1,3,0,new Size(50,50),new Size());

补充问题日志

 Process: com.example.myapplication, PID: 3254
    java.lang.NullPointerException: Attempt to invoke virtual method 'void org.opencv.objdetect.CascadeClassifier.detectMultiScale(org.opencv.core.Mat, org.opencv.core.MatOfRect, double, int, int, org.opencv.core.Size, org.opencv.core.Size)' on a null object reference
        at com.example.myapplication.MainActivity.procSrc2Gray(MainActivity.java:139)
        at com.example.myapplication.MainActivity$ProcessClickListener.onClick(MainActivity.java:188)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
```不知道为什么这个就变成空指针了=。=

1个回答

日志日志日志,新手博客先看一遍https://blog.csdn.net/weimingjue/article/details/87921494

weimingjue
王能 回复weixin_44613287: 空指针异常博客也有描述,再进去看看就明白了了
大约一年之前 回复
weixin_44613287
weixin_44613287 Process: com.example.myapplication, PID: 3254 java.lang.NullPointerException: Attempt to invoke virtual method 'void org.opencv.objdetect.CascadeClassifier.detectMultiScale(org.opencv.core.Mat, org.opencv.core.MatOfRect, double, int, int, org.opencv.core.Size, org.opencv.core.Size)' on a null object reference at com.example.myapplication.MainActivity.procSrc2Gray(MainActivity.java:139) at com.example.myapplication.MainActivity$ProcessClickListener.onClick(MainActivity.java:188) at android.view.View.performClick(View.java:4780) at android.view.View$PerformClick.run(View.java:19866) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
大约一年之前 回复
weixin_44613287
weixin_44613287 感谢评论,日志文件是
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐