Ask Your Question
2

Template Matching with Multiple Occurance

asked 2013-04-09 17:24:39 -0500

andern gravatar image

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);
edit retag flag offensive close merge delete

Comments

btw: there was a similar question (however maybe the answers here are better for your needs): http://answers.opencv.org/question/10388/template-matching-in-multiple-images/

Guanta gravatar imageGuanta ( 2013-04-10 05:59:56 -0500 )edit
bsdnoobz gravatar imagebsdnoobz ( 2013-04-11 16:05:32 -0500 )edit

Hi i use the same code but it gives me maxVal =minVal =0.0 !!

Marwen Z gravatar imageMarwen Z ( 2013-04-22 03:33:19 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2013-04-10 01:26:55 -0500

uvts_cvs gravatar image

updated 2013-04-10 03:48:00 -0500

Hello, I have just an hint, not a full answer: I am assuming mResult9u is the image where you look for peaks, and a peak means a found occurrence: then if you find a peak you will set to zero all the pixels in mResult9u near the peak; the number of pixels you set to zero depends on the overlap you allow between different occurences. Setting to zero the pixels has the effect of "removing" the first peak you found. If you set to zero a rectangular area as big as your pattern then you will not allow any overlap between patterns.

Then maybe you have to smooth this area where the first peak was, in order to avoid false peak detection in that area.

Then you perform a new minMaxLoc on mResult9u and you should find the next peak, and then again to find the next peaks.

edit flag offensive delete link more

Comments

1

thanks your hint helped i added a for loop and created another Mat, then in that Mat i would add in a filled Rectangle at the location of match and would just keep template matching that Mat until i got all the matches and it would draw the corresponding rect on the correct Mat then convert that to bitmap. Thanks

andern gravatar imageandern ( 2013-04-11 18:49:13 -0500 )edit

Hi, I'm new to OpenCV. I'm having the same problem. Could you please tell me how you create a filled Rectangle. Can you please share the code.

ssdehero gravatar imagessdehero ( 2013-06-09 13:46:16 -0500 )edit
0

answered 2013-04-10 02:41:59 -0500

NightLife gravatar image

The number of matches are defined or they are variable? If they are limit and you know exactly the number of matches , you can choose the specific variable for each of them and use minMaxLoc for all of them one by one.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-04-09 17:24:39 -0500

Seen: 6,093 times

Last updated: Apr 10 '13