|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations |
|
|
|
import math |
|
|
|
|
|
class Stat: |
|
def __init__(self, image_or_list, mask=None): |
|
try: |
|
if mask: |
|
self.h = image_or_list.histogram(mask) |
|
else: |
|
self.h = image_or_list.histogram() |
|
except AttributeError: |
|
self.h = image_or_list |
|
if not isinstance(self.h, list): |
|
msg = "first argument must be image or list" |
|
raise TypeError(msg) |
|
self.bands = list(range(len(self.h) // 256)) |
|
|
|
def __getattr__(self, id): |
|
"""Calculate missing attribute""" |
|
if id[:4] == "_get": |
|
raise AttributeError(id) |
|
|
|
v = getattr(self, "_get" + id)() |
|
setattr(self, id, v) |
|
return v |
|
|
|
def _getextrema(self): |
|
"""Get min/max values for each band in the image""" |
|
|
|
def minmax(histogram): |
|
res_min, res_max = 255, 0 |
|
for i in range(256): |
|
if histogram[i]: |
|
res_min = i |
|
break |
|
for i in range(255, -1, -1): |
|
if histogram[i]: |
|
res_max = i |
|
break |
|
return res_min, res_max |
|
|
|
return [minmax(self.h[i:]) for i in range(0, len(self.h), 256)] |
|
|
|
def _getcount(self): |
|
"""Get total number of pixels in each layer""" |
|
return [sum(self.h[i : i + 256]) for i in range(0, len(self.h), 256)] |
|
|
|
def _getsum(self): |
|
"""Get sum of all pixels in each layer""" |
|
|
|
v = [] |
|
for i in range(0, len(self.h), 256): |
|
layer_sum = 0.0 |
|
for j in range(256): |
|
layer_sum += j * self.h[i + j] |
|
v.append(layer_sum) |
|
return v |
|
|
|
def _getsum2(self): |
|
"""Get squared sum of all pixels in each layer""" |
|
|
|
v = [] |
|
for i in range(0, len(self.h), 256): |
|
sum2 = 0.0 |
|
for j in range(256): |
|
sum2 += (j**2) * float(self.h[i + j]) |
|
v.append(sum2) |
|
return v |
|
|
|
def _getmean(self): |
|
"""Get average pixel level for each layer""" |
|
return [self.sum[i] / self.count[i] for i in self.bands] |
|
|
|
def _getmedian(self): |
|
"""Get median pixel level for each layer""" |
|
|
|
v = [] |
|
for i in self.bands: |
|
s = 0 |
|
half = self.count[i] // 2 |
|
b = i * 256 |
|
for j in range(256): |
|
s = s + self.h[b + j] |
|
if s > half: |
|
break |
|
v.append(j) |
|
return v |
|
|
|
def _getrms(self): |
|
"""Get RMS for each layer""" |
|
return [math.sqrt(self.sum2[i] / self.count[i]) for i in self.bands] |
|
|
|
def _getvar(self): |
|
"""Get variance for each layer""" |
|
return [ |
|
(self.sum2[i] - (self.sum[i] ** 2.0) / self.count[i]) / self.count[i] |
|
for i in self.bands |
|
] |
|
|
|
def _getstddev(self): |
|
"""Get standard deviation for each layer""" |
|
return [math.sqrt(self.var[i]) for i in self.bands] |
|
|
|
|
|
Global = Stat |
|
|