def GetColorHistogram(image, ignore_color, tolerance):
    if cv2 is not None:
        mask = None
        if ignore_color is not None:
            color = np.array([ignore_color.b, ignore_color.g, ignore_color.r])
            mask = ~cv2.inRange(image, np.subtract(color, tolerance),
                                np.add(color, tolerance))

        flatten = np.ndarray.flatten
        hist_b = flatten(cv2.calcHist([image], [0], mask, [256], [0, 256]))
        hist_g = flatten(cv2.calcHist([image], [1], mask, [256], [0, 256]))
        hist_r = flatten(cv2.calcHist([image], [2], mask, [256], [0, 256]))
    else:
        filtered = image.reshape(-1, 3)
        if ignore_color is not None:
            color = np.array([ignore_color.b, ignore_color.g, ignore_color.r])
            colorm = np.array(color) - tolerance
            colorp = np.array(color) + tolerance
            in_range = ((filtered[:, 0] < colorm[0]) |
                        (filtered[:, 0] > colorp[0]) |
                        (filtered[:, 1] < colorm[1]) |
                        (filtered[:, 1] > colorp[1]) |
                        (filtered[:, 2] < colorm[2]) |
                        (filtered[:, 2] > colorp[2]))
            filtered = np.compress(in_range, filtered, axis=0)
        if len(filtered[:, 0]) == 0:
            return color_histogram.ColorHistogram(np.zeros((256)),
                                                  np.zeros((256)),
                                                  np.zeros((256)),
                                                  ignore_color)
        hist_b = np.bincount(filtered[:, 0], minlength=256)
        hist_g = np.bincount(filtered[:, 1], minlength=256)
        hist_r = np.bincount(filtered[:, 2], minlength=256)

    return color_histogram.ColorHistogram(hist_r, hist_g, hist_b, ignore_color)
Esempio n. 2
0
 def Histogram(self, ignore_color, tolerance):
   ignore_color_int = -1 if ignore_color is None else int(ignore_color)
   response = self._RunCommand(_BitmapTools.HISTOGRAM,
                               ignore_color_int, tolerance)
   out = array.array('i')
   out.fromstring(response)
   assert len(out) == 768, (
       'The ColorHistogram has the wrong number of buckets: %s' % len(out))
   return color_histogram.ColorHistogram(out[:256], out[256:512], out[512:],
                                   ignore_color)
Esempio n. 3
0
 def __init__(self, r, g, b):
   self._histogram = color_histogram.ColorHistogram(r, g, b, rgba_color.WHITE)