1

I was implementing watershed segmentation on android and I happen to get an error. The error reads

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

Please help me with it. Here is the code:

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;
        }
    }

Here is my 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
2
  • any help with this issue? Commented Oct 25, 2016 at 5:39
  • i'm really in need of help with this question, can you help me, please? stackoverflow.com/questions/61216402/… Commented Apr 28, 2020 at 17:16

1 Answer 1

1

I am getting similar error. I think there is problem with the Imgproc. Try putting Log.i("thistag", "$$"); before fuction lines to see which is the function that is creating the problem. In my case I found some alternative than using Imgproc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.