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

64 lines
1.7 KiB
Python

from dect import calc_shape_corners
import math
from scipy import ndimage
from skimage import filters
import numpy as np
import matplotlib.pyplot as plt
img = ndimage.imread("/home/tpolgrabia/Pobrane/CSFID/tracks_cropped/00003.jpg")
(n,m) = img.shape
otsu_lvl = filters.threshold_otsu(img)
m = img <= otsu_lvl
img_label, nb_labels = ndimage.label(m)
sizes = ndimage.sum(m, img_label, range(0, nb_labels+1))
min_seg_size = 100
to_be_removed_segs = sizes < min_seg_size
remove_mask = to_be_removed_segs[img_label]
img_label[remove_mask] = 0
labels = np.unique(img_label)
nb_labels = len(labels)
img_label = np.searchsorted(labels, img_label)
labels = np.unique(img_label)
centers = ndimage.center_of_mass(m, img_label, labels)
centers = np.array(centers, dtype=np.float)
(nc,who_cares) = centers.shape
def distance_point(m1, m2):
df = m1-m2
df2 = df*df
sd2 = np.sum(df2)
d = math.sqrt(sd2)
return d
# TODO check if generates all rates of distance pairs
for i in range(0, nc):
for j in range(0, i):
for k in range(0, j):
m1 = centers[i,:]
m2 = centers[j,:]
m3 = centers[k,:]
d1 = distance_point(m1, m2)
d2 = distance_point(m2, m3)
d = d1 / d2
print("{}-{}/{}-{}: {}".format(i, j, j, k, d))
corners = np.zeros(img.shape, dtype=np.bool)
for i in range(0, nb_labels):
mseg = img_label == i
# plt.imshow(mseg, cmap="Greys_r")
seg_corner = calc_shape_corners(mseg)
corners = corners + seg_corner
(n,m) = img.shape
mat_x = np.tile(np.arange(0, m),n).reshape((n,m))
mat_y = np.tile(np.arange(0, n),m).reshape((m,n)).T
xs = mat_x[corners]
ys = mat_y[corners]
plt.imshow(img, cmap="Greys_r")
plt.plot(xs, ys, "b+")
plt.show()