SMIL 1.1.2
Loading...
Searching...
No Matches
example-inertia-matrix.py
1import smilPython as sp
2import numpy as np
3from numpy import linalg as la
4
5# get image
6imIn = sp.Image("https://smil.cmm.minesparis.psl.eu/images/tools.png")
7
8# labelize input image
9imThr = sp.Image(imIn)
10sp.topHat(imIn, imThr, sp.hSE(20))
11sp.threshold(imThr, imThr)
12imLbl = sp.Image(imIn, "UINT16")
13sp.label(imThr, imLbl)
14
15# compute blobs and get their data
16blobs = sp.computeBlobs(imLbl)
17central = True
18inertia = sp.blobsInertiaMatrix(imIn, blobs, central)
19barys = sp.blobsBarycenter(imIn, blobs)
20
21nshape = (2, 2)
22if imIn.getDimension() == 3:
23 nshape = (3, 3)
24
25for k in inertia.keys():
26 print("=" * 64)
27 s = ""
28 for v in barys[k]:
29 s += " {:6.1f}".format(v)
30 print("Blob label : {:3d} - Barycenter :{:}".format(k, s))
31 # Smil returns inertia matrix as a vector. Reshape it.
32 xi = np.array(inertia[k])
33 xi = xi.reshape(nshape)
34 np.set_printoptions(precision=3)
35 print(" ====== Inertia Matrix")
36 print(xi)
37 # let numpy evaluate eingenvalues and eingenvectors
38 w, v = la.eig(xi)
39 print(" ====== Eingenvalues")
40 print(w)
41 print(" ====== Eingenvectors")
42 print(v)
43