opencv_core249d没有被指定在windows上运行,或者它包含错误
的有关信息介绍如下:整个项目的结构图:
编写***.java,代码如下:
[java]view
plaincopyprint?
packagecom.njupt.zhb.test;
importorg.opencv.core.Core;
i济mportorg.opencv.core.Mat;
importorg.opencv.core.MatOfRect;
impor来自torg.opencv.core.Point;
importorg.opencv.core.Rect;
importorg.opencv.core.Scalar;
importorg.opencv.highgui.Highgui;
impo衣知价员类操束清rtorg.opencv.ob地jdetect.CascadeClassifier;
//
//Detectsfacesinanimage,drawsbox候选esaroundthem,andwritestheresults
//to"faceDetection.png".
//
publicclassDetect白例思酸显财FaceDemo{
publicvoidrun(){
System.out.println("\n侵李宗口格帝停卷独干RunningDetectFaceDemo");
Syst360问答em.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());
//Createafacedetectorfrom染端调分按参找算保汉解thecascadefileintheresources
//directory.
//CascadeC肉县宽你朝望粮都田用lassifierface北理宜兰厂轮款Detector跑万被简话景曲害量应=newCascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());
//Matimage=Highgui.imread(getClass().getResource("lena.png").getPath());
//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误
/*
*Detected0fa院好抗术cesWritingfaceDetection.pnglibpngwarning:Image
*widthis场zeroinIHDRlibpngwarning:Imageheightiszer价示密oinIHDR
*libpngerror:InvalidIHDRdata
*/
//因此,我们将第一个字符去掉
StringxmlfilePath=getClass().getResource("lbpcascade_frontalface.读市益值升重十刚xml").getPath().substring(1);
CascadeClassifierfaceDe伤布让处自tector=newC而变松迫其识烈优里毛ascadeClassifier(xmlfilePath格火);
Matimage=H苦轴十医体误ighgui.imread(getClass().getResource("we.jpg").getPath().substring(1));
//Detectfacesintheimage.
//MatOfRectisaspecialcontainerclassforRect.
MatOfRectfaceDetections=newMatOfRect();
faceDetector.detectMultiScale(image,faceDetections);
System.out.println(String.format("Detected%sfaces",faceDetections.toArray().length));
//Drawaboundingboxaroundeachface.
for(Rectrect:faceDetections.toArray()){
Core.rectangle(image,newPoint(rect.x,rect.y),newPoint(rect.x+rect.width,rect.y+rect.height),newScalar(0,255,0));
}
//Savethevisualizeddetection.
Stringfilename="faceDetection.png";
System.out.println(String.format("Writing%s",filename));
Highgui.imwrite(filename,image);
}
}
packagecom.njupt.zhb.test;
importorg.opencv.core.Core;
importorg.opencv.core.Mat;
importorg.opencv.core.MatOfRect;
importorg.opencv.core.Point;
importorg.opencv.core.Rect;
importorg.opencv.core.Scalar;
importorg.opencv.highgui.Highgui;
importorg.opencv.objdetect.CascadeClassifier;
//
//Detectsfacesinanimage,drawsboxesaroundthem,andwritestheresults
//to"faceDetection.png".
//
publicclassDetectFaceDemo{
publicvoidrun(){
System.out.println("\nRunningDetectFaceDemo");
System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());
//Createafacedetectorfromthecascadefileintheresources
//directory.
//CascadeClassifierfaceDetector=newCascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());
//Matimage=Highgui.imread(getClass().getResource("lena.png").getPath());
//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误
/*
*Detected0facesWritingfaceDetection.pnglibpngwarning:Image
*widthiszeroinIHDRlibpngwarning:ImageheightiszeroinIHDR
*libpngerror:InvalidIHDRdata
*/
//因此,我们将第一个字符去掉
StringxmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);
CascadeClassifierfaceDetector=newCascadeClassifier(xmlfilePath);
Matimage=Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));
//Detectfacesintheimage.
//MatOfRectisaspecialcontainerclassforRect.
MatOfRectfaceDetections=newMatOfRect();
faceDetector.detectMultiScale(image,faceDetections);
System.out.println(String.format("Detected%sfaces",faceDetections.toArray().length));
//Drawaboundingboxaroundeachface.
for(Rectrect:faceDetections.toArray()){
Core.rectangle(image,newPoint(rect.x,rect.y),newPoint(rect.x+rect.width,rect.y+rect.height),newScalar(0,255,0));
}
//Savethevisualizeddetection.
Stringfilename="faceDetection.png";
System.out.println(String.format("Writing%s",filename));
Highgui.imwrite(filename,image);
}
}
3.编写测试类:
[java]view
plaincopyprint?
packagecom.njupt.zhb.test;
publicclassTestMain{
publicstaticvoidmain(String[]args){
System.out.println("Hello,OpenCV");
//Loadthenativelibrary.
System.loadLibrary("opencv_java246");
newDetectFaceDemo().run();
}
}
//运行结果:
//Hello,OpenCV
//
//RunningDetectFaceDemo
///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
//Detected8faces
//WritingfaceDetection.png
packagecom.njupt.zhb.test;
publicclassTestMain{
publicstaticvoidmain(String[]args){
System.out.println("Hello,OpenCV");
//Loadthenativelibrary.
System.loadLibrary("opencv_java246");
newDetectFaceDemo().run();
}
}
//运行结果:
//Hello,OpenCV
//
//RunningDetectFaceDemo
///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
//Detected8faces
//WritingfaceDetection.png