SMIL 1.1.1
Loading...
Searching...
No Matches
example-blob-moments.py
1import smilPython as sp
2
3# How to print moments
4def printBlobMoments(m, title=""):
5 print(title.center(90))
6 hdr = ["m00", "m10", "m01", "m11", "m20", "m02"]
7 s = " " * 5
8 for h in hdr:
9 s += h.rjust(16)
10 print(s)
11 for label in m.keys():
12 s = '{:3} :'.format(label)
13 for v in m[label]:
14 s += ' {:14.2f}'.format(v)
15 print(s)
16
17# Serious work begins here
18#
19binaryImage = False
20if binaryImage:
21 imageName = "https://smil.cmm.minesparis.psl.eu/images/balls.png"
22 imo = sp.Image(imageName)
23 iml = sp.Image(imo, 'UINT16')
24 # normalize binary image values
25 imo /= 255
26 sp.label(imo, iml)
27else:
28 imageName = "https://smil.cmm.minesparis.psl.eu/images/tools.png"
29 imo = sp.Image(imageName)
30 imThr = sp.Image(imo)
31 sp.topHat(imo, imThr, sp.hSE(20))
32 sp.threshold(imThr, imThr)
33 iml = sp.Image(imo, "UINT16")
34 sp.label(imThr, iml)
35
36# create blobs
37blobs = sp.computeBlobs(iml)
38
39print("=== > Image : ", imageName, "\n")
40
41# Calculate not centered moments
42moments = sp.blobsMoments(imo, blobs, False)
43printBlobMoments(moments, "Not centered moments")
44
45print("=" * 102)
46# Calculate centered moments
47moments = sp.blobsMoments(imo, blobs, True)
48printBlobMoments(moments, "Centered moments")