简体   繁体   English

模板匹配误报

[英]Template matching false positive

I am using template matching on openCV java to identify if a subimage exist in larger image. 我在openCV java上使用模板匹配来识别较大图像中是否存在子图像。 I want to get coordinates of match only if exact subimage is available in larger image. 我只想在大图像中有确切的子图像时才能获得匹配坐标。 I am using this code but getting a lot of false positive. 我正在使用此代码,但收到很多误报。 Attached is the subimage and larger image. 附带的是子图像和大图像。 Subimage is not present in larger image but i am getting match at (873,715) larger image subimage 子图像没有出现在较大的图像中,但是我正在以(873,715) 较大的图像 子图像进行匹配

    public void run(String inFile, String templateFile, String outFile,
    int match_method) {
    System.out.println("Running Template Matching");

    Mat img = Imgcodecs.imread(inFile);
    Mat templ = Imgcodecs.imread(templateFile);

    // / Create the result matrix
    int result_cols = img.cols() - templ.cols() + 1;
    int result_rows = img.rows() - templ.rows() + 1;
    Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);

    // / Do the Matching and Normalize
    Imgproc.matchTemplate(img, templ, result, match_method);
    // Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new
    // Mat());
    Imgproc.threshold(result, result, 0.1, 1.0, Imgproc.THRESH_TOZERO);
    // / Localizing the best match with minMaxLoc
    MinMaxLocResult mmr = Core.minMaxLoc(result);

    Point matchLoc;
    if (match_method == Imgproc.TM_SQDIFF
            || match_method == Imgproc.TM_SQDIFF_NORMED) {
        matchLoc = mmr.minLoc;
    } else {
        matchLoc = mmr.maxLoc;
    }
    double threashhold = 1.0;
    if (mmr.maxVal > threashhold) {
        System.out.println(matchLoc.x+" "+matchLoc.y);  
       Imgproc.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
                matchLoc.y + templ.rows()), new Scalar(0, 255, 0));
    }
    // Save the visualized detection.
    Imgcodecs.imwrite(outFile, img);
}

Here is an answer of the same question: Determine if an image exists within a larger image, and if so, find it, using Python 这是相同问题的答案: 确定图像是否存在于较大图像中,如果存在,则使用Python查找它

You will have to convert the python code to Java 您将不得不将python代码转换为Java

I am not familiar with OpenCV in Java but OpenCV C++. 我对Java中的OpenCV不熟悉,但对OpenCV C ++不熟悉。

I don't think following code is necessary. 我认为以下代码不是必需的。

Imgproc.threshold(result, result, 0.1, 1.0, Imgproc.THRESH_TOZERO);

The min/max values of 'Mat result' will be between -1 and 1 if you uses normalized option. 如果使用归一化选项,则“垫子结果”的最小/最大值将在-1和1之间。 Therefore, your following code will not work because your threshold is 1.0 if you use normalized option. 因此,以下代码将不起作用,因为如果您使用标准化选项,则阈值为1.0。

if (mmr.maxVal > threshold) 

Also, if you use CV_TM_SQDIFF, above code should be 另外,如果您使用CV_TM_SQDIFF,则上述代码应为

if (mmr.minVal < threshold)

with proper threshold. 有适当的门槛。

How about drawing minMaxLoc before comparing minVal/maxVal with threshold? 在将minVal / maxVal与阈值进行比较之前绘制minMaxLoc怎么样? to see it gives correct result? 看到它给出正确的结果? because match at (873,715) is ridiculous. 因为(873,715)的匹配太荒谬了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM