我有一张图片,比如说small.png
和一张更大的图片big.png
。在更大的图片中,小图片出现了2次,我想找到它的位置。
我尝试使用numpy和Image库,但是出现了错误'JpegImageFile' object has no attribute '__getitem__'
。我之前用的是png
格式,但也出现了相同的错误。
是否有其他模块或方法可以完成这个任务。我对任何方法都持开放态度。
目前会抛出错误的代码如下:
import numpy
from PIL import Image
here = Image.open(r"/Users/vks/Desktop/heren.jpg")
big = Image.open(r"/Users/vks/Desktop/Settingsn.jpg")
print here,big
herear = numpy.asarray(here)
bigar = numpy.asarray(big)
herearx = herear.shape[0]
hereary = herear.shape[1]
bigarx = bigar.shape[0]
bigary = bigar.shape[1]
print herearx , hereary , bigarx, bigary
stopx = bigarx - herearx + 1
stopy = bigary - hereary + 1
for x in range(0, stopx):
for y in range(0, stopy):
x2 = x+ herearx
y2 = y + hereary
pic = big[y:y2, x:x2] ===> error thrown here
test = pic = here
if test.all():
print x,y
else:
print "None"
我在我的Mac上安装不了另一个cv2
模块。当我执行pip install cv2
时,它失败并显示未找到包。
pic = bigar[y:y2, x:x2]; test = (pic == herear)
(注意,big
和here
都已经被更改为它们在numpy数组版本中的名称(这样你就可以使用索引和等式测试),并且下一行的第二个等号被替换为双等号表示等于测试而不是赋值)。 - jedwards