Java opencv模板匹配和泛洪行为. [英] Java opencv template matching and floodfill behavior.

查看:226
本文介绍了Java opencv模板匹配和泛洪行为.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩opencv,我不明白Floodfill是如何工作的.我有一个较大的图像和一个模板"图像,试图找到所有匹配项(在这种情况下,我知道应该有两个匹配项).我的想法是找到第一个匹配项,使用Floodfill用w/e像素填充它,然后再次运行模板匹配,依此类推.这是我的代码

I've been playing around with opencv and I cant understand how floodfill works. I have one larger image and one "template" image, trying to find all matches (in this case I know that there should be two matches). My idea is to find the first match, use floodfill to fill it with w/e pixels and run template matching again and so on. Here is my code

import org.opencv.core.*;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.*;



public class Main {
    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat template = Imgcodecs.imread("imgs/template.jpg");
        Mat image = Imgcodecs.imread("imgs/imagetwo.jpg");


        int result_cols = image.cols() - template.cols() + 1;
        int result_rows = image.rows() - template.rows() + 1;
        Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);

        Imgproc.matchTemplate(image, template, result, Imgproc.TM_CCOEFF);
        Core.normalize(result, result, 0.9, 1, Core.NORM_MINMAX, -1);

        Core.MinMaxLocResult res = Core.minMaxLoc(result);
        Point loc = res.maxLoc;
        double x = res.maxLoc.x;
        System.out.println(loc);

        Imgproc.rectangle(image, loc, new Point(loc.x + template.width(), loc.y + template.height()), new Scalar(0));
        Imgproc.floodFill(image, new Mat(), loc, new Scalar(0, 255, 0));
        Imgproc.matchTemplate(image, template, result, Imgproc.TM_CCOEFF);
        Core.normalize(result, result, 0.8, 1, Core.NORM_MINMAX, -1);
        res = Core.minMaxLoc(result);
        if (res.maxVal >= 0.9) {
            loc = res.maxLoc;
        } else {
            System.out.println("No matches");
        }
        System.out.println(loc);

        Imgproc.rectangle(image, loc, new Point(loc.x + template.width(), loc.y + template.height()), new Scalar(0));
        Imgproc.floodFill(image, new Mat(), loc, new Scalar(0, 255, 0));
        Imgproc.matchTemplate(image, template, result, Imgproc.TM_CCOEFF);
        Core.normalize(result, result, 0.8, 1, Core.NORM_MINMAX, -1);
        res = Core.minMaxLoc(result);
        if (res.maxVal >= 0.9) {
            loc = res.maxLoc;
        } else {
            System.out.println("No matches");
        }
        System.out.println(loc);

    }
}

结果是:

{151.0, 167.0}
{142.0, 167.0}
{151.0, 167.0}

因此,基本上,第一次使用Floodfill可以正常工作,并且能够找到第二个匹配项,但是当我第三次运行循环"而不是获得没有匹配项"时,我又获得了第一个Point意味着我的第一个水淹没了?

So basically floodfill works fine the first time I use it and I am able to find the second match, but when I run the "loop" for the third time instead of getting "No matches" I get first Point again, which means my first floodfill is gone??

我会承认,即使阅读了所有可以在网上找到的关于洪水填埋的信息,我仍然不确定它应该如何工作,并且我觉得自己弄乱了它的定义,但是我不知道在哪里.

I'll admit that even after reading all I could find online about floodfill I'm still very uncertain on how it's supposed to work and I feel that I messed up somewhere in it's definition but I have no idea where.

感谢您的帮助, 谢谢.

Any help is appreciated, Thanks.

推荐答案

您需要初始化Mask,这是FloodFill方法的第二个参数.应该将其设置为零.因为该算法无法跨越非零像素.另外,您需要考虑矩阵的大小.它应该比图片宽2像素,高2像素.

You need to initialize the Mask, which is the second parameter of the floodFill method. It is supposed to be set up to zero. Because the algorithm can't go across the non zero pixels. Additionally you need to consider the size of the matrix. It should be 2 pixels wider and 2 pixels taller than the image.

这篇关于Java opencv模板匹配和泛洪行为.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆