私はOpenCVが初めてです。特定の画像にテンプレートが与えられているかどうかをユーザーに伝えるアプリケーションを実行しています。このコードを使用しました。完璧にマッチングします。ただし、指定された画像に一致するテンプレートがないかどうかは検出されません。私が読んだとき、それは最高値になり、偽の結果が表示されます(画像の別の場所に表示されます)。minVal と maxVal について読みました。しかし、それでも私はそれが何をするのかさえよく理解できませんでした。コードをデバッグしたためです。minVal が 0.0 で maxVal が 255.0 であるたびに (画像にテンプレートが含まれているかどうかに関係なく、毎回という意味です)。私はここで立ち往生しています。正しい結果のみを表示するのを手伝ってください。
Utils.bitmapToMat(bmp2, mFind);
Utils.bitmapToMat(bmp1, Input);
Utils.bitmapToMat(bmp3, mResult9u);
Imgproc.matchTemplate(mFind, Input, mResult, matcher);
Core.normalize(mResult, mResult8u, 0, 255, Core.NORM_MINMAX, CvType.CV_8U);
Point matchLoc;
MinMaxLocResult minMaxLoc = Core.minMaxLoc(mResult8u);
/// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
if (matcher == Imgproc.TM_SQDIFF || matcher == Imgproc.TM_SQDIFF_NORMED)
{
matchLoc = minMaxLoc.minLoc;
}
else
{
matchLoc = minMaxLoc.maxLoc;
}
float thresholdMatchForMin = 0.02f;
float thresholdMatchForMax = 0.08f;
// minMaxLoc.minVal < thresholdMatchForMin ||
if (minMaxLoc.maxVal > thresholdMatchForMax)
{
/// Show me what you got
Core.rectangle(mResult9u, matchLoc,
new Point(matchLoc.x + Input.cols(), matchLoc.y + Input.rows()), new Scalar(0, 255, 255, 0));
Utils.matToBitmap(mFind, bmp2);
Utils.matToBitmap(mResult9u, bmp3);
Paint paint = new Paint();
paint.setColor(Color.RED);
Canvas canvas = new Canvas(bmp2);
canvas.drawRect(new Rect((int) matchLoc.x, (int) matchLoc.y, (int) (matchLoc.x + Input.cols()),
(int) (matchLoc.y + Input.rows())), paint);
}