Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Template Matching with Multiple Occurance

I am trying to do Template matching that would detect 4 or so matches and put those values of the locations into an array or just to some variables. I am having problems with trying to detect more then one of the locations. I am using minMaxLoc and it works for one but i cant figure out how to get more then that one. Heres the code, if you see any other errors in my code please let me know i am still new to openCV. I want to keep this in Java while working with android so no Native code.

    bmp1=bmpp;// bmp1=input bmp2 = search image, bmp3= result found
    bmp2=gray;

    mFind=new Mat(256, 192, CvType.CV_8UC4); 
    Input = new Mat(256, 192, CvType.CV_8UC4);
    mResult8u = new Mat(256, 192, CvType.CV_8UC4);
    mResult9u = new Mat(256, 192, CvType.CV_8UC4);
    mResult = new Mat(256, 192, CvType.CV_8UC4);
    Utils.bitmapToMat(bmp2, mFind);
    Utils.bitmapToMat(bmp1, Input);

    Imgproc.matchTemplate(mFind, Input, mResult, Imgproc.TM_SQDIFF);

    Imgproc.threshold( mResult, mResult8u,0.08, 255,Imgproc.THRESH_BINARY );
    Core.normalize(mResult8u, mResult9u, 0, 255, Core.NORM_MINMAX, CvType.CV_8U);


    int result_cols =  mFind.cols() - Input.cols() + 1;
    int result_rows = mFind.rows() - Input.rows() + 1;


        MinMaxLocResult locRes = Core.minMaxLoc(mResult9u);  
        double minVal = locRes.minVal;  
        Point minLoc = locRes.minLoc;
        double maxVal = locRes.maxVal;  
        Point maxLoc = locRes.maxLoc;
        maxLoc.x+=25;
        maxLoc.y+=25;

        Point point = new Point();
        point.x=maxLoc.x+Input.cols();
        point.y=maxLoc.y+Input.rows();

        double thresholds=0.08;


        Core.rectangle(mFind, maxLoc,point, new Scalar(0, 255, 0, 255), 3);


    bmp3= Bitmap.createBitmap(mFind.cols(), mFind.rows(),Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(mFind, bmp3);     

     mImageView.setImageBitmap(bmp3);