如果匹配与否则返回

时间:2016-05-19 08:13:36

标签: opencv template-matching

在此匹配模板函数中。哪一个我需要采取的变量来比较和确定它是否匹配??

`Mat  image_ = Highgui.imread("charCrop"+a+"_"+i+".jpg",Highgui.CV_LOAD_IMAGE_COLOR);
Mat template = Highgui.imread(template_img,Highgui.CV_LOAD_IMAGE_COLOR);

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

Imgproc.matchTemplate(template, image_, result_, Imgproc.TM_CCOEFF);
Imgproc.threshold(result_, result_, 0.1, 1., Imgproc.THRESH_TOZERO);
Core.normalize(result_, result_, 0.9, 1, Core.NORM_MINMAX, -1);
Mat r = template.clone();  
while(true){
    Core.MinMaxLocResult res = Core.minMaxLoc(result_);
    Point loc = res.maxLoc;
    double x = res.maxLoc.x;
    double threshold = 0.6;
    System.out.println(loc);

    if(res.maxVal >= threshold){

        if(loc.equals(crop1)){
            System.out.println("Template match with input image");

            Core.rectangle(template, loc, new Point(loc.x + image_.width(), loc.y + image_.height()), new Scalar(0));
            Imgproc.floodFill(template, new Mat(), loc, new Scalar(255, 0, 0)); 
        }
        break;
    }else{
        System.out.println("Template does not match with input image");
        break;
    }

}       

请谢谢。

1 个答案:

答案 0 :(得分:0)

        int result_cols = img.cols() - template.cols() + 1;
        int result_rows = img.rows() - template.rows() + 1;
        Log.v(TAG, "matchImages: Initialize the result Mat.");
        Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);

        Mat img_gray = new Mat();
        Imgproc.cvtColor(img, img_gray, Imgproc.COLOR_RGB2GRAY);

        //Do the matching and NOT normalize
        Log.v(TAG, "matchImages: Do the match.");
        Imgproc.matchTemplate(img_gray, template, result, Imgproc.TM_CCOEFF_NORMED);

        //Localize the best match with minMaxLoc
        Log.v(TAG, "matchImages: Localize the best match.");
        Core.MinMaxLocResult mmr = Core.minMaxLoc(result);

        Log.v(TAG, "matchImages: Match percentage : " + mmr.maxVal);

        if (mmr.maxVal >= THRESHOLD) {
            Log.d(TAG, "matchImages: Match percentage is above the THRESHOLD.");
            return true;
        } else {
            Log.w(TAG, "matchImages: Match percentage is below the THRESHOLD!!!");
            return false;
        }