Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image in Image with cvMatchTemplate - but how?

I want to find out at which position of a source-image a certain sub-image appears (e.g. source image: http://i.pictr.com/6xg895m69q.png, sub-image: http://i.pictr.com/jdaz9zwzej.png). As far as I know it is necessary to transform the arrays to make them "readable" to OpenCV, this is what I tried, but for some reason, it does not work. here is my code so far:

 from PIL import Image
 import numpy
 from pylab import *
 import cv2
 import cv

 image = cv2.imread('source_img.jpg')
 template = cv2.imread('template_img.jpg')

 im = cv.fromarray(image)
 templ = cv.fromarray(template)
 result = numpy.zeros(shape=(1,10)) ##create a matrix with 0s
 a = cv.fromarray(result)
 cv.MatchTemplate(im, templ, a, cv.CV_TM_CCORR)
 print result
 print image

my goal is to write the coordinates of the sub-images in the result array (the rest of the array should keep the value 0 (I know that my code wont make this up to now). This the error message, I get when executing the code:

OpenCV Error: Assertion failed (result.size() == cv::Size(std::abs(img.cols - templ.cols) + 1, std::abs(img.rows - templ.rows) + 1) && result.type() == CV_32F) in cvMatchTemplate, file /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/OpenCV-2.4.3/modules/imgproc/src/templmatch.cpp, line 376 Traceback (most recent call last): File "/Users/strongbow/imagerecognition.py", line 27, in cv.MatchTemplate(im, templ, a, cv.CV_TM_CCORR) cv2.error: result.size() == cv::Size(std::abs(img.cols - templ.cols) + 1, std::abs(img.rows - templ.rows) + 1) && result.type() == CV_32F

I am new to OpenCV and really don't know what to do with this error-message. Anyone an idea/pointer what to do?

like image 251
julianschnell Avatar asked Jan 23 '13 18:01

julianschnell


People also ask

What does CV matchTemplate return?

It returns a grayscale image, where each pixel denotes how much does the neighbourhood of that pixel match with template.

How does cv2 matchTemplate work?

OpenCV comes with a function cv2. 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.

How do I improve my match template?

Improvements can be made to the matching method by using more than one template (eigenspaces), these other templates can have different scales and rotations. It is also possible to improve the accuracy of the matching method by hybridizing the feature-based and template-based approaches.

What is template matching in image processing?

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.


2 Answers

import sys
import cv2
import numpy

img = cv2.imread(sys.argv[1])
template = cv2.imread(sys.argv[2])
th, tw = template.shape[:2]

result = cv2.matchTemplate(img, template, cv2.TM_CCORR_NORMED)
threshold = 0.99
loc = numpy.where(result >= threshold)
for pt in zip(*loc[::-1]):
    cv2.rectangle(img, pt, (pt[0] + tw, pt[1] + th), 0, 2)

cv2.imwrite(sys.argv[3], img)

enter image description here

like image 68
mmgp Avatar answered Oct 14 '22 02:10

mmgp


import cv2
from cv2 import cv

image = cv2.imread('1_tree.jpg')
template = cv2.imread('1_tree_detail.jpg')

values = cv2.matchTemplate(image, template, method=cv.CV_TM_SQDIFF)
best_fit_point = cv2.minMaxLoc(values)[2]
bottom_right = best_fit_point[0]+template.shape[0], best_fit_point[1]+template.shape[1]
cv2.rectangle(image, best_fit_point, bottom_right, (255,255,255))
cv2.imshow('tree',image)
cv2.imwrite('tree_match.jpg', image)
cv2.waitKey()

enter image description hereenter image description hereenter image description here

like image 41
fraxel Avatar answered Oct 14 '22 02:10

fraxel