Histogram.py 914 B

12345678910111213141516171819202122232425262728293031323334
  1. class Histogram:
  2. def __init__(self, bins, min, max):
  3. self.bins = [0] * bins
  4. self.min = min
  5. self.max = max
  6. def add(self, number):
  7. self.bins[number] += 1
  8. def remove(self, number):
  9. self.bins[number] -= 1
  10. def get_bin_index(self, number):
  11. return (number - self.min) / ((self.max - self.min) / len(self.bins))
  12. def normalize(self):
  13. minimum = min(self.bins)
  14. self.bins = map(lambda b: b - minimum, self.bins)
  15. maximum = float(max(self.bins))
  16. self.bins = map(lambda b: b / maximum, self.bins)
  17. def intersect(self, other):
  18. h1 = self.bins
  19. h2 = other.bins
  20. match = 0
  21. # Add the minimum of each bin to the result
  22. for b in xrange(len(self.bins)):
  23. match += min(h1[b], h2[b])
  24. # Normalize by dividing by the number of pixels
  25. return float(match) / sum(h2)