We can use a technique called template matching to find a template of size in an input image. We use the function cv2.matchTemplate() which slides through the image and compares the overlapped patches of size
against the input image using a statistical comparison method.
The template image can be anywhere inside the input image but it cannot be rotated, this is a limitation of template matching. A full description of the function and formulas can be found here: http://docs.opencv.org/modules/imgproc/doc/object_detection.html?highlight=matchtemplate#matchtemplate
Output:
In this example I searched for the template image in the red box;
Image source: http://filmfabrikasi.com/wp-content/uploads/2012/08/x-men_3.jpg
Template matching in Python:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import numpy as np
import cv2
image = cv2.imread('wallpaper.jpg')
imageColor = cv2.imread('wallpaper.jpg')
template = cv2.imread('template.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
h,w = template.shape
result = cv2.matchTemplate(image,template, cv2.TM_CCOEFF)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(imageColor,top_left, bottom_right,(0,0,255),6)
cv2.imshow("Result", imageColor)
cv2.waitKey(0)
|
Object detection with template matching in C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#include
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
/// Global Variables
Mat img; Mat templ; Mat result;
const char* image_window = "Source Image";
const char* result_window = "Result window";
int match_method;
/// Function Headers
void MatchingMethod( int, void* );
/**
* @function main
*/
int main( int, char** argv )
{
/// Load image and template
img = imread( "wallpaper.jpg", 1 );
templ = imread( "logo.jpg", 1 );
// Match
MatchingMethod( 0, 0 );
waitKey(0);
return 0;
}
/**
* @function MatchingMethod
* @brief Trackbar callback
*/
void MatchingMethod( int, void* )
{
/// Source image to display
Mat img_display;
img.copyTo( img_display );
/// Create the result matrix
int result_cols = img.cols - templ.cols + 1;
int result_rows = img.rows - templ.rows + 1;
result.create( result_cols, result_rows, CV_32FC1 );
/// Do the Matching and Normalize
matchTemplate( img, templ, result, match_method );
normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );
/// Localizing the best match with minMaxLoc
double minVal; double maxVal; Point minLoc; Point maxLoc;
Point matchLoc;
minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );
/// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
if( match_method == TM_SQDIFF || match_method == TM_SQDIFF_NORMED )
{ matchLoc = minLoc; }
else
{ matchLoc = maxLoc; }
/// Show me what you got
rectangle( img, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar(0,0,255), 4, 8, 0 );
imshow( result_window, img );
return;
}
|