Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageStat.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # global image statistics
  6. #
  7. # History:
  8. # 1996-04-05 fl Created
  9. # 1997-05-21 fl Added mask; added rms, var, stddev attributes
  10. # 1997-08-05 fl Added median
  11. # 1998-07-05 hk Fixed integer overflow error
  12. #
  13. # Notes:
  14. # This class shows how to implement delayed evaluation of attributes.
  15. # To get a certain value, simply access the corresponding attribute.
  16. # The __getattr__ dispatcher takes care of the rest.
  17. #
  18. # Copyright (c) Secret Labs AB 1997.
  19. # Copyright (c) Fredrik Lundh 1996-97.
  20. #
  21. # See the README file for information on usage and redistribution.
  22. #
  23. import functools
  24. import math
  25. import operator
  26. class Stat:
  27. def __init__(self, image_or_list, mask=None):
  28. try:
  29. if mask:
  30. self.h = image_or_list.histogram(mask)
  31. else:
  32. self.h = image_or_list.histogram()
  33. except AttributeError:
  34. self.h = image_or_list # assume it to be a histogram list
  35. if not isinstance(self.h, list):
  36. msg = "first argument must be image or list"
  37. raise TypeError(msg)
  38. self.bands = list(range(len(self.h) // 256))
  39. def __getattr__(self, id):
  40. """Calculate missing attribute"""
  41. if id[:4] == "_get":
  42. raise AttributeError(id)
  43. # calculate missing attribute
  44. v = getattr(self, "_get" + id)()
  45. setattr(self, id, v)
  46. return v
  47. def _getextrema(self):
  48. """Get min/max values for each band in the image"""
  49. def minmax(histogram):
  50. n = 255
  51. x = 0
  52. for i in range(256):
  53. if histogram[i]:
  54. n = min(n, i)
  55. x = max(x, i)
  56. return n, x # returns (255, 0) if there's no data in the histogram
  57. v = []
  58. for i in range(0, len(self.h), 256):
  59. v.append(minmax(self.h[i:]))
  60. return v
  61. def _getcount(self):
  62. """Get total number of pixels in each layer"""
  63. v = []
  64. for i in range(0, len(self.h), 256):
  65. v.append(functools.reduce(operator.add, self.h[i : i + 256]))
  66. return v
  67. def _getsum(self):
  68. """Get sum of all pixels in each layer"""
  69. v = []
  70. for i in range(0, len(self.h), 256):
  71. layer_sum = 0.0
  72. for j in range(256):
  73. layer_sum += j * self.h[i + j]
  74. v.append(layer_sum)
  75. return v
  76. def _getsum2(self):
  77. """Get squared sum of all pixels in each layer"""
  78. v = []
  79. for i in range(0, len(self.h), 256):
  80. sum2 = 0.0
  81. for j in range(256):
  82. sum2 += (j**2) * float(self.h[i + j])
  83. v.append(sum2)
  84. return v
  85. def _getmean(self):
  86. """Get average pixel level for each layer"""
  87. v = []
  88. for i in self.bands:
  89. v.append(self.sum[i] / self.count[i])
  90. return v
  91. def _getmedian(self):
  92. """Get median pixel level for each layer"""
  93. v = []
  94. for i in self.bands:
  95. s = 0
  96. half = self.count[i] // 2
  97. b = i * 256
  98. for j in range(256):
  99. s = s + self.h[b + j]
  100. if s > half:
  101. break
  102. v.append(j)
  103. return v
  104. def _getrms(self):
  105. """Get RMS for each layer"""
  106. v = []
  107. for i in self.bands:
  108. v.append(math.sqrt(self.sum2[i] / self.count[i]))
  109. return v
  110. def _getvar(self):
  111. """Get variance for each layer"""
  112. v = []
  113. for i in self.bands:
  114. n = self.count[i]
  115. v.append((self.sum2[i] - (self.sum[i] ** 2.0) / n) / n)
  116. return v
  117. def _getstddev(self):
  118. """Get standard deviation for each layer"""
  119. v = []
  120. for i in self.bands:
  121. v.append(math.sqrt(self.var[i]))
  122. return v
  123. Global = Stat # compatibility