简体   繁体   English

如何在opencv中修复NullPointerException

[英]How to fix NullPointerException in opencv

I wanted to implement Gabor Filter with opencv in java in eclipse environment : 我想在eclipse环境中用java在opencv中实现Gabor Filter:

  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 : 但是当使用convertTo函数时,显示错误:

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. 我搜索了一下,并尝试显示Mat对象,以了解它在哪里为空但不能为空。

How can I fix it, please or even know where the null is? 我如何解决它,请甚至知道null在哪里?

I don't see a declaration of source_f in the snippet you posted. 我在您发布的代码段中看不到source_f的声明。 You might want to initialize source_f before pointing to it using convertTo , perhaps something like: 您可能想要在使用convertTo指向source_f之前初始化source_f,也许像这样:

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. 不幸的是,我没有使用过太多的Java api,因此不能100%确定语法正确。

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 ? 图像是否正确加载(带有imread ),如果是,则将其显示在屏幕上?
  2. Is dst correctly displaying the pixels after the gray scale conversion ? 灰度转换后, dst是否正确显示像素?
  3. Finally, is source_f displaying the expected output after the conversion ? 最后,source_f是否在转换后显示预期的输出?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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