test_compare.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/python
  2. from matplotlib.pyplot import imshow, subplot, show
  3. from LocalBinaryPatternizer import LocalBinaryPatternizer
  4. from GrayscaleImage import GrayscaleImage
  5. from cPickle import load
  6. from numpy import zeros, resize
  7. chars = load(file('characters', 'r'))[::2]
  8. left = None
  9. right = None
  10. s = {}
  11. for char in chars:
  12. if char.value not in s:
  13. s[char.value] = [char]
  14. else:
  15. s[char.value].append(char)
  16. left = s['F'][2].image
  17. right = s['A'][0].image
  18. size = 12
  19. d = (left.size[0] * 4, left.size[1] * 4)
  20. #GrayscaleImage.resize(left, d)
  21. #GrayscaleImage.resize(right, d)
  22. p1 = LocalBinaryPatternizer(left, size)
  23. h1 = p1.get_single_histogram()
  24. p1.create_features_vector()
  25. p1 = p1.features
  26. p2 = LocalBinaryPatternizer(right, size)
  27. h2 = p2.get_single_histogram()
  28. p2.create_features_vector()
  29. p2 = p2.features
  30. total_intersect = h1.intersect(h2)
  31. s = (len(p1), len(p1[0]))
  32. match = zeros(left.shape)
  33. m = 0
  34. for y in range(s[0]):
  35. for x in range(s[1]):
  36. h1 = p1[y][x]
  37. h2 = p2[y][x]
  38. intersect = h1.intersect(h2)
  39. print intersect
  40. for i in xrange(size):
  41. for j in xrange(size):
  42. try:
  43. match[y*size + i, x*size + j] = 1 - intersect
  44. except IndexError:
  45. pass
  46. m += intersect
  47. print 'Match: %d%%' % int(m / (s[0] * s[1]) * 100)
  48. print 'Single histogram instersection: %d%%' % int(total_intersect * 100)
  49. subplot(311)
  50. imshow(left.data, cmap='gray')
  51. subplot(312)
  52. imshow(match, cmap='gray')
  53. subplot(313)
  54. imshow(right.data, cmap='gray')
  55. show()