求助,opencv车辆识别报错 20C

错误信息

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)

说是空指针但是找不到问题在哪里。。

求好心人帮帮我。。

前面我有给分类器赋值

public void initializeOpenCVDependencies() {
        try {
            // Copy the resource into a temp file so OpenCV can load it
            InputStream is = getResources().openRawResource(R.raw.cars);
            File cascadeDir = getDir("cars", Context.MODE_PRIVATE);
            File mCascadeFile = new File(cascadeDir.getAbsoluteFile(), "cars.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
            detectface = new CascadeClassifier(mCascadeFile.getAbsolutePath());
        } catch (Exception e) {
            Log.e("OpenCVActivity", "Error loading cascade", e);
        }         
    }
```请问是不是赋值语句有问题?

1个回答

我觉得说的已经很明显了,你看一下为什么这个“CascadeClassifier”类是空的,,,

weixin_44613287
weixin_44613287 但是我前面有给他赋值啊,不应该是空的,赋值语句:public void initializeOpenCVDependencies() { try { // Copy the resource into a temp file so OpenCV can load it InputStream is = getResources().openRawResource(R.raw.cars); File cascadeDir = getDir("cars", Context.MODE_PRIVATE); File mCascadeFile = new File(cascadeDir.getAbsoluteFile(), "cars.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 detectface = new CascadeClassifier(mCascadeFile.getAbsolutePath()); } catch (Exception e) { Log.e("OpenCVActivity", "Error loading cascade", e); } }
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐