使用Opencv Android实现分水岭分段时出错

时间:2015-03-20 14:13:54

标签: java android opencv4android

我在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

1 个答案:

答案 0 :(得分:1)

我收到了类似的错误。我认为Imgproc存在问题。尝试在功能线之前放置Log.i("thistag", "$$");以查看哪个是创建问题的功能。就我而言,我发现了一些替代方法,而不是使用Imgproc。