简体   繁体   English

Python,cv2,numpy在图像中查找图像

[英]Python, cv2, numpy to find images in an image

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. 与这里的问题类似, Python将序列排列在2个组合的数组中 ,找到一个解决方案很有趣,找出一个组合中有多少个不同的图像及其序列。

The target image is: 目标图像是:

c:\\four.jpg c:\\ four.jpg

在此处输入图片说明

By using “cv2” and “numpy”, what's the way to have a result like: 通过使用“ cv2”和“ numpy”,可以得到如下结果:

['tooth fairy', 'santa and deer', 'santa', 'deer', 'tooth fairy', 'deer', 'santa and deer', 'santa', 'tooth fairy', 'santa']

The individuals are: 个人是:

c:\\tooth fairy.jpg c:\\ tooth fairy.jpg 在此处输入图片说明

c:\\santa and deer.jpg c:\\ santa和deer.jpg 在此处输入图片说明

c:\\santa.jpg c:\\ santa.jpg 在此处输入图片说明

c:\\deer.jpg c:\\ deer.jpg 在此处输入图片说明

thanks. 谢谢。

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 首先使用imread在rgb空间中打开图像,然后可以使用matchtemplatenumpy.where查找位置,然后从y坐标获取序列,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM