Reputation: 9348
Similar to the question here, Python, arranging sequences in 2 combined arrays, it’s interesting to have a solution, to find out how many different images and their sequences in a combination.
The target image is:
c:\four.jpg
By using “cv2” and “numpy”, what’s the way to have a result like:
['tooth fairy', 'santa and deer', 'santa', 'deer', 'tooth fairy', 'deer', 'santa and deer', 'santa', 'tooth fairy', 'santa']
The individuals are:
thanks.
Upvotes: 0
Views: 175
Reputation: 2323
Interesting example. Sure I'll condensate what we got in the chat.
First open your images in rgb space with imread
, then you can use matchtemplate
and numpy.where
to find the position, and from the y coordinate obtain the sequence, as
import cv2
import numpy as np
img_rgb = cv2.imread("four.jpg")
template = cv2.imread('fairy.jpg')
template1 = cv2.imread('sad.jpg')
template2 = cv2.imread('san.jpg')
template3 = cv2.imread('deer.jpg')
res = cv2.matchTemplate(img_rgb,template,cv2.TM_CCOEFF_NORMED)
res1 = cv2.matchTemplate(img_rgb,template1,cv2.TM_CCOEFF_NORMED)
res2 = cv2.matchTemplate(img_rgb,template2,cv2.TM_CCOEFF_NORMED)
res3 = cv2.matchTemplate(img_rgb,template3,cv2.TM_CCOEFF_NORMED)
threshold = 0.99
loc = np.where (res >= threshold)
loc1 = np.where (res1 >= threshold)
loc2 = np.where (res2 >= threshold)
loc3 = np.where (res3 >= threshold)
fairy = list(loc[0])
sandeer = list(loc1[0])
san = list(loc2[0])
deer = list(loc3[0])
x=sorted(fairy+sandeer+san+deer)
out=', '.join(['tooth fairy' if y in fairy else 'santa and deer' if y in sandeer else 'santa' if y in san else 'deer' for y in x])
print out
Upvotes: 1