diff --git a/autojs/src/main/java/com/stardust/autojs/core/image/ColorFinder.java b/autojs/src/main/java/com/stardust/autojs/core/image/ColorFinder.java index 054d0785..77aa07ec 100644 --- a/autojs/src/main/java/com/stardust/autojs/core/image/ColorFinder.java +++ b/autojs/src/main/java/com/stardust/autojs/core/image/ColorFinder.java @@ -73,16 +73,23 @@ public class ColorFinder { Scalar upperBound = new Scalar(Color.red(color) + threshold, Color.green(color) + threshold, Color.blue(color) + threshold, 255); if (rect != null) { - Core.inRange(new Mat(image.getMat(), rect), lowerBound, upperBound, bi); + Mat m = new Mat(image.getMat(), rect); + Core.inRange(m, lowerBound, upperBound, bi); + m.release(); } else { Core.inRange(image.getMat(), lowerBound, upperBound, bi); } Mat nonZeroPos = new Mat(); Core.findNonZero(bi, nonZeroPos); + MatOfPoint result; if (nonZeroPos.rows() == 0 || nonZeroPos.cols() == 0) { - return null; + result = null; + } else { + result = new MatOfPoint(nonZeroPos); } - return new MatOfPoint(nonZeroPos); + bi.release(); + nonZeroPos.release(); + return result; } public Point findMultiColors(ImageWrapper image, int firstColor, int threshold, Rect rect, int[] points) { diff --git a/autojs/src/main/java/com/stardust/autojs/core/image/TemplateMatching.java b/autojs/src/main/java/com/stardust/autojs/core/image/TemplateMatching.java index 9cdf56ae..053a2fd5 100644 --- a/autojs/src/main/java/com/stardust/autojs/core/image/TemplateMatching.java +++ b/autojs/src/main/java/com/stardust/autojs/core/image/TemplateMatching.java @@ -52,7 +52,7 @@ public class TemplateMatching { } //保存每一轮匹配到模板图片在原图片的位置 Point p = null; - Mat matchResult; + Mat matchResult = null; double similarity = 0; boolean isFirstMatching = true; for (int level = maxLevel; level >= 0; level--) { @@ -65,6 +65,8 @@ public class TemplateMatching { if (!isFirstMatching && !shouldContinueMatching(level, maxLevel)) { break; } + if (matchResult != null) + matchResult.release(); matchResult = matchTemplate(src, currentTemplate, matchMethod); Pair bestMatched = getBestMatched(matchResult, matchMethod, weakThreshold); p = bestMatched.first; @@ -72,7 +74,11 @@ public class TemplateMatching { } else { //根据上一轮的匹配点,计算本次匹配的区域 Rect r = getROI(p, src, currentTemplate); - matchResult = matchTemplate(new Mat(src, r), currentTemplate, matchMethod); + if (matchResult != null) + matchResult.release(); + Mat m = new Mat(src, r); + matchResult = matchTemplate(m, currentTemplate, matchMethod); + m.release(); Pair bestMatched = getBestMatched(matchResult, matchMethod, weakThreshold); //不满足弱阈值,返回null if (bestMatched.second < weakThreshold) { @@ -84,6 +90,8 @@ public class TemplateMatching { p.x += r.x; p.y += r.y; } + src.release(); + currentTemplate.release(); //满足强阈值,返回当前结果 if (similarity >= strictThreshold) { pyrUp(p, level); @@ -160,18 +168,6 @@ public class TemplateMatching { } - public static List buildPyramid(Mat mat, int maxLevel) { - List pyramid = new ArrayList<>(); - pyramid.add(mat); - for (int i = 0; i < maxLevel; i++) { - Mat m = new Mat((mat.rows() + 1) / 2, (mat.cols() + 1) / 2, mat.type()); - Imgproc.pyrDown(mat, m); - pyramid.add(m); - mat = m; - } - return pyramid; - } - public static Mat matchTemplate(Mat img, Mat temp, int match_method) { int result_cols = img.cols() - temp.cols() + 1; int result_rows = img.rows() - temp.rows() + 1; diff --git a/autojs/src/main/java/com/stardust/autojs/runtime/api/Images.java b/autojs/src/main/java/com/stardust/autojs/runtime/api/Images.java index 6dc918ca..d9687dec 100644 --- a/autojs/src/main/java/com/stardust/autojs/runtime/api/Images.java +++ b/autojs/src/main/java/com/stardust/autojs/runtime/api/Images.java @@ -261,13 +261,11 @@ public class Images { point.x = mScreenMetrics.scaleX((int) point.x); point.y = mScreenMetrics.scaleX((int) point.y); } - + if (src != image.getMat()) { + src.release(); + } return point; } - public void notityImageInserted(String path) { - - } - }