当前位置:   article > 正文

使用openCV比对任意两张图片的相似度(亲测较准确)_java使用opencv的gpu模块进行图片相似度

java使用opencv的gpu模块进行图片相似度

方案:使用openCV中的直方图算法做对比。测试效果较好。

步骤(在java中使用openCV):

1.引入openCV的依赖

  1. <!-- https://mvnrepository.com/artifact/org.openimaj/core -->
  2. <dependency>
  3. <groupId>org.openpnp</groupId>
  4. <artifactId>opencv</artifactId>
  5. <version>4.5.5-1</version>
  6. </dependency>

2.代码

代码中提供了均方差算法(MSE)、结构相似性指数算法(SSIM)、峰值信噪比(PSNR)、直方图算法。其中直方图效果最好

  1. package com.angus.temp;
  2. import org.opencv.core.*;
  3. import org.opencv.imgcodecs.Imgcodecs;
  4. import org.opencv.imgproc.Imgproc;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. /**
  8. * @author angus
  9. * @version 1.0.0
  10. * @Description
  11. * @createTime 2023年06月01日 19:15:00
  12. */
  13. public class OpenCVImageSimilarity {
  14. public static void main(String[] args) {
  15. // 加载OpenCV库
  16. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  17. // 读取两张图像。准备比对的图片
  18. Mat image1 = Imgcodecs.imread("C:\\Users\\Pictures\\0009.jpg");
  19. Mat image2 = Imgcodecs.imread("C:\\Users\\Pictures\\0011.jpg");
  20. // 将图片处理成一样大
  21. Imgproc.resize(image1, image1, image2.size());
  22. Imgproc.resize(image2, image2, image1.size());
  23. // 计算均方差(MSE)
  24. double mse = calculateMSE(image1, image2);
  25. System.out.println("均方差(MSE): " + mse);
  26. // 计算结构相似性指数(SSIM)
  27. double ssim = calculateSSIM(image1, image2);
  28. System.out.println("结构相似性指数(SSIM): " + ssim);
  29. // 计算峰值信噪比(PSNR)
  30. double psnr = calculatePSNR(image1, image2);
  31. System.out.println("峰值信噪比(PSNR): " + psnr);
  32. // 计算直方图
  33. final double similarity = calculateHistogram(image1, image2);
  34. System.out.println("图片相似度(直方图): " + similarity);
  35. // 计算归一化交叉相关(NCC)
  36. // double ncc = calculateNCC(image1, image2);
  37. // System.out.println("归一化交叉相关(NCC): " + ncc);
  38. }
  39. // 计算均方差(MSE)
  40. private static double calculateHistogram(Mat image1, Mat image2) {
  41. // 计算直方图
  42. Mat hist1 = calculateHistogram(image1);
  43. Mat hist2 = calculateHistogram(image2);
  44. // 计算相似度
  45. final double similarity = Imgproc.compareHist(hist1, hist2, Imgproc.CV_COMP_CORREL);
  46. return similarity;
  47. }
  48. // 计算均方差(MSE)
  49. private static double calculateMSE(Mat image1, Mat image2) {
  50. Mat diff = new Mat();
  51. Core.absdiff(image1, image2, diff);
  52. Mat squaredDiff = new Mat();
  53. Core.multiply(diff, diff, squaredDiff);
  54. Scalar mseScalar = Core.mean(squaredDiff);
  55. return mseScalar.val[0];
  56. }
  57. // 计算结构相似性指数(SSIM)
  58. private static double calculateSSIM(Mat image1, Mat image2) {
  59. Mat image1Gray = new Mat();
  60. Mat image2Gray = new Mat();
  61. Imgproc.cvtColor(image1, image1Gray, Imgproc.COLOR_BGR2GRAY);
  62. Imgproc.cvtColor(image2, image2Gray, Imgproc.COLOR_BGR2GRAY);
  63. MatOfFloat ssimMat = new MatOfFloat();
  64. Imgproc.matchTemplate(image1Gray, image2Gray, ssimMat, Imgproc.CV_COMP_CORREL);
  65. Scalar ssimScalar = Core.mean(ssimMat);
  66. return ssimScalar.val[0];
  67. }
  68. // 计算峰值信噪比(PSNR)
  69. private static double calculatePSNR(Mat image1, Mat image2) {
  70. Mat diff = new Mat();
  71. Core.absdiff(image1, image2, diff);
  72. Mat squaredDiff = new Mat();
  73. Core.multiply(diff, diff, squaredDiff);
  74. Scalar mseScalar = Core.mean(squaredDiff);
  75. double mse = mseScalar.val[0];
  76. double psnr = 10.0 * Math.log10(255.0 * 255.0 / mse);
  77. return psnr;
  78. }
  79. // 计算归一化交叉相关(NCC)
  80. // private static double calculateNCC(Mat image1, Mat image2) {
  81. // Mat image1Gray = new Mat();
  82. // Mat image2Gray = new Mat();
  83. // Imgproc.cvtColor(image1, image1Gray, Imgproc.COLOR_BGR2GRAY);
  84. // Imgproc.cvtColor(image2, image2Gray, Imgproc.COLOR_BGR2GRAY);
  85. // MatOfInt histSize = new MatOfInt(256);
  86. // MatOfFloat ranges = new MatOfFloat(0, 256);
  87. // Mat hist1 = new Mat();
  88. // Mat hist2 = new Mat();
  89. //
  90. // Core.normalize(hist1, hist1, 0, 1, Core.NORM_MINMAX);
  91. // Core.normalize(hist2, hist2, 0, 1, Core.NORM_MINMAX);
  92. // double ncc = Core.compareHist(hist1, hist2, Imgproc.CV_COMP_CORREL);
  93. // return ncc;
  94. // }
  95. private static Mat calculateHistogram(Mat image) {
  96. Mat hist = new Mat();
  97. // 设置直方图参数
  98. MatOfInt histSize = new MatOfInt(256);
  99. MatOfFloat ranges = new MatOfFloat(0, 256);
  100. MatOfInt channels = new MatOfInt(0);
  101. List<Mat> images = new ArrayList<>();
  102. images.add(image);
  103. // 计算直方图
  104. Imgproc.calcHist(images, channels, new Mat(), hist, histSize, ranges);
  105. return hist;
  106. }
  107. }

3.会遇到一个问题

Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java455 in java.library.path

  1. Exception in thread "main" java.lang.UnsatisfiedLinkError: no opencv_java455 in java.library.path: [C:\Program Files\Java\jdk-14.0.2\bin, C:\Windows\Sun\Java\bin, C:\Windows\system32, C:\Windows, C:\Windows\system32, C:\Windows, C:\Windows\System32\Wbem, C:\Windows\System32\WindowsPowerShell\v1.0\, C:\Windows\System32\OpenSSH\, D:\angus\soft\Xshell 7\, D:\angus\soft\Xftp 7\, C:\Program Files\Git\cmd, C:\Program Files\Java\jdk-14.0.2\bin, C:\Users\angus\AppData\Local\Microsoft\WindowsApps, ., D:\angus\soft\Microsoft VS Code\bin, .]
  2. at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2680)
  3. at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:807)
  4. at java.base/java.lang.System.loadLibrary(System.java:1907)
  5. at com.angus.easyes.temp.OpenCVImageSimilarity.main(OpenCVImageSimilarity.java:17)

5.运行效果

6.相似性效果对比

https://blog.csdn.net/star1210644725/article/details/131005052?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22131005052%22%2C%22source%22%3A%22star1210644725%22%7Dicon-default.png?t=N4P3https://blog.csdn.net/star1210644725/article/details/131005052?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22131005052%22%2C%22source%22%3A%22star1210644725%22%7D

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/103781
推荐阅读
相关标签
  

闽ICP备14008679号