【问题标题】:Running opencv using java error使用java错误运行opencv
【发布时间】:2013-05-21 05:30:50
【问题描述】:

我已经开始使用 JAVA 语言学习 OpenCV。我试图运行从 http://docs.opencv.org/2.4.4-beta/doc/tutorials/introduction/desktop_java/java_dev_intro.html

并使用日食 (JUNO)。当我运行以下代码时

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();
  }
}

我明白了

Hello, OpenCV

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

在哪里 **

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

**

我是这个领域的新手,不知道如何解决这个错误。我需要你的帮助。

【问题讨论】:

    标签: java eclipse opencv


    【解决方案1】:

    getClass().getResource() 从类路径加载资源,如果找不到文件,它将返回 null。因此,在您的情况下,似乎未加载文件,并且当您调用 getPath() 时,会出现空指针异常。您需要检查,无论您如何构建,xml 文件都会被复制到类文件编译到的任何位置。或者只是确保 xml 文件存在于您的类路径中。

    希望对你有帮助!

    【讨论】:

    • 感谢您的快速回复,您是对的。错误已删除,但我收到此错误
      您好,OpenCV Running DetectFaceDemo Detected 0 faces Writing faceDetection.png libpng 警告:IHDR libpng 警告中的图像宽度为零:IHDR libpng 中的图像高度为零错误:IHDR 数据无效前>
    • 通过将图像和 XML 文件复制到我的类路径来解决部分问题。但是我注意到运行 getClass().getResource("/lena.png").getPath()) 的结果包含一个额外的“/”:“/D:/MFiles/Java/HelloOpenCV/bin/lena.png” .我添加了一个小代码来删除多余的斜杠符号。像这样 str = str.replace("/D:","D:");
    • @Hamid 是的,额外的斜线是 jdk 中一些错误修复的结果。在这里您可以找到更多详细信息:bugs.sun.com/bugdatabase/view_bug.do?bug_id=4466485。恐怕您将不得不自己做一些事情来删除多余的斜线。希望您的字符串替换工作正常。
    • 能不能分享 Mat image = Highgui.imread 的工作线..........................
    【解决方案2】:

    给出完整路径 Mat image = Highgui.imread("/Users/test/workspace/OpenCV/bin/lena.png");

    【讨论】:

      猜你喜欢
      • 2011-03-27
      • 2015-07-28
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 2012-07-28
      相关资源
      最近更新 更多