private ImageFinderResult findImage(Mat sourceMat, Mat templateMat, double desiredAccuracy) {
if (sourceMat.width() < templateMat.width() || sourceMat.height() < templateMat.height()) {
throw new UnsupportedOperationException("The template image is larger than the source image. Ensure that the width and/or height of the image you are trying to find do not exceed the dimensions of the source image.");
}
Mat result = new Mat(sourceMat.rows() - templateMat.rows() + 1, sourceMat.rows() - templateMat.rows() + 1, CvType.CV_32FC1);
int intMatchingMethod;
switch (this.matchingMethod) {
case MM_CORELLATION_COEFF:
intMatchingMethod = Imgproc.TM_CCOEFF_NORMED;
break;
case MM_CROSS_CORELLATION:
intMatchingMethod = Imgproc.TM_CCORR_NORMED;
break;
default:
intMatchingMethod = Imgproc.TM_SQDIFF_NORMED;
}
Imgproc.matchTemplate(sourceMat, templateMat, result, intMatchingMethod);
MinMaxLocResult minMaxLocRes = Core.minMaxLoc(result);
double accuracy = 0;
Point location = null;
if (this.matchingMethod == MatchingMethod.MM_SQUARE_DIFFERENCE) {
accuracy = 1 - minMaxLocRes.minVal;
location = minMaxLocRes.minLoc;
} else {
accuracy = minMaxLocRes.maxVal;
location = minMaxLocRes.maxLoc;
}
if (accuracy < desiredAccuracy) {
throw new ImageNotFoundException(
String.format(
"Failed to find template image in the source image. The accuracy was %.2f and the desired accuracy was %.2f",
accuracy,
desiredAccuracy),
new Rectangle((int) location.x, (int) location.y, templateMat.width(), templateMat.height()),
accuracy);
}
if (!minMaxLocResultIsValid(minMaxLocRes)) {
throw new ImageNotFoundException(
"Image find result (MinMaxLocResult) was invalid. This usually happens when the source image is covered in one solid color.",
null,
null);
}
Rectangle foundRect = new Rectangle(
(int) location.x,
(int) location.y,
templateMat.width(),
templateMat.height());
return new ImageFinderResult(foundRect, accuracy);
}
ImageFinder.java 文件源码
java
阅读 27
收藏 0
点赞 0
评论 0
项目:opentest
作者:
评论列表
文章目录