2

I have two images one is full image of 780 x 128 and other one is the cropped image from that 780 x 128(full image).

I want to find if the first image contains the second image, and if so, find out the coordinates of the top-left and right-bottom pixels inside the first image where the match is.

Is there a way to do that purely in Numpy, in a fast enough way, rather than using (4! very slow) pure Python loops?

I only bother about the coordinates other solutions for related questions are not suitable to give the coordinates of small image(img2) over the big image(img1).

Example:

img1 = numpy.array([
       [0, 1,  2,  3],
       [4, 5,  6,  7],
       [8, 9, 10, 11]
])

img2 = numpy.array([
          [2, 3],
          [6, 7]
])

How to do something like this?

coordinates = a.find(b)
coordinates of small image(img1) would then be [(0, 2), (1, 3)]
  • I got the solution of my own question. 'import cv2' 'import numpy as np' 'image = cv2.imread("img1.jpg")' 'template = cv2.imread("img2.jpg")' 'result = cv2.matchTemplate(image,template,cv2.TM_CCOEFF_NORMED)' 'coor0 = np.unravel_index(result.argmax(),result.shape)' 'print(coor0)' 'coor1 = (coor0[0]+template.shape[0], coor0[1]+template.shape[1])' 'print(coor1)' – Mayank Singh Soni Jul 08 '17 at 12:30

0 Answers0