Template Matching Opencv Code

  
Template Matching Opencv Code Average ratng: 3,7/5 57votes
Opencv Code Example

Prev Tutorial: Next Tutorial: Goal In this tutorial you will learn how to: • Use the OpenCV function to search for matches between an image patch and an input image • Use the OpenCV function to find the maximum and minimum values (as well as their positions) in a given array. Theory What is template matching? Template matching is a technique for finding areas of an image that match (are similar) to a template image (patch). While the patch must be a rectangle it may be that not all of the rectangle is relevant. In such a case, a mask can be used to isolate the portion of the patch that should be used to find the match. How does it work? • We need two primary components: • Source image (I): The image in which we expect to find a match to the template image • Template image (T): The patch image which will be compared to the template image our goal is to detect the highest matching area.

The image above is the result R of sliding the patch with a metric TM_CCORR_NORMED. The brightest locations indicate the highest matches. As you can see, the location marked by the red circle is probably the one with the highest value, so that location (the rectangle formed by that point as a corner and width and height equal to the patch image) is considered the match. • In practice, we locate the highest value (or lower, depending of the type of matching method) in the R matrix, using the function How does the mask work? • If masking is needed for the match, three components are required: • Source image (I): The image in which we expect to find a match to the template image • Template image (T): The patch image which will be compared to the template image • Mask image (M): The mask, a grayscale image that masks the template • Only two matching methods currently accept a mask: CV_TM_SQDIFF and CV_TM_CCORR_NORMED (see below for explanation of all the matching methods available in opencv). Burnermax Payload Tool. • The mask must have the same dimensions as the template • The mask should have a CV_8U or CV_32F depth and the same number of channels as the template image.