当前位置:   article > 正文

基于Java语言的OpenCV使用_java opencv

java opencv

今天就来记录一下在Java中使用Opencv得配置吧,至于OpenCV的使用很简单,现成API的调用,查查文档就好了,但是关于OpenCV 这些API背后的原理就需要去学习一下数字图像处理的知识了。推荐冈萨雷斯的《数字图像处理》,这本书真的很棒棒呢

一、OpenCV官网下载opencv到本地,比如我下载到本地目录:F:\opencv3

二、新建一个Java项目,然后在其Project Structure中加入我们下载的OpenCV .jar文件。

三、从本地文件中加载动态库

  1. static {
  2. System.load("F:\\opencv3\\opencv\\build\\java\\x64\\opencv_java455.dll");
  3. }

四、使用

最后就是使用啦,我用它计算一个图形的重心,思路就是,带有目标物的图像通过背差法减掉背景图,然后通过腐蚀膨胀开运算去除小的噪声,然后Canny边缘检测之后转为二值图,最后加权计算重心位置。有个弊端就是开运算改变了像素点的值,导致重心些微偏移。所以还是采用滤波做噪声消除比较合适。

  1. import org.opencv.core.*;
  2. import org.opencv.highgui.HighGui;
  3. import org.opencv.imgcodecs.Imgcodecs;
  4. import org.opencv.imgproc.Imgproc;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. public class test {
  8. static {
  9. System.load("F:\\opencv3\\opencv\\build\\java\\x64\\opencv_java455.dll");
  10. }
  11. public static void main(String[] args) {
  12. String aimPicture1 = "C:\\Users\\Administrator\\Desktop\\test\\01.jpg";
  13. String backPicture = "C:\\Users\\Administrator\\Desktop\\test\\background.jpg";
  14. String destPicture = "C:\\Users\\Administrator\\Desktop\\test\\subtract.jpg";
  15. Mat aim1 = Imgcodecs.imread(aimPicture1);
  16. Point p1=dotest(aim1, backPicture, destPicture);
  17. System.out.println("x:"+p1.x);
  18. System.out.println("y:"+p1.y);
  19. }
  20. public static Point dotest(Mat aim, String backgroundPicture, String destPath) {
  21. Mat background = Imgcodecs.imread(backgroundPicture);
  22. Mat dest = new Mat(aim.size(), aim.type());
  23. //去除背景
  24. Core.subtract(aim, background, dest);
  25. Imgcodecs.imwrite(destPath, dest);
  26. //腐蚀再膨胀
  27. Mat openMat = open(dest);
  28. //拉普拉斯锐化再检测
  29. // Mat laplacianMat = new Mat(openMat.size(),CvType.CV_64F);
  30. // Imgproc.Laplacian(openMat,laplacianMat,3);
  31. // Imgcodecs.imwrite("C:\\Users\\Administrator\\Desktop\\test\\laplacian.jpg",laplacianMat);
  32. // 边缘检测
  33. Mat edgeMat = new Mat(openMat.size(), CvType.CV_64F);
  34. List<Mat> matList = new ArrayList<>();
  35. Core.split(openMat, matList);
  36. Imgcodecs.imwrite("C:\\Users\\Administrator\\Desktop\\test\\gray0.jpg", matList.get(0));
  37. Imgcodecs.imwrite("C:\\Users\\Administrator\\Desktop\\test\\gray1.jpg", matList.get(1));
  38. Imgcodecs.imwrite("C:\\Users\\Administrator\\Desktop\\test\\gray2.jpg", matList.get(2));
  39. Imgproc.Canny(matList.get(2), edgeMat, 15, 40);
  40. Imgcodecs.imwrite("C:\\Users\\Administrator\\Desktop\\test\\canny.jpg", edgeMat);
  41. //水平线检测,垂直线检测
  42. float[] hori = {-1, -1, -1, 2, 2, 2, -1, -1, -1};
  43. Mat horiMat = new Mat(3, 3, CvType.CV_32F);
  44. horiMat.put(0, 0, hori);
  45. Mat hLineMat = new Mat(openMat.size(), openMat.type());
  46. Imgproc.filter2D(edgeMat, hLineMat, 3, horiMat);
  47. Imgcodecs.imwrite("C:\\Users\\Administrator\\Desktop\\test\\hLineMat.jpg", hLineMat);
  48. float[] veri = {-1, 2, -1, -1, 2, -1, -1, 2, -1};
  49. Mat veriMat = new Mat(3, 3, CvType.CV_32F);
  50. veriMat.put(0, 0, veri);
  51. Mat vLineMat = new Mat(openMat.size(), openMat.type());
  52. Imgproc.filter2D(edgeMat, vLineMat, 3, veriMat);
  53. Imgcodecs.imwrite("C:\\Users\\Administrator\\Desktop\\test\\vLineMat.jpg", vLineMat);
  54. //范围裁剪
  55. int rows = edgeMat.rows();
  56. int cols = edgeMat.cols();
  57. int rows_10 = (int) (rows * 0.2);
  58. int cols_10 = (int) (cols * 0.2);
  59. Mat dstClipRange = edgeMat.rowRange((int) (rows * 0.2), rows - (int) (rows * 0.2))
  60. .colRange((int) (cols * 0.2), cols - (int) (cols * 0.2));
  61. edgeMat.release();
  62. //计算xline sumWeight
  63. int sumWeightx = 0;
  64. int sumX = 0;
  65. for (int i = 0; i < dstClipRange.cols(); i++) {
  66. int sum = 0;
  67. for (int j = 0; j < dstClipRange.rows(); j++) {
  68. sum += dstClipRange.get(j, i)[0];
  69. }
  70. sumWeightx += (i + 1) * sum;
  71. sumX += sum;
  72. }
  73. int sumWeighty = 0;
  74. int sumY = 0;
  75. for (int i = 0; i < dstClipRange.rows(); i++) {
  76. int sum = 0;
  77. for (int j = 0; j < dstClipRange.cols(); j++) {
  78. sum += dstClipRange.get(i, j)[0];
  79. }
  80. sumWeighty += (i + 1) * sum;
  81. sumY += sum;
  82. }
  83. double centerX = sumWeightx / sumX;
  84. double centerY = sumWeighty / sumY;
  85. centerX = centerX + cols_10;
  86. centerY = centerY + rows_10;
  87. Point p = new Point();
  88. p.x =centerX;
  89. p.y=centerY;
  90. System.out.println("x:" + centerX + "y:" + centerY);
  91. return p;
  92. }
  93. public static double getSumY(Mat singleChannelMat, Mat result) {
  94. Core.reduce(singleChannelMat, result, 1, Core.REDUCE_SUM);
  95. Core.MinMaxLocResult max = Core.minMaxLoc(result);
  96. return max.maxVal;
  97. }
  98. public static double getSumX(Mat singleChannelMat, Mat result) {
  99. Core.reduce(singleChannelMat, result, 0, Core.REDUCE_SUM);
  100. Core.MinMaxLocResult max = Core.minMaxLoc(result);
  101. return max.maxVal;
  102. }
  103. public static double getSingleValue(Mat singleChannelMat) {
  104. return getSingleValueFloat(singleChannelMat);
  105. }
  106. public static double getSingleValueFloat(Mat singleChannelMat) {
  107. double sum = 0;
  108. float[] temp = new float[singleChannelMat.rows() * singleChannelMat.cols() * singleChannelMat.channels()];
  109. singleChannelMat.get(0, 0, temp);
  110. for (double v : temp) {
  111. sum += v;
  112. }
  113. return sum;
  114. }
  115. public static Mat open(Mat srcImage) {
  116. Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, (new Size(10, 10)));
  117. Mat destImage = new Mat(srcImage.size(), CvType.CV_64F);
  118. Imgproc.erode(srcImage, destImage, element); //腐蚀
  119. Imgproc.dilate(destImage, destImage, element); //膨胀
  120. Imgcodecs.imwrite("C:\\Users\\Administrator\\Desktop\\test\\open.jpg", destImage);
  121. return destImage;
  122. }
  123. public static Mat open2(Mat srcImage) {
  124. Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, (new Size(5, 5)));
  125. Mat destImage = new Mat(srcImage.size(), CvType.CV_64F);
  126. Imgproc.erode(srcImage, destImage, element); //腐蚀
  127. Imgproc.dilate(destImage, destImage, element); //膨胀
  128. return destImage;
  129. }
  130. }

效果也还行吧,就整挺好的。API的调用很简单,但是要游刃有余还是需要学习一下数字图像处理的基本知识。

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

闽ICP备14008679号