Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9192953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:03:00+00:00 2026-06-17T21:03:00+00:00

I want to find out at which position of a source-image a certain sub-image

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:03:01+00:00Added an answer on June 17, 2026 at 9:03 pm
    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to find out the position of an element which is in iframe.
I want to find out the X and Y position of a point which
I want to find out which tables have been modified in the last hour
I want to be able to find out which DOM elements are present in
I have the code in which i want to find out the greatest number
I have a stack which contains some integer data. I want to find out
HI, I am using a bot program in which i want to find out
I want to find out which methods have been assigned to handle events of
I am working on one project. In which i want to find out Is
I want to find out if it is possible to create Android API's that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.