OpenCV报错

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*
 原因:路径问题,java代码getPath()返回的路径前面多个/,c++中不识别,解决方法如下。
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 = Imgcodecs.imread(getClass().getResource("/lena.png").getPath());

 修改为:

final URI xmlUri = getClass().getResource("/lbpcascade_frontalface.xml").toURI();
final CascadeClassifier faceDetector = new CascadeClassifier(new File(xmlUri).getAbsolutePath());
final URI imageUri = getClass().getResource("/4.jpg").toURI();
Mat image = Imgcodecs.imread(new File(imageUri).getAbsolutePath());
posted @ 2017-07-11 17:04  ACaptain  阅读(2727)  评论(0编辑  收藏  举报