Find original template image

 

 

 

 

 

 

 

 

 

 

 

 code show as below:

 

#include <opencv2 / opencv.hpp> 
#include <the iostream> 

#include <math.h>
 the using  namespace STD;
 the using  namespace CV; 

Mat the src, TEMP, DST; 
// Parameters of Trackbar: 
int match_method = TM_SQDIFF;   // match_method pointer to the first matching 
int max_track = . 5 ;              // max_track five ways pointer        

const  char * INPUT_T = " INPUT Image " ;
 const  char * OUTPUT_T = " Result Image " ;
 const  charMatch_t = * " Template match-Demo " ;
 void Match_Demo ( int , void * );
 int main ( int argc, char ** the argv) {
     // image to be detected 
    the src = imread ( " L: /10.jpg " );
     @ template image 
    TEMP = imread ( " L: /11.jpg " );
     IF (src.empty () || temp.empty ()) { 
        the printf ( " Could Not Load image ... \ n- " );
         return - 1;
    }
    namedWindow(INPUT_T, CV_WINDOW_AUTOSIZE);
    namedWindow(OUTPUT_T, CV_WINDOW_NORMAL);
    namedWindow(match_t, CV_WINDOW_AUTOSIZE);
    imshow(INPUT_T, temp);
    const char* trackbar_title = "Match Algo Type:";
    createTrackbar(trackbar_title, OUTPUT_T, &match_method, max_track, Match_Demo); 
    // 注意两个指针的作用 &match_method, max_track
    Match_Demo(0, 0);

    waitKey(0);
    return 0;
}

void Match_Demo(int, Void * ) {
     // definition of a result matrix, must be a single channel 32-bit floating point,
     // assumed that the source image WxH, WXH template image, the result must be W - w + 1, H - + h 1 of size. 
    int width = src.cols - temp.cols + . 1 ;
     int height = src.rows - temp.rows + . 1 ; 
    Mat Result (width, height, CV_32FC1);     

    matchTemplate (the src, TEMP, Result, match_method, Mat ()) ; // match_method pointer refers to a five methods
     // matchTemplate plate magic matching function: a source image template 2. 3. the output image matching result 4. 5.Mat () 
    
    the normalize (result, result, 0 , 1 , NORM_MINMAX, - . 1 , Mat ());     // normalization function


    Minloc Point;                          // matching calculation result of the position of the minimum point 
    Point maxloc;                          // matching calculation result of the maximum point position 
    Double min, max;                       // Double type 
    src.copyTo (DST);                          
    Point temLoc;                          // definitions found position coordinate point 
    minMaxLoc (result, & min, max &, & minloc, & maxloc, Mat ());   // find the result matrix minimum value and a maximum point 

    IF (match_method == == TM_SQDIFF || match_method TM_SQDIFF_NORMED) { 
        temLoc = minloc; 
    } 
    the else { 
        temLoc = maxloc; 
    } 

    //Draw a rectangle 
    Rectangle (DST, Rect (temLoc.x, temLoc.y, temp.cols, temp.rows), the Scalar ( 0 , 0 , 255 ), 2 , . 8 );
     // Rectangle function parameters: a target image 2 . Rect rectangle coordinates of the starting point and length and width of the rectangle color line 3. 4. 5. the default thickness 8 antialiasing 
    rectangle (Result, Rect (temLoc.x, temLoc.y, temp.cols, temp.rows), the Scalar ( 0 , 0 , 255 ), 2 , . 8 ); 

    imshow (OUTPUT_T, Result); 
    imshow (match_t, DST); 
}

 

 Original:                                                             

 

 

             

template:

 

 

 Five kinds of matches :( Note: result here is owned by the calculation of the image after a finding drawn and labeled the rectangular coordinate point template matching)

 

 

 

 

 

 

 

 

 

 

 

 

 

 As can be seen from the results above, in addition to the third method can not be found in the image other than the position of the template matching,

The other four methods can pinpoint the exact location of the subject vehicle.

 

Guess you like

Origin www.cnblogs.com/Jack-Elvis/p/11530283.html