|
@@ -1,26 +1,119 @@
|
|
package org.jeecg.modules.demo.opencv;
|
|
package org.jeecg.modules.demo.opencv;
|
|
|
|
|
|
|
|
+import cn.com.ctop.common.module.utils.ResultMapUtils;
|
|
|
|
+import cn.com.ctop.common.module.utils.StatusCode;
|
|
import org.opencv.core.*;
|
|
import org.opencv.core.*;
|
|
import org.opencv.imgcodecs.Imgcodecs;
|
|
import org.opencv.imgcodecs.Imgcodecs;
|
|
import org.opencv.imgproc.Imgproc;
|
|
import org.opencv.imgproc.Imgproc;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.Collections;
|
|
|
|
-import java.util.Comparator;
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 角点检测
|
|
* 角点检测
|
|
*/
|
|
*/
|
|
|
|
+
|
|
public class CornerCheck {
|
|
public class CornerCheck {
|
|
static {
|
|
static {
|
|
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
|
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
|
}
|
|
}
|
|
|
|
|
|
- private static int EXPAND_SIZE = 0;
|
|
|
|
|
|
+ private static int EXPAND_SIZE = 3;
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
- detectConerList("D:\\data\\images\\233.jpg");
|
|
|
|
-// channelSplit();
|
|
|
|
|
|
+// harris();
|
|
|
|
+// detectConers();
|
|
|
|
+ channelSplit();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static Map<String, Object> detectConers() {
|
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
|
+ 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\\235.jpg");
|
|
|
|
+ if (src.empty()) {
|
|
|
|
+ throw new Exception("no file");
|
|
|
|
+ }
|
|
|
|
+ Mat dst = src.clone();
|
|
|
|
+ Mat gray = new Mat();
|
|
|
|
+ //灰度
|
|
|
|
+ Imgproc.cvtColor(src, gray, Imgproc.COLOR_RGB2GRAY);
|
|
|
|
+ Imgcodecs.imwrite("D:\\data\\gray.jpg", gray);
|
|
|
|
+ Mat binary = new Mat();
|
|
|
|
+ //二值化
|
|
|
|
+ Imgproc.threshold(gray, binary, 1, 255, Imgproc.THRESH_TOZERO);
|
|
|
|
+ Imgcodecs.imwrite("D:\\data\\binary.jpg", binary);
|
|
|
|
+ Imgproc.goodFeaturesToTrack(binary, corners, maxCorners, qualityLevel, minDistance,
|
|
|
|
+ new Mat(), blockSize, useHarrisDetector, k);
|
|
|
|
+ Point[] pCorners = corners.toArray();
|
|
|
|
+ List<Point> pointList = new ArrayList<>();
|
|
|
|
+ double maxX = 0d;
|
|
|
|
+ double maxY = 0d;
|
|
|
|
+ double minX = 10000d;
|
|
|
|
+ double minY = 100000d;
|
|
|
|
+ for (int i = 0; i < pCorners.length; i++) {
|
|
|
|
+ Imgproc.circle(dst, pCorners[i], 4, new Scalar(0, 0, 0), Imgproc.FILLED);
|
|
|
|
+ if (pCorners[i].x > 130 && pCorners[i].x < 570 && pCorners[i].y > 200 && pCorners[i].y < 850) {
|
|
|
|
+ if (pCorners[i].x >= maxX) {
|
|
|
|
+ maxX = pCorners[i].x;
|
|
|
|
+ }
|
|
|
|
+ if (pCorners[i].y >= maxY) {
|
|
|
|
+ maxY = pCorners[i].y;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (pCorners[i].x <= minX) {
|
|
|
|
+ minX = pCorners[i].x;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (pCorners[i].y <= minY) {
|
|
|
|
+ minY = pCorners[i].y;
|
|
|
|
+ }
|
|
|
|
+ pointList.add(pCorners[i]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ Imgcodecs.imwrite("D:\\data\\allConers.jpg", dst);
|
|
|
|
+ List<Point> cornerPoints = new ArrayList<>();
|
|
|
|
+ for (Point point : pointList) {
|
|
|
|
+ double getX = point.x;
|
|
|
|
+ double getY = point.y;
|
|
|
|
+ if ((getY >= (minY + 20) && getY <= (maxY - 20))) {
|
|
|
|
+ Imgproc.circle(dst, point, 4, new Scalar(255, 255, 255), Imgproc.FILLED);
|
|
|
|
+ } else {
|
|
|
|
+ cornerPoints.add(point);
|
|
|
|
+ Imgproc.circle(dst, point, 4, new Scalar(0, 0, 0), Imgproc.FILLED);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+// {139.0, 240.0} 左上 {144.0, 837.0} 左下
|
|
|
|
+// {482.0, 238.0} 右上 {532.0, 825.0} 右下
|
|
|
|
+ Collections.sort(cornerPoints, new Comparator<Point>() {
|
|
|
|
+ @Override
|
|
|
|
+ public int compare(Point p1, Point p2) {
|
|
|
|
+ return (int) (p1.x + p1.y - p2.x - p2.y);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ Imgcodecs.imwrite("D:\\data\\coners.jpg", dst);
|
|
|
|
+ for (Point p : cornerPoints) {
|
|
|
|
+ }
|
|
|
|
+ result.put("leftTop", cornerPoints.get(0));
|
|
|
|
+ result.put("rightBottom", cornerPoints.get(3));
|
|
|
|
+ if (cornerPoints.get(2).y > cornerPoints.get(1).y) {
|
|
|
|
+ result.put("rightTop", cornerPoints.get(1));
|
|
|
|
+ result.put("leftBottom", cornerPoints.get(2));
|
|
|
|
+ } else {
|
|
|
|
+ result.put("rightTop", cornerPoints.get(2));
|
|
|
|
+ result.put("leftBottom", cornerPoints.get(1));
|
|
|
|
+ }
|
|
|
|
+ ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
|
|
|
|
+ return result;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ ResultMapUtils.setResultMap(result, StatusCode.COMMON_SERVER_ERROR);
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
public static List<Point> matCornerList(List<Point> points) {
|
|
public static List<Point> matCornerList(List<Point> points) {
|