def testHistogramDistanceIgnoreColor(self):
        pixels = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
        bmp = image_util.FromRGBPixels(2, 2, pixels)

        hist1 = image_util.GetColorHistogram(bmp,
                                             ignore_color=rgba_color.RgbaColor(
                                                 1, 2, 3))
        hist2 = image_util.GetColorHistogram(bmp)

        self.assertEquals(hist1.Distance(hist2), 0)
def _ExtractCompletenessRecordFromVideo(video_path):
    """Extracts the completeness record from a video.

  The video must start with a filled rectangle of orange (RGB: 222, 100, 13), to
  give the view-port size/location from where to compute the completeness.

  Args:
    video_path: Path of the video to extract the completeness list from.

  Returns:
    list(CompletenessPoint)
  """
    video_file = tempfile.NamedTemporaryFile()
    shutil.copy(video_path, video_file.name)
    video_capture = video.Video(video_file)

    histograms = [(time,
                   image_util.GetColorHistogram(image,
                                                ignore_color=rgba_color.WHITE,
                                                tolerance=8))
                  for time, image in video_capture.GetVideoFrameIter()]

    start_histogram = histograms[1][1]
    final_histogram = histograms[-1][1]
    total_distance = start_histogram.Distance(final_histogram)

    def FrameProgress(histogram):
        if total_distance == 0:
            if histogram.Distance(final_histogram) == 0:
                return 1.0
            else:
                return 0.0
        return 1 - histogram.Distance(final_histogram) / total_distance

    return [(time, FrameProgress(hist)) for time, hist in histograms]
    def testHistogramIgnoreColor(self):
        pixels = [
            1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 8, 7, 6, 5, 4, 6, 1,
            2, 3, 1, 2, 3, 8, 7, 6, 5, 4, 6, 1, 2, 3
        ]
        bmp = image_util.FromRGBPixels(4, 3, pixels)

        hist = image_util.GetColorHistogram(bmp,
                                            ignore_color=rgba_color.RgbaColor(
                                                1, 2, 3))
        self.assertEquals(hist.r[1], 0)
        self.assertEquals(hist.r[5], 2)
        self.assertEquals(hist.r[8], 2)
        self.assertEquals(hist.g[2], 0)
        self.assertEquals(hist.g[4], 2)
        self.assertEquals(hist.g[7], 2)
        self.assertEquals(hist.b[3], 0)
        self.assertEquals(hist.b[6], 4)
    def testHistogramIgnoreColorTolerance(self):
        pixels = [1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6]
        bmp = image_util.FromRGBPixels(2, 2, pixels)

        hist = image_util.GetColorHistogram(bmp,
                                            ignore_color=rgba_color.RgbaColor(
                                                0, 1, 2),
                                            tolerance=1)
        self.assertEquals(hist.r[1], 0)
        self.assertEquals(hist.r[4], 1)
        self.assertEquals(hist.r[7], 1)
        self.assertEquals(hist.r[8], 1)
        self.assertEquals(hist.g[2], 0)
        self.assertEquals(hist.g[5], 1)
        self.assertEquals(hist.g[7], 1)
        self.assertEquals(hist.g[8], 1)
        self.assertEquals(hist.b[3], 0)
        self.assertEquals(hist.b[6], 2)
        self.assertEquals(hist.b[9], 1)
    def testHistogram(self):
        pixels = [
            1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 8, 7, 6, 5, 4, 6, 1,
            2, 3, 1, 2, 3, 8, 7, 6, 5, 4, 6, 1, 2, 3
        ]
        bmp = image_util.FromRGBPixels(4, 3, pixels)
        bmp = image_util.Crop(bmp, 1, 1, 2, 2)

        hist = image_util.GetColorHistogram(bmp)
        for i in xrange(3):
            self.assertEquals(sum(hist[i]),
                              image_util.Width(bmp) * image_util.Height(bmp))
        self.assertEquals(hist.r[1], 0)
        self.assertEquals(hist.r[5], 2)
        self.assertEquals(hist.r[8], 2)
        self.assertEquals(hist.g[2], 0)
        self.assertEquals(hist.g[4], 2)
        self.assertEquals(hist.g[7], 2)
        self.assertEquals(hist.b[3], 0)
        self.assertEquals(hist.b[6], 4)