下载安装
从官网下载安装
Windows操作系统下进行安装
https://opencv.org/releases/
下载最新版
安装到本地
依赖
<!-- https://mvnrepository.com/artifact/org.bytedeco/opencv -->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>opencv</artifactId>
<version>4.5.3-1.5.6</version>
</dependency>
开发
实体
public class CvtMatEntity {
//原图Mat
public Mat img;
//灰度图Mat
public Mat gray;
public static CvtMatEntity cvtR2G(Mat img){
CvtMatEntity cvtMatEntity = new CvtMatEntity();
Mat rgb = new Mat();
//实现图片灰度转换
Imgproc.cvtColor(img, rgb, Imgproc.COLOR_BGR2RGB);
Mat gray = new Mat();
Imgproc.cvtColor(rgb, gray, Imgproc.COLOR_RGB2GRAY);
//赋值
cvtMatEntity.img = img;
cvtMatEntity.gray = gray;
//返回
return cvtMatEntity;
}
}
核心类
public class InitInstance {
private static Logger logger = LoggerFactory.getLogger(InitInstance.class);
//脸部识别实例
private static CascadeClassifier faceDetector;
//此类加载人脸识别模块
public static void init(String dllAbsPath, String facexmlAbsPath, String eyexmlAbsPath) {
logger.info("开始读取脸部识别实例");
//加载dll文件
System.load(dllAbsPath);
faceDetector = new CascadeClassifier(facexmlAbsPath);
if (faceDetector.empty()) {
logger.error("人脸识别模块读取失败");
} else logger.info("人脸识别模块读取成功");
}
//此类实现打开视频,识别人脸
public static void videoDetectorModel() {
//打开摄像头
VideoCapture videoCapture = new VideoCapture(0);
//判断摄像头是否打开
if (!videoCapture.open(0)) {
logger.error("相机打开失败");
return;
}
while (true) {
//创建图片Mat
Mat img = new Mat();
//读取摄像头下的图像
if (!videoCapture.read(img)) return;
//为保证教程详细度,此处不调用实体方法,大家可自行选择
//图片灰度转化
Mat rgb = new Mat();
Imgproc.cvtColor(img, rgb, Imgproc.COLOR_BGR2RGB);
Mat gray = new Mat();
Imgproc.cvtColor(rgb, gray, Imgproc.COLOR_RGB2GRAY);
//创建人脸识别出的矩形变量
MatOfRect faveRect = new MatOfRect();
//检测人脸
faceDetector.detectMultiScale(gray, faveRect);
//图形面勾选人脸
for (Rect re : faveRect.toArray()) {
Imgproc.rectangle(img, new Point(re.x, re.y), new Point(re.x + re.width, re.y + re.height), new Scalar(0, 0, 255), 2);
}
//显示在屏幕
HighGui.imshow("人脸识别", img);
//按\'q\'退出
if (HighGui.waitKey(1) == 81) break;
}
//释放资源
videoCapture.release();
HighGui.destroyAllWindows();
}
//以下内容为对比人脸模块。与打开视频,识别人脸完全分离
/**
* 获取灰度人脸
*/
public static Mat conv_Mat(String img) {
//读取图片Mat
Mat imgInfo = Imgcodecs.imread(img);
//此处调用了实体方法,实现灰度转化
CvtMatEntity cvtMatEntity = CvtMatEntity.cvtR2G(imgInfo);
//创建Mat矩形
MatOfRect faceMat = new MatOfRect();
//识别人人脸
faceDetector.detectMultiScale(cvtMatEntity.gray, faceMat);
for (Rect rect : faceMat.toArray()) {
//选出灰度人脸
Mat face = new Mat(cvtMatEntity.gray, rect);
return face;
}
return null;
}
/**
* 图片对比人脸
*/
public static double compare_image(String img_1, String img_2) {
//获得灰度人脸
Mat mat_1 = conv_Mat(img_1);
Mat mat_2 = conv_Mat(img_2);
Mat hist_1 = new Mat();
Mat hist_2 = new Mat();
//参数定义
MatOfFloat ranges = new MatOfFloat(0f, 256f);
MatOfInt histSize = new MatOfInt(10000000);
//实现图片计算
Imgproc.calcHist(Arrays.asList(mat_1), new MatOfInt(0), new Mat(), hist_1, histSize, ranges);
Imgproc.calcHist(Arrays.asList(mat_2), new MatOfInt(0), new Mat(), hist_2, histSize, ranges);
// 相关系数,获得相似度
double res = Imgproc.compareHist(hist_1, hist_2, Imgproc.CV_COMP_CORREL);
//返回相似度
return res;
}
}
调用
public class openapiMainApplication {
public static void main(String[] args) throws UnsupportedEncodingException {
//此为opencv的opencv_java453.dll
//位置在opencv安装目录下的build\\java\\x64\\位置
//可以将该dll文件放在系统 Path目录下
String dllAbsPath = "D:\\opencv\\build\\java\\x64\\opencv_java453.dll";
//位置在opencv安装目录下的sources\\data\\haarcascades\\位置
String facexmlAbsPath = "D:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
//必须加载
InitInstance.init(dllAbsPath, facexmlAbsPath,eyexmlAbsPath);
}
}