code-examples/2015/2015_03/shoe-soles/measures.py

86 lines
1.7 KiB
Python
Raw Normal View History

#!/usr/bin/python
from scipy import ndimage
import sys
import math
import matplotlib.pyplot as plt
import numpy as np
print("Name: {}".format(__name__))
if __name__ != "__main__":
print("not runned")
sys.exit(0)
if len(sys.argv) <= 1:
print("Too small arguments")
sys.exit(1)
path = sys.argv[1]
img = ndimage.imread(path)
img_gray = img[:,:,0]
(n,m) = img_gray.shape
mask = img_gray >= 128
sx = 0.0
sy = 0.0
c = 0
for i in range(0, n):
for j in range(0, m):
if mask[i,j]:
sx += j
sy += i
c += 1
ax = sx / c
ay = sy / c
print("Position: {} x {}".format(ax,ay))
grav_measure = 0.0
for i in range(0, n):
for j in range(0, m):
if mask[i,j]:
grav_measure += (j - ax) * (j - ax) + (i - ay) * (i - ay)
grav_measure2 = math.sqrt(grav_measure / c)
print("Grav measure: {}".format(grav_measure))
# vector of offset
ox = 0.0
oy = 0.0
for i in range(0, n):
for j in range(0, m):
if mask[i,j]:
ox += math.pow(j - ax, 1.0)
oy += math.pow(i - ay, 1.0)
print("Vector of offset (measure of beaing simetrical): {}x{}".format(ox,oy))
img_filled = ndimage.binary_fill_holes(img_gray)
s = np.sum(img_filled)
unit = max(n,m)
c2 = c / float(unit)
s2 = s / (float(unit)*float(unit))
print("l: {}, s: {}".format(c2,s2))
# plt.imshow(img_filled, cmap="Greys_r")
# plt.show()
circularity1 = 2.0 * math.sqrt(s2 / np.pi)
print("Circularity1: {}".format(circularity1))
circularity2 = c2 / math.pi
print("Circularity2: {}".format(circularity2))
w_measure = c2 / (2.0 * math.sqrt(np.pi * s2)) - 1.0
print("W measure: {}".format(w_measure))
w9_measure = 2.0 * math.sqrt(np.pi * s2) / c2
print("W9 measure (malinkowskiej) {}".format(w9_measure))