Merge pull request #72 from LYS86/master

PR#72 - LYS86 - findMultiColors调整
This commit is contained in:
SuperMonster003
2023-06-03 15:27:20 +08:00
committed by GitHub
2 changed files with 46 additions and 17 deletions

View File

@@ -160,7 +160,7 @@ module.exports = function (scriptRuntime, scope) {
throw TypeError(`Argument size for images.medianBlur must be either a number or an array with SAME two number elements`); throw TypeError(`Argument size for images.medianBlur must be either a number or an array with SAME two number elements`);
} }
} }
let ksize = Array.isArray(size) ? [ size ][0] : size; let ksize = Array.isArray(size) ? [size][0] : size;
let mat = new Mat(); let mat = new Mat();
Imgproc.medianBlur(img.mat, mat, ksize); Imgproc.medianBlur(img.mat, mat, ksize);
img.shoot(); img.shoot();
@@ -240,7 +240,7 @@ module.exports = function (scriptRuntime, scope) {
this.results = []; this.results = [];
for (let i = 0; i < cir.rows(); i++) { for (let i = 0; i < cir.rows(); i++) {
for (let j = 0; j < cir.cols(); j++) { for (let j = 0; j < cir.cols(); j++) {
let [ x, y, radius ] = cir.get(i, j); let [x, y, radius] = cir.get(i, j);
this.results.push({ x, y, radius }); this.results.push({ x, y, radius });
} }
} }
@@ -304,13 +304,13 @@ module.exports = function (scriptRuntime, scope) {
}, },
findColorInRegion(img, color, x, y, width, height, threshold) { findColorInRegion(img, color, x, y, width, height, threshold) {
return this.findColor(img, color, { return this.findColor(img, color, {
region: [ x, y, width, height ], region: [x, y, width, height],
threshold: threshold, threshold: threshold,
}); });
}, },
findColorEquals(img, color, x, y, width, height) { findColorEquals(img, color, x, y, width, height) {
return this.findColor(img, color, { return this.findColor(img, color, {
region: [ x, y, width, height ], region: [x, y, width, height],
threshold: 0, threshold: 0,
}); });
}, },
@@ -323,21 +323,31 @@ module.exports = function (scriptRuntime, scope) {
'region' in opt ? _.buildRegion(img, opt.region) : null); 'region' in opt ? _.buildRegion(img, opt.region) : null);
return _.toPointArray(o); return _.toPointArray(o);
}, },
findMultiColors(img, firstColor, paths, options) { findMultiColors(img, firstColor, paths, options) {
_.initIfNeeded(); _.initIfNeeded();
let list = java.lang.reflect.Array.newInstance(java.lang.Integer.TYPE, paths.length * 3); let list = java.lang.reflect.Array.newInstance(java.lang.Integer.TYPE, paths.length * 3);
for (let i = 0; i < paths.length; i += 1) { for (let i = 0; i < paths.length; i += 1) {
let [ x, y, color ] = paths[i]; let [x, y, color] = paths[i];
list[i * 3] = x; list[i * 3] = x;
list[i * 3 + 1] = y; list[i * 3 + 1] = y;
list[i * 3 + 2] = colors.toInt(color); list[i * 3 + 2] = colors.toInt(color);
} }
let opt = options || {}; let opt = options || {};
return rtImages.colorFinder.findMultiColors(img, let findAll = opt.All || false;
colors.toInt(firstColor),
_.parseThreshold(opt), if (findAll) {
'region' in opt ? _.buildRegion(img, opt.region) : null, return Array.from(rtImages.colorFinder.findAllMultiColors(img,
list); colors.toInt(firstColor),
_.parseThreshold(opt),
'region' in opt ? _.buildRegion(img, opt.region) : null,
list));
} else {
return rtImages.colorFinder.findMultiColors(img,
colors.toInt(firstColor),
_.parseThreshold(opt),
'region' in opt ? _.buildRegion(img, opt.region) : null,
list);
}
}, },
findImage(img, template, options) { findImage(img, template, options) {
_.initIfNeeded(); _.initIfNeeded();
@@ -350,7 +360,7 @@ module.exports = function (scriptRuntime, scope) {
}, },
findImageInRegion(img, template, x, y, width, height, threshold) { findImageInRegion(img, template, x, y, width, height, threshold) {
return this.findImage(img, template, { return this.findImage(img, template, {
region: [ x, y, width, height ], region: [x, y, width, height],
threshold: threshold, threshold: threshold,
}); });
}, },
@@ -663,7 +673,7 @@ module.exports = function (scriptRuntime, scope) {
if (size.length === 1) { if (size.length === 1) {
width = height = size[0]; width = height = size[0];
} else { } else {
[ width, height ] = size; [width, height] = size;
} }
} }
return new Size(width, height); return new Size(width, height);
@@ -683,7 +693,7 @@ module.exports = function (scriptRuntime, scope) {
* @returns {org.opencv.core.Point} * @returns {org.opencv.core.Point}
*/ */
parsePoint(point) { parsePoint(point) {
let [ x, y ] = point; let [x, y] = point;
return new Point(x, y); return new Point(x, y);
}, },
parseQuality(q) { parseQuality(q) {
@@ -771,10 +781,10 @@ module.exports = function (scriptRuntime, scope) {
* @returns {org.opencv.core.Rect} * @returns {org.opencv.core.Rect}
*/ */
buildRegion(img, region) { buildRegion(img, region) {
let [ x, y, w, h ] = region instanceof org.opencv.core.Rect let [x, y, w, h] = region instanceof org.opencv.core.Rect
? [ region.x, region.y, region.width, region.height ] ? [region.x, region.y, region.width, region.height]
: region instanceof android.graphics.Rect : region instanceof android.graphics.Rect
? [ region.left, region.top, region.width(), region.height() ] ? [region.left, region.top, region.width(), region.height()]
: Array.isArray(region) ? region : []; : Array.isArray(region) ? region : [];
x = _.parseNumber(x, 0); x = _.parseNumber(x, 0);

View File

@@ -13,6 +13,10 @@ import org.opencv.core.Point;
import org.opencv.core.Rect; import org.opencv.core.Rect;
import org.opencv.core.Scalar; import org.opencv.core.Scalar;
import java.util.List;
import java.util.ArrayList;
/** /**
* Created by Stardust on 2017/5/18. * Created by Stardust on 2017/5/18.
*/ */
@@ -108,6 +112,21 @@ public class ColorFinder {
image.shoot(); image.shoot();
return result; return result;
} }
public Point[] findAllMultiColors(ImageWrapper image, int firstColor, int threshold, Rect rect, int[] points) {
Point[] firstPoints = findAllPointsForColor(image, firstColor, threshold, rect);
List<Point> resultPoints = new ArrayList<>();
for (Point firstPoint : firstPoints) {
if (firstPoint != null) {
if (checksPath(image, firstPoint, threshold, points)) {
resultPoints.add(firstPoint);
}
}
}
image.shoot();
return resultPoints.toArray(new Point[0]);
}
private boolean checksPath(ImageWrapper image, Point startingPoint, int threshold, int[] points) { private boolean checksPath(ImageWrapper image, Point startingPoint, int threshold, int[] points) {
for (int i = 0; i < points.length; i += 3) { for (int i = 0; i < points.length; i += 3) {