下载安装

从官网下载安装
Windows操作系统下进行安装
https://opencv.org/releases/
下载最新版
安装到本地

依赖

  1. <!-- https://mvnrepository.com/artifact/org.bytedeco/opencv -->
  2. <dependency>
  3. <groupId>org.bytedeco</groupId>
  4. <artifactId>opencv</artifactId>
  5. <version>4.5.3-1.5.6</version>
  6. </dependency>

开发

实体

  1. public class CvtMatEntity {
  2. //原图Mat
  3. public Mat img;
  4. //灰度图Mat
  5. public Mat gray;
  6. public static CvtMatEntity cvtR2G(Mat img){
  7. CvtMatEntity cvtMatEntity = new CvtMatEntity();
  8. Mat rgb = new Mat();
  9. //实现图片灰度转换
  10. Imgproc.cvtColor(img, rgb, Imgproc.COLOR_BGR2RGB);
  11. Mat gray = new Mat();
  12. Imgproc.cvtColor(rgb, gray, Imgproc.COLOR_RGB2GRAY);
  13. //赋值
  14. cvtMatEntity.img = img;
  15. cvtMatEntity.gray = gray;
  16. //返回
  17. return cvtMatEntity;
  18. }
  19. }

核心类

  1. public class InitInstance {
  2. private static Logger logger = LoggerFactory.getLogger(InitInstance.class);
  3. //脸部识别实例
  4. private static CascadeClassifier faceDetector;
  5. //此类加载人脸识别模块
  6. public static void init(String dllAbsPath, String facexmlAbsPath, String eyexmlAbsPath) {
  7. logger.info("开始读取脸部识别实例");
  8. //加载dll文件
  9. System.load(dllAbsPath);
  10. faceDetector = new CascadeClassifier(facexmlAbsPath);
  11. if (faceDetector.empty()) {
  12. logger.error("人脸识别模块读取失败");
  13. } else logger.info("人脸识别模块读取成功");
  14. }
  15. //此类实现打开视频,识别人脸
  16. public static void videoDetectorModel() {
  17. //打开摄像头
  18. VideoCapture videoCapture = new VideoCapture(0);
  19. //判断摄像头是否打开
  20. if (!videoCapture.open(0)) {
  21. logger.error("相机打开失败");
  22. return;
  23. }
  24. while (true) {
  25. //创建图片Mat
  26. Mat img = new Mat();
  27. //读取摄像头下的图像
  28. if (!videoCapture.read(img)) return;
  29. //为保证教程详细度,此处不调用实体方法,大家可自行选择
  30. //图片灰度转化
  31. Mat rgb = new Mat();
  32. Imgproc.cvtColor(img, rgb, Imgproc.COLOR_BGR2RGB);
  33. Mat gray = new Mat();
  34. Imgproc.cvtColor(rgb, gray, Imgproc.COLOR_RGB2GRAY);
  35. //创建人脸识别出的矩形变量
  36. MatOfRect faveRect = new MatOfRect();
  37. //检测人脸
  38. faceDetector.detectMultiScale(gray, faveRect);
  39. //图形面勾选人脸
  40. for (Rect re : faveRect.toArray()) {
  41. 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);
  42. }
  43. //显示在屏幕
  44. HighGui.imshow("人脸识别", img);
  45. //按\'q\'退出
  46. if (HighGui.waitKey(1) == 81) break;
  47. }
  48. //释放资源
  49. videoCapture.release();
  50. HighGui.destroyAllWindows();
  51. }
  52. //以下内容为对比人脸模块。与打开视频,识别人脸完全分离
  53. /**
  54. * 获取灰度人脸
  55. */
  56. public static Mat conv_Mat(String img) {
  57. //读取图片Mat
  58. Mat imgInfo = Imgcodecs.imread(img);
  59. //此处调用了实体方法,实现灰度转化
  60. CvtMatEntity cvtMatEntity = CvtMatEntity.cvtR2G(imgInfo);
  61. //创建Mat矩形
  62. MatOfRect faceMat = new MatOfRect();
  63. //识别人人脸
  64. faceDetector.detectMultiScale(cvtMatEntity.gray, faceMat);
  65. for (Rect rect : faceMat.toArray()) {
  66. //选出灰度人脸
  67. Mat face = new Mat(cvtMatEntity.gray, rect);
  68. return face;
  69. }
  70. return null;
  71. }
  72. /**
  73. * 图片对比人脸
  74. */
  75. public static double compare_image(String img_1, String img_2) {
  76. //获得灰度人脸
  77. Mat mat_1 = conv_Mat(img_1);
  78. Mat mat_2 = conv_Mat(img_2);
  79. Mat hist_1 = new Mat();
  80. Mat hist_2 = new Mat();
  81. //参数定义
  82. MatOfFloat ranges = new MatOfFloat(0f, 256f);
  83. MatOfInt histSize = new MatOfInt(10000000);
  84. //实现图片计算
  85. Imgproc.calcHist(Arrays.asList(mat_1), new MatOfInt(0), new Mat(), hist_1, histSize, ranges);
  86. Imgproc.calcHist(Arrays.asList(mat_2), new MatOfInt(0), new Mat(), hist_2, histSize, ranges);
  87. // 相关系数,获得相似度
  88. double res = Imgproc.compareHist(hist_1, hist_2, Imgproc.CV_COMP_CORREL);
  89. //返回相似度
  90. return res;
  91. }
  92. }

调用

  1. public class openapiMainApplication {
  2. public static void main(String[] args) throws UnsupportedEncodingException {
  3. //此为opencv的opencv_java453.dll
  4. //位置在opencv安装目录下的build\\java\\x64\\位置
  5. //可以将该dll文件放在系统 Path目录下
  6. String dllAbsPath = "D:\\opencv\\build\\java\\x64\\opencv_java453.dll";
  7. //位置在opencv安装目录下的sources\\data\\haarcascades\\位置
  8. String facexmlAbsPath = "D:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
  9. //必须加载
  10. InitInstance.init(dllAbsPath, facexmlAbsPath,eyexmlAbsPath);
  11. }
  12. }

附录