java.lang.NullPointerException when I use HOGDescriptor.compute(frame, descriptors)

asked 2017-08-02 15:39:55 -0500

Agnieszka gravatar image

I'm trying compute HOGDescriptor from a frame on android with openCV 3.2.0. Before I have made it in python and it's working. When I use it on android i have error:

E/AndroidRuntime: FATAL EXCEPTION: Thread-24279 Process: com.example.agnieszka.openvctest, PID: 14567
java.lang.NullPointerException: Attempt to read from field 'long org.opencv.core.Mat.nativeObj' on a null object reference

at org.opencv.objdetect.HOGDescriptor.compute(

at com.example.agnieszka.openvctest.HogDescriptor.compute( at com.example.agnieszka.openvctest.MainActivity.onCameraFrame( at at$ at

The code below:

public class HogDescriptor {
    HOGDescriptor Hog;
    int nbins = 9;
    int derivAperture = 1;
    double winSigma = -1.0;
    int histogramNormType = 0;
    double L2HysThreshold = 0.2;
    boolean gammaCorrection = true;
    int nlevels = 64;
    MatOfFloat descriptors;
    Mat frameB = new Mat();

    public HogDescriptor()
        Hog = new HOGDescriptor(new org.opencv.core.Size(60, 100),new org.opencv.core.Size(40, 40), new org.opencv.core.Size(20, 20),new org.opencv.core.Size(10, 10), nbins, derivAperture, winSigma, histogramNormType, L2HysThreshold, gammaCorrection, nlevels);

    void compute(Mat frame)
            Imgproc.resize(frame, frameB, winSize,1,1, Imgproc.INTER_AREA);
            Hog.compute(frameB, descriptors);
    MatOfFloat getDescriptors()
        return this.descriptors;
    Mat getFrameB()
        return this.frameB;

Mat frameB I can preview on the phone screen and I can make some processing but when I use method Hog.compute() i have this error. There are fragments from MainActivity :

private HogDescriptor hog;
public void onCameraViewStarted(int width, int height) {
    appStatus = CALIBRATION;
    mRgba = new Mat(height, width, CvType.CV_8UC4);
    mMask = new Mat(height, width, CvType.CV_8SC1);
    imProc = new ImProcessing(height,width);
    hog = new HogDescriptor();

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    mMask = imProc.backgroungRemove(mRgba, appStatus);
    mRgba = imProc.drawContours(mRgba,mMask);
    return mMask;

Anyone can help me why I get NullPointerException in this case?

edit retag flag offensive close merge delete