Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use OpenCV MatchTemplate?

I'm attempting to find an image in another.

im = cv.LoadImage('1.png', cv.CV_LOAD_IMAGE_UNCHANGED)
    tmp = cv.LoadImage('e1.png', cv.CV_LOAD_IMAGE_UNCHANGED)
    w,h = cv.GetSize(im)
    W,H = cv.GetSize(tmp)
    width = w-W+1
    height = h-H+1
    result = cv.CreateImage((width, height), 32, 1)
    cv.MatchTemplate(im, tmp, result, cv.CV_TM_SQDIFF)
    print result

When I run this, everything executes just fine, no errors get thrown. But I'm unsure what to do from here. The doc says that result stores "A map of comparison results". I tried printing it, but it gives me width, height, and step.

How do I use this information to find whether or not one image is in another/where it is located?

like image 300
Zack Avatar asked Mar 14 '12 20:03

Zack


People also ask

How does CV matchTemplate work?

OpenCV comes with a function cv. matchTemplate() for this purpose. It simply slides the template image over the input image (as in 2D convolution) and compares the template and patch of input image under the template image. Several comparison methods are implemented in OpenCV.

Which are the matching methods available in OpenCV?

matchTemplate function with three parameters: The input image that contains the object we want to detect. The template of the object (i.e., what we want to detect in the image ) The template matching method.

What is mask in 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 to use OpenCV template matching in Python?

Template matching using OpenCV in Python. Template matching is a technique for finding areas of an image that are similar to a patch (template). A patch is a small image with certain features. The goal of template matching is to find the patch/template in an image. To find it, the user has to give two input images: Source Image (S) ...

How do I find a specific template in OpenCV?

Template Matching is a method for searching and finding the location of a template image in a larger image. OpenCV comes with a function cv.matchTemplate() for this purpose. It simply slides the template image over the input image (as in 2D convolution) and compares the template and patch of input image under the template image.

How to match image template using CV2 match template?

In the function cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED) the first parameter is the mainimage, second parameter is the template to be matched and third parameter is the method used for matching. img_rgb = cv2.imread('mainimage.jpg').

How do I compare two images in OpenCV?

OpenCV comes with a function cv.matchTemplate () for this purpose. It simply slides the template image over the input image (as in 2D convolution) and compares the template and patch of input image under the template image. Several comparison methods are implemented in OpenCV. (You can check docs for more details).


1 Answers

This might work for you! :)

def FindSubImage(im1, im2):
    needle = cv2.imread(im1)
    haystack = cv2.imread(im2)

    result = cv2.matchTemplate(needle,haystack,cv2.TM_CCOEFF_NORMED)
    y,x = np.unravel_index(result.argmax(), result.shape)
    return x,y

CCOEFF_NORMED is just one of many comparison methoeds. See: http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html for full list.

Not sure if this is the best method, but is fast, and works just fine for me! :)

like image 176
JHolta Avatar answered Sep 22 '22 08:09

JHolta