问:如何加快速度?
下面是我对Matlab的im2col '滑动'的实现,以及返回每个第n列的附加功能.该函数采用图像(或任意2个暗淡的数组)并从左到右,从上到下滑动,拾取给定大小的每个重叠子图像,并返回其列为子图像的数组.
import numpy as np
def im2col_sliding(image, block_size, skip=1):
rows, cols = image.shape
horz_blocks = cols - block_size[1] + 1
vert_blocks = rows - block_size[0] + 1
output_vectors = np.zeros((block_size[0] * block_size[1], horz_blocks * vert_blocks))
itr = 0
for v_b in xrange(vert_blocks):
for h_b in xrange(horz_blocks):
output_vectors[:, itr] = image[v_b: v_b + block_size[0], h_b: h_b + block_size[1]].ravel()
itr += 1
return output_vectors[:, ::skip]
Run Code Online (Sandbox Code Playgroud)
例:
a = np.arange(16).reshape(4, 4)
print a
print im2col_sliding(a, (2, 2)) # return every …Run Code Online (Sandbox Code Playgroud) 所以我有一个图像让我们说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): …Run Code Online (Sandbox Code Playgroud) 目标
我需要检查一个图像是否是“另一个图像的子集”。我正在使用以下两个函数,它们似乎偶尔会起作用,但我正在寻找一些改进以使其可靠。我正在使用的功能是下面的 A 和 B。他们的目标是检查图像 X 是否是 Y 的子集 - 或者换句话说检查 Y 图像是否包含图像 X。(调用者函数负责传递哪些图像)
注意:超集、子集、并集和交集的术语改编自集合论,如果它有助于理解问题。
警告:
图像可能不是“像素相同”,但肉眼看起来可能完全相同。因此,下面的函数 A 和 B 使用 返回一个数字getImageDifference,其中 0 是完美的绝对匹配,非零数字由被调用函数根据其他查询返回的内容进行相对处理。
只能使用 Pillow (PIL) 和/或 NumPy
职能
相关文章 :
https://wildclick.wordpress.com/2016/07/09/python-pil-pillow-numpy-intersect-images/和 https://wildclick.wordpress.com/2016/07/08/python-pil-pillow -numpy-add-images/
现有代码:
图像比较器
def getImageDifference(self, x, y):
# Calculate the difference b/w two images, works
return diff3
pass
Run Code Online (Sandbox Code Playgroud)
功能 A 和 B
def A(self, x, y):
'''
check …Run Code Online (Sandbox Code Playgroud) 我有一个大图像作为2D数组(让我们假设它是一个500 x 1000像素的灰度图像).我有一个小图像(比方说是15 x 15像素).我想将小图像滑过大图像,对于小图像的给定位置,我想计算小图像和大图像的下部分之间的相似性度量.
我想灵活地选择一种相似度.例如,我可能想要计算均方偏差或平均绝对偏差或其他东西(只是一些采用相同大小的两个矩阵并返回实数的操作).
结果应该是2D数组.我想有效地执行此操作(这意味着我想避免循环).
作为相似性的度量,我计划使用两个图像的颜色之间的平方偏差.但是,正如我所提到的,如果我可以更改度量(例如使用颜色之间的相关性),那将是很好的.
numpy中有功能吗?
numpy ×4
python ×3
image ×2
python-2.7 ×2
convolution ×1
performance ×1
scipy ×1
similarity ×1