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?
1 Answer