|
@@ -0,0 +1,130 @@
|
|
|
+package org.jeecg.modules.demo.opencv;
|
|
|
+
|
|
|
+import org.opencv.core.*;
|
|
|
+import org.opencv.highgui.HighGui;
|
|
|
+import org.opencv.imgcodecs.Imgcodecs;
|
|
|
+import org.opencv.imgproc.Imgproc;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 角点检测
|
|
|
+ */
|
|
|
+public class CornerCheck {
|
|
|
+ static {
|
|
|
+ System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+// harris();
|
|
|
+ detectConers();
|
|
|
+// getConers();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void getConers() {
|
|
|
+ Mat src = Imgcodecs.imread("D:\\data\\167.jpg");
|
|
|
+ List<Point> points = new ArrayList<>();
|
|
|
+ int minX = 10000;
|
|
|
+ int minY = 10000;
|
|
|
+ int maxX = 0;
|
|
|
+ int maxY = 0;
|
|
|
+ for (int j = 0; j < src.rows(); j++) {
|
|
|
+ for (int i = 0; i < src.cols(); i++) {
|
|
|
+ double[] details = src.get(j, i);
|
|
|
+ if (null != details && details.length > 0) {
|
|
|
+ if (details[0] == 0 && details[1] == 236 && details[2] == 2) {
|
|
|
+ System.out.print("{" + j + "," + i + "}:");
|
|
|
+ Point point = new Point();
|
|
|
+ point.x = i;
|
|
|
+ point.y = j;
|
|
|
+ if (minX >= i) {
|
|
|
+ minX = i;
|
|
|
+ }
|
|
|
+ if (maxX <= i) {
|
|
|
+ maxX = i;
|
|
|
+ }
|
|
|
+ if (maxY <= j) {
|
|
|
+ maxY = j;
|
|
|
+ }
|
|
|
+ points.add(point);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.println(minX);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void detectConers() {
|
|
|
+ try {
|
|
|
+ System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
|
|
+
|
|
|
+ final int maxCorners = 50, blockSize = 3;
|
|
|
+ final double qualityLevel = 0.01, minDistance = 20.0, k = 0.04;
|
|
|
+ final boolean useHarrisDetector = false;
|
|
|
+ MatOfPoint corners = new MatOfPoint();
|
|
|
+
|
|
|
+ Mat src = Imgcodecs.imread("D:\\data\\167.jpg");
|
|
|
+ if (src.empty()) {
|
|
|
+ throw new Exception("no file");
|
|
|
+ }
|
|
|
+ Mat dst = src.clone();
|
|
|
+ Mat gray = new Mat();
|
|
|
+ Imgproc.cvtColor(src, gray, Imgproc.COLOR_RGB2GRAY);
|
|
|
+ Imgproc.goodFeaturesToTrack(gray, corners, maxCorners, qualityLevel, minDistance,
|
|
|
+ new Mat(), blockSize, useHarrisDetector, k);
|
|
|
+ Point[] pCorners = corners.toArray();
|
|
|
+
|
|
|
+ for (int i = 0; i < pCorners.length; i++) {
|
|
|
+ double[] details = src.get((int) pCorners[i].y, (int) pCorners[i].x);
|
|
|
+ if (pCorners[i].x > 130 && pCorners[i].x < 570 && pCorners[i].y > 200 && pCorners[i].y < 850) {
|
|
|
+ System.out.println(pCorners[i].toString());
|
|
|
+ Imgproc.circle(dst, pCorners[i], 4, new Scalar(0, 0, 255), Imgproc.FILLED);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Imgcodecs.imwrite("D:\\data\\coners.jpg", dst);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ System.out.println("例外:" + e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * OpenCV-4.1.0 Harris 角点检测
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ * @Author: hyacinth
|
|
|
+ * @Title: harris
|
|
|
+ * @Description: TODO
|
|
|
+ * @date: 2019年8月26日 下午10:13:10
|
|
|
+ */
|
|
|
+ public static void harris() {
|
|
|
+ //载入图片
|
|
|
+ Mat src = Imgcodecs.imread("D:\\data\\167.jpg");
|
|
|
+ Mat gray = new Mat();
|
|
|
+ Mat dst = new Mat();
|
|
|
+ //灰度处理
|
|
|
+ Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
|
|
|
+ HighGui.imshow("灰度处理", gray);
|
|
|
+ Imgproc.cornerHarris(gray, dst, 2, 3, 0.06);
|
|
|
+ HighGui.imshow("角点检测", dst);
|
|
|
+ Core.normalize(dst, dst, 0, 255, Core.NORM_MINMAX, -1, new Mat());
|
|
|
+ HighGui.imshow("归一处理", dst);
|
|
|
+ Core.convertScaleAbs(dst, dst);
|
|
|
+
|
|
|
+ Mat result = src.clone();
|
|
|
+ for (int i = 0, row = result.rows(); i < row; i++) {
|
|
|
+ for (int j = 0, col = result.cols(); j < col; j++) {
|
|
|
+ if (dst.get(i, j).clone()[0] > 180) {
|
|
|
+ Imgproc.circle(result, new Point(j, i), 1, new Scalar(0, 0, 255), Imgproc.FILLED);
|
|
|
+ System.out.println("像素点位:" + j + "," + i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ HighGui.imshow("Harris 角点检测", result);
|
|
|
+ HighGui.waitKey(0);
|
|
|
+ }
|
|
|
+}
|