Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have started to learn OpenCV using JAVA language. I tried to run very simple code borrowed from http://docs.opencv.org/2.4.4-beta/doc/tutorials/introduction/desktop_java/java_dev_intro.html

and using the eclipse (JUNO). When I run the following codes

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;

//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
class DetectFaceDemo {
  public void run() {
    System.out.println("\nRunning DetectFaceDemo");

    // Create a face detector from the cascade file in the resources
    // directory.
    CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("/lbpcascade_frontalface.xml").getPath());
    Mat image = Highgui.imread(getClass().getResource("/lena.png").getPath());

    // Detect faces in the image.
    // MatOfRect is a special container class for Rect.
    MatOfRect faceDetections = new MatOfRect();
    faceDetector.detectMultiScale(image, faceDetections);

    System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));

    // Draw a bounding box around each face.
    for (Rect rect : faceDetections.toArray()) {
        Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
    }

    // Save the visualized detection.
    String filename = "faceDetection.png";
    System.out.println(String.format("Writing %s", filename));
    Highgui.imwrite(filename, image);
  }
}

public class HelloOpenCV {
  public static void main(String[] args) {
    System.out.println("Hello, OpenCV");

    // Load the native library.
    System.loadLibrary("opencv_java245");
    new DetectFaceDemo().run();
  }
}

I get

Hello, OpenCV

Running DetectFaceDemo
Exception in thread "main" java.lang.NullPointerException
    at DetectFaceDemo.run(HelloOpenCV.java:20)
    at HelloOpenCV.main(HelloOpenCV.java:48)

where **

20     CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());
48    new DetectFaceDemo().run();

**

I am new in this area and do not know how to get ride of this error. I need your help.

share|improve this question

2 Answers

up vote 0 down vote accepted

getClass().getResource() loads resources from the classpath and if file is not found it will return null. So in your case it seems file is not loaded and when you call getPath(), you get null pointer exception. You need to check that, however you're building, the xml file gets copied over to wherever the class files are compiled to. Or simply make sure that xml file is present in your classpath.

Hope it helps!

share|improve this answer
 
Thank you for quick reply you were right. The error is removed but I got this error <pre> Hello, OpenCV Running DetectFaceDemo Detected 0 faces Writing faceDetection.png libpng warning: Image width is zero in IHDR libpng warning: Image height is zero in IHDR libpng error: Invalid IHDR data </pre> –  Hamid May 25 at 23:51
 
One part of the problem is solved by copying the image and XML files to my classpath. However I noticed that the result of running getClass().getResource("/lena.png").getPath()) contains an extra "/" : "/D:/MFiles/Java/HelloOpenCV/bin/lena.png ". I added a small code to remove the extra slash sign. Something like this str = str.replace("/D:","D:"); –  Hamid May 26 at 14:31
 
@Hamid Yes that extra slash is a result of some bug fix in jdk. Here you can find more details: bugs.sun.com/bugdatabase/view_bug.do?bug_id=4466485. I am afraid ou will have to do something by yourself to remove that extra slash. Hope your string replace is working fine. –  Juned Ahsan May 26 at 14:36
 
Yes, finally the code is working fine now. –  Hamid May 26 at 15:44

give full path Mat image = Highgui.imread("/Users/test/workspace/OpenCV/bin/lena.png");

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.