繁体   English   中英

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

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

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

目标图像是:

c:\\ four.jpg

在此处输入图片说明

通过使用“ cv2”和“ numpy”,可以得到如下结果:

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

个人是:

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

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

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

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

谢谢。

有趣的例子。 当然,我会浓缩一下我们在聊天中得到的东西。

首先使用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