OpenCv之人脸检测
openCv人类检测-java
导入相关文件
lbpcascade_frontalface.xml
opencv-490.jar
opencv_java490.dll
编写示例
public class OpenCvFace {
static {
//这里加载dll文件
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
public static void main(String[] args) {
String file ="you file path";
Mat src = Imgcodecs.imread(file);
Mat newSrc = Imgcodecs.imread(file);
//加载模型文件
CascadeClassifier classifier = new CascadeClassifier("lbpcascade_frontalface.xml");
MatOfRect faceDetections = new MatOfRect();
classifier.detectMultiScale(src, faceDetections);
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
// 295像素×413像素
// Drawing boxes
for (Rect rect : faceDetections.toArray()) {
Imgproc.rectangle(
newSrc, // where to draw the box
new Point(rect.x , rect.y), // bottom left
new Point(rect.x + rect.width, rect.y + rect.height), // top right
new Scalar(0, 0, 255),
3 // RGB colour
);
}
// Writing the image
Imgcodecs.imwrite("temp/cv.jpg", newSrc);
System.out.println("Image Processed");
// 使用HighGui库显示原始图像和修改后的图像
HighGui.imshow("原图", src);
HighGui.imshow("新图", newSrc);
}
}
添加关注,回复“人脸检测” 获取demo示例

本文介绍了如何在Java中使用OpenCV进行人脸检测,包括导入相关jar和dll文件,加载预训练模型,以及编写一个简单的示例代码,展示了如何在图像上检测并绘制人脸矩形框。
8861

被折叠的 条评论
为什么被折叠?



