PIL,NumPy:检查图像是否是其他图像的子集

时间:2016-07-23 19:21:07

标签: image python-2.7 numpy scipy python-imaging-library

目标

我需要检查一个图像是否是“另一个图像的子集”。我正在使用以下两个功能,这些功能似乎偶尔会起作用,但我正在寻找一些改进以使其可靠。我正在使用的功能是下面的A和B. 他们的目标是检查图像X是否是Y的子集 - 换句话说,检查Y图像是否包含图像X 。 (调用者函数负责处理哪些图像)

注意:超集,子集,联合和交集的术语是根据集合论改编的,如果它有助于理解这个问题。

警告:

  • 图像可能不是“像素相同”,但它们看起来可能完全相同 肉眼。因此,下面的函数A和B返回a 使用getImageDifference编号,其中0是完全绝对的 匹配,并且非零数字由被调用函数相对处理 基于其他查询返回的内容。

  • 只能使用Pillow(PIL)和/或NumPy

功能

  • 功能A:如果X是Y的子集,则X和Y的交点是X
  • 函数B:如果x是Y的子集,则X和Y的并集是Y

相关文章:

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

功能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

0 个答案:

没有答案