2016年1月17日 星期日

OpenCV 2.4.11 with JAVA and Netbeans

1. Add opencv-2411.jar and opencv_java2411.dll to your libraries in netbeans.
ex:    C:\Users\Desktop\opencv\build\java\2.4.11\opencv-2411.jar
         C:\Users\Desktop\opencv\build\java\2.4.11\x64  

2. Modify VM Options in Run of netbeans
ex:    -Djava.library.path="C:\Users\Desktop\opencv\build\java\2.4.11\x64"

3. add below code in Main
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

4. Modify source code and copy files "lbpcascade_frontalface.xml" and "lena.png" to your src folder in netbeans.
From:
    CascadeClassifier faceDetector = new  CascadeClassifier(getClass().getResource("/lbpcascade_frontalface.xml").getPath());
    Mat image = Highgui.imread(getClass().getResource("/lena.png").getPath());

To:  (For short term solution if you can't find the file that will show null point result)
    CascadeClassifier faceDetector = new CascadeClassifier(".\\src\\lbpcascade_frontalface.xml");
    Mat image = Highgui.imread(".\\src\\lena.png");

4. Run it and that will generate a faceDetection.png file if your settings is well.

BTW, if using openCV 3.1.0 below source code should be changed, but I still got fail.

From
Mat image = Highgui.imread(".\\src\\lena.png");
Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
 
To
Mat image = Imgcodecs.imread("C:\\Users\\kyc1109\\Documents\\NetBeansProjects\\HelloOpenCV\\src\\lena.png");
Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));



Reference page :
http://stackoverflow.com/questions/26845885/java-opencv-org-opencv-core-core-rectangle-method-missing

http://stackoverflow.com/questions/17134126/open-cv-detecting-faces-in-java

Netbeans settings:
http://www.codeproject.com/Tips/717283/How-to-Use-OpenCV-with-Java-under-NetBeans-IDE