1

我正在android上实现分水岭分割,我碰巧遇到了一个错误。错误读取

 java.lang.NullPointerException:Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference

请帮帮我。这是代码:

public Mat steptowatershed(Mat img)
 {
     Mat threeChannel = new Mat();

    Imgproc.cvtColor(img, threeChannel, Imgproc.COLOR_BGR2GRAY);
    Imgproc.threshold(threeChannel, threeChannel, 100, 255, Imgproc.THRESH_BINARY);

    Mat fg = new Mat(img.size(),CvType.CV_8U);
    Imgproc.erode(threeChannel,fg,new Mat());

    Mat bg = new Mat(img.size(),CvType.CV_8U);
    Imgproc.dilate(threeChannel,bg,new Mat());
    Imgproc.threshold(bg,bg,1, 128,Imgproc.THRESH_BINARY_INV);

    Mat markers = new Mat(img.size(),CvType.CV_8U, new Scalar(0));
    Core.add(fg, bg, markers);

    WatershedSegmenter segmenter = new WatershedSegmenter();
    segmenter.setMarkers(markers);
    Mat result = segmenter.process(img);
    return result;
 }
 public class WatershedSegmenter
     {
        public Mat markers;

        public void setMarkers(Mat markerImage)
        {
            markerImage.convertTo(markers, CvType.CV_32S);
        }

        public Mat process(Mat image)
        {
            Imgproc.watershed(image, markers);
            markers.convertTo(markers,CvType.CV_8U);
            return markers;
        }
    }

这是我的Logcat:

03-20 19:09:34.050: E/AndroidRuntime(13948): FATAL EXCEPTION: main
03-20 19:09:34.050: E/AndroidRuntime(13948): Process: net.viralpatel.android.imagegalleray, PID: 13948
03-20 19:09:34.050: E/AndroidRuntime(13948): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/14963 flg=0x1 }} to activity {net.viralpatel.android.imagegalleray/net.viralpatel.android.imagegalleray.ImageGalleryDemoActivity}: java.lang.NullPointerException: Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference
03-20 19:09:34.050: E/AndroidRuntime(13948):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3626)
03-20 19:09:34.050: E/AndroidRuntime(13948):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3669)
03-20 19:09:34.050: E/AndroidRuntime(13948):    at android.app.ActivityThread.access$1300(ActivityThread.java:148)
03-20 19:09:34.050: E/AndroidRuntime(13948):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
03-20 19:09:34.050: E/AndroidRuntime(13948):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-20 19:09:34.050: E/AndroidRuntime(13948):    at android.os.Looper.loop(Looper.java:135)
03-20 19:09:34.050: E/AndroidRuntime(13948):    at android.app.ActivityThread.main(ActivityThread.java:5312)
03-20 19:09:34.050: E/AndroidRuntime(13948):    at java.lang.reflect.Method.invoke(Native Method)
03-20 19:09:34.050: E/AndroidRuntime(13948):    at java.lang.reflect.Method.invoke(Method.java:372)
03-20 19:09:34.050: E/AndroidRuntime(13948):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
03-20 19:09:34.050: E/AndroidRuntime(13948):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
03-20 19:09:34.050: E/AndroidRuntime(13948): Caused by: java.lang.NullPointerException: Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference
03-20 19:09:34.050: E/AndroidRuntime(13948):    at org.opencv.core.Mat.convertTo(Mat.java:1010)
03-20 19:09:34.050: E/AndroidRuntime(13948):    at net.viralpatel.android.imagegalleray.ImageGalleryDemoActivity$WatershedSegmenter.setMarkers(ImageGalleryDemoActivity.java:141)
03-20 19:09:34.050: E/AndroidRuntime(13948):    at net.viralpatel.android.imagegalleray.ImageGalleryDemoActivity.steptowatershed(ImageGalleryDemoActivity.java:131)
03-20 19:09:34.050: E/AndroidRuntime(13948):    at net.viralpatel.android.imagegalleray.ImageGalleryDemoActivity.onActivityResult(ImageGalleryDemoActivity.java:105)
03-20 19:09:34.050: E/AndroidRuntime(13948):    at android.app.Activity.dispatchActivityResult(Activity.java:6161)
03-20 19:09:34.050: E/AndroidRuntime(13948):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3622)
03-20 19:09:34.050: E/AndroidRuntime(13948):    ... 10 more
4

1 回答 1

1

我收到类似的错误。我认为 Imgproc 有问题。尝试 Log.i("thistag", "$$");在功能行之前放置,看看是哪个功能造成了问题。就我而言,我找到了一些比使用 Imgproc 的替代方法。

于 2016-02-19T14:21:32.120 回答