abl*_*aze 6 numpy image scipy python-imaging-library python-2.7
目标
我需要检查一个图像是否是“另一个图像的子集”。我正在使用以下两个函数,它们似乎偶尔会起作用,但我正在寻找一些改进以使其可靠。我正在使用的功能是下面的 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 if image X is a subset of Y, using intersection
:param x:
:param y:
:return:
'''
xx = Image.open(self.getImagePathByName(x)).convert(mode='L', dither=Image.NONE)
yy = Image.open(self.getImagePathByName(y)).convert(mode='L', dither=Image.NONE)
pixelThreshold = 200
xx = np.array(xx)
xx = np.where(xx > pixelThreshold, 255, 0)
yy = np.array(yy)
yy = np.where(yy > pixelThreshold, 255, 0)
intersection = (xx + yy)
# if xx is a subset of yy then the intersection of xx and yy, is xx
xx = Image.open(self.getImagePathByName(x)).convert(mode='L', dither=Image.NONE)
intersection = Image.fromarray(intersection.astype(np.uint8)).convert(mode='L', dither=Image.NONE)
difference = self.getImageDifference(xx, intersection)
return difference
pass
def B(self, x, y):
'''
check if image X is a subset of Y, using union
:param x:
:param y:
:return:
'''
xx = Image.open(self.getImagePathByName(x)).convert(mode='L', dither=Image.NONE)
yy = Image.open(self.getImagePathByName(y)).convert(mode='L', dither=Image.NONE)
pixelThreshold = 200
xx = np.array(xx)
xx = np.where(xx > pixelThreshold, 255, 0)
yy = np.array(yy)
yy = np.where(yy > pixelThreshold, 255, 0)
union = (xx + yy) / 2
union = np.where(union > pixelThreshold, 255, 0)
# if xx is a subset of yy then the union of xx and yy, is yy
yy = Image.open(self.getImagePathByName(y)).convert(mode='L', dither=Image.NONE)
union = Image.fromarray(union.astype(np.uint8)).convert(mode='L', dither=Image.NONE)
difference = self.getImageDifference(yy, union)
return difference
pass
Run Code Online (Sandbox Code Playgroud)