question about template matching with single color image

asked 2014-04-16 04:34:49 -0600

cy937956803 gravatar image

updated 2014-04-16 04:37:42 -0600

berak gravatar image

public static boolean isSame(String src, String template, boolean partSameTrue){ boolean flag = partSameTrue; IplImage img, tpl, res; CvPoint minloc = new CvPoint(); CvPoint maxloc = new CvPoint();

    double minval[] = new double[5];
    double maxval[] = new double[5];

    int img_width, img_height;
    int tpl_width, tpl_height;
    int res_width, res_height;

    img = cvLoadImage(src, CV_LOAD_IMAGE_UNCHANGED);//CV_LOAD_IMAGE_COLOR
    tpl = cvLoadImage(template, CV_LOAD_IMAGE_UNCHANGED);//CV_LOAD_IMAGE_COLOR

    img_width = img.width();
    img_height = img.height();
    tpl_width = tpl.width();
    tpl_height = tpl.height();

    res_width = img_width - tpl_width + 1;
    res_height = img_height - tpl_height + 1;

    System.out.println("res_width: " + res_width + " res_height:" + res_height);

    res = cvCreateImage(cvSize(res_width, res_height), IPL_DEPTH_32F, 1);

    cvMatchTemplate(img, tpl, res, CV_TM_CCORR_NORMED );//CV_TM_CCORR_NORMED

// cvNormalize(res, res, 0, 1,NORM_MINMAX,null); cvMinMaxLoc(res, minval, maxval, minloc, maxloc, null);

    System.out.println("minloc.x(): " + minloc.x() + " minloc.y(): " + minloc.y());
    System.out.println("maxloc.x(): " +maxloc.x()+" maxloc.y(): " + maxloc.y());

    for (int i = 0; i < maxval.length; i++) {
        System.out.println(maxval[i]);
    }

    System.out.println();
    for (int i = 0; i < minval.length; i++) {
        System.out.println(minval[i]);
    }

    if(maxval[0] == 1.0) {
        flag = true;

    //image size the same and match probability is smaller than 0.99
    } else if (maxval[0] < 0.99 || (res_width==1 && res_height==1)){
        flag = false;
    } 

    /* free memory*/
    cvReleaseImage(img);
    cvReleaseImage(tpl);
    cvReleaseImage(res);

    return flag;
}

My purpose is to judge weather the two image are total same,or is part of the image correspond by the src, and I use template matching, but when I passing the parameters with two images which are total black with different size, I got the print result:

res_width: 122 res_height:82 minloc.x(): 0 minloc.y(): 0 maxloc.x(): 0 maxloc.y(): 0 maxval: 0.0

Is it means that the smaller black image is not part of the bigger black image? How can i solve the problem?

edit retag flag offensive close merge delete