fix: try to fix memory leak of images

This commit is contained in:
hyb1996 2018-03-29 21:34:24 +08:00
parent a7ed49d8b1
commit 61ba5dbed5
3 changed files with 23 additions and 22 deletions

View File

@ -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) {

View File

@ -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<Point, Double> 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<Point, Double> 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<Mat> buildPyramid(Mat mat, int maxLevel) {
List<Mat> 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;

View File

@ -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) {
}
}