简体   繁体   中英

How to fix NullPointerException in opencv

I wanted to implement Gabor Filter with opencv in java in eclipse environment :

  public static void main( String[] args ) throws IOException
       {

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat mat = Highgui.imread("./img/lena.jpg");

        Mat dst =new Mat();

        Imgproc.cvtColor(mat, dst, Imgproc.COLOR_RGB2GRAY);

        dst.convertTo(source_f, CvType.CV_64F, (1.0/255) , 0.0);
.
.
.
}

But when it use convertTo Function, it display with error :

Exception in thread "main" java.lang.NullPointerException
at org.opencv.core.Mat.convertTo(Mat.java:959)
at testOpenCV.GaborFilter.main(GaborFilter.java:175)

I searched about that and tried display Mat object to knew where it was null but couldn't.

How can I fix it, please or even know where the null is?

I don't see a declaration of source_f in the snippet you posted. You might want to initialize source_f before pointing to it using convertTo , perhaps something like:

Mat dst = new Mat();
Mat source_f = new Mat();
Imgproc.cvtColor(mat, dst, Imgproc.COLOR_RGB2GRAY);
dst.convertTo(source_f, CvType.CV_64F, (1.0/255) , 0.0);

Unfortunately I haven't used the java api much, so not 100% sure the syntax is correct.

Also, it's a good idea to go step by step and test your assumptions:

  1. Is the image correctly loaded(with imread ), if so, display it on screen ?
  2. Is dst correctly displaying the pixels after the gray scale conversion ?
  3. Finally, is source_f displaying the expected output after the conversion ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM