calcHist java Null pointer Exception

asked 2016-12-27 12:34:34 -0600

Hi, I am new to openCV. Please help me resolve "null pointer exception" issue when I execute the below code. Thanks in advance.

/* Program */ import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfFloat; import org.opencv.core.MatOfInt; import org.opencv.imgproc.Imgproc;

import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList;

import static org.opencv.core.Core.split; import static org.opencv.imgcodecs.Imgcodecs.imread;

public class Manu {

static{
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    System.out.println("Executing library "+Core.NATIVE_LIBRARY_NAME);
}

public static void main(String[] args) {
    System.out.println("Welcome to OpenCV " + Core.VERSION);
    Path p = null;
    try {
        p = Paths.get(new File(".").getCanonicalPath() + "/src/resource/ostracon - design.png");
    } catch (IOException e) {
        e.printStackTrace();
    }
    Mat src, dst;
    src = imread(p.toString()+" ");
    if(src.empty()){
        System.out.println("Image could not be loaded..");
        System.exit(1);
    }
    ArrayList<Mat> rgb_planes = new ArrayList<Mat>(3);
    split(src,rgb_planes);
    int histSize = 256;
    float range[] = {0,256};
    float histRange[][] = {range};
    boolean uniform = true;
    boolean accumulate = false;
    Mat rHist = null,gHist,bHist;
    Imgproc.calcHist(rgb_planes,new MatOfInt(1),new Mat(),rHist, new MatOfInt(histSize),new MatOfFloat(range),accumulate);
}

}

Error that I am facing -

Executing library opencv_java310 Welcome to OpenCV 3.1.0 Exception in thread "main" java.lang.NullPointerException at org.opencv.imgproc.Imgproc.calcHist(Imgproc.java:1487) at master.Manu.main(Manu.java:51) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 1

edit retag flag offensive close merge delete

Comments

1

can you try to initialize rhist properly, like

 Mat rHist = new Mat();
berak gravatar imageberak ( 2016-12-27 13:34:15 -0600 )edit

hi thanks. so silly of me! But I am having newer problems now. I can't plot the histogram! I found this answer on stackoverflow but it is for One dims. I am working on RGB planes all together for which I wish to find histogram. Can you help me with that ?

Manasvini Ganesh gravatar imageManasvini Ganesh ( 2016-12-28 08:50:37 -0600 )edit

most easy thing would be to draw 3 curves, one per histogram

berak gravatar imageberak ( 2016-12-28 09:11:28 -0600 )edit