整个项目的结构图:
编写DetectFaceDemo.java,代码如下:
[java] view
plaincopyprint?
package com.njupt.zhb.test
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".
//
public class DetectFaceDemo {
public void run() {
System.out.println("\nRunning DetectFaceDemo")
System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath())
// 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())
//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误
/*
* 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
*/
//因此,我们将第一个字符去掉
String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1)
CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath)
Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1))
// 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)
}
}
package com.njupt.zhb.test
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".
//
public class DetectFaceDemo {
public void run() {
System.out.println("\nRunning DetectFaceDemo")
System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath())
// 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())
//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误
/*
* 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
*/
//因此,我们将第一个字符去掉
String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1)
CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath)
Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1))
// 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)
}
}
3.编写测试类:
[java] view
plaincopyprint?
package com.njupt.zhb.test
public class TestMain {
public static void main(String[] args) {
System.out.println("Hello, OpenCV")
// Load the native library.
System.loadLibrary("opencv_java246")
new DetectFaceDemo().run()
}
}
//运行结果:
//Hello, OpenCV
//
//Running DetectFaceDemo
///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
//Detected 8 faces
//Writing faceDetection.png
package com.njupt.zhb.test
public class TestMain {
public static void main(String[] args) {
System.out.println("Hello, OpenCV")
// Load the native library.
System.loadLibrary("opencv_java246")
new DetectFaceDemo().run()
}
}
//运行结果:
//Hello, OpenCV
//
//Running DetectFaceDemo
///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
//Detected 8 faces
//Writing faceDetection.png
方法/步骤1安装opencv2.4.9,解压,请务必记住自己解压的路径。宜家以我自己的路径为例D:\Program Files
2
配置环境变量
1、系统变量 Path:添加D:\Program Files\opencv\build\x86\vc12\bin
2、用户变量:添加opencv变量,值D:\Program Files\opencv\build,添加PATH变量(有就不需要添加,但是值需要添加)值D:\Program Files\opencv\build\x86\vc12\bin
说明:不管你系统是32位还是64位,路脊昌径目录均选择X86,因为编译都是使用32位编译;如果选用X64,则程序运行时候会出错。
3
新建visual C项目
新建 visual C++项目,如下图所示,项目选项注意:如下图。
4
工程目录的配置(Debug)
找到属性管理器,如果找不到,请安装下图方法找到。双击Debug|Win32打开如下窗口,
设置如下:(下图红框项为世漏设置项)
1、包含樱返扒目录:(VC++目录)
D:\Program Files\opencv\build\include
D:\Program Files\opencv\build\include\opencv
D:\Program Files\opencv\build\include\opencv2
2、库目录:(VC++目录)D:\Program Files\opencv\build\x86\vc12\lib
3、连接器->输入->附加依赖项:
opencv_ml249d.lib
opencv_calib3d249d.lib
opencv_contrib249d.lib
opencv_core249d.lib
opencv_features2d249d.lib
opencv_flann249d.lib
opencv_gpu249d.lib
opencv_highgui249d.lib
opencv_imgproc249d.lib
opencv_legacy249d.lib
opencv_objdetect249d.lib
opencv_ts249d.lib
opencv_video249d.lib
opencv_nonfree249d.lib
opencv_ocl249d.lib
opencv_photo249d.lib
opencv_stitching249d.lib
opencv_superres249d.lib
opencv_videostab249d.lib
其实以上都是D:\Program Files\opencv\build\x86\vc12\lib下所有的lib文件,你会发现,有的后面带上d,有的没有d,这是因为Debug的就有d,Release则没有d。
图片路径写对了吗?请把测试图像放到D:\works\testimage.jpg下,然后扮禅在程序里使用这个绝蔽缺碧对路径调用之。
把程序贴出来看看。宏举
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)