SMIL  1.0.4
inertia_moments.py
1 from smilPython import *
2 from math import *
3 
4 # Load an image
5 imIn = Image("https://smil.cmm.minesparis.psl.eu/images/tools.png")
6 imIn.show()
7 
8 imThr = Image(imIn)
9 topHat(imIn, imThr, hSE(20))
10 threshold(imThr, imThr)
11 
12 imLbl = Image(imIn, "UINT16")
13 label(imThr, imLbl)
14 imLbl.showLabel()
15 
16 def fitRectangle(mat):
17  m00, m10, m01, m11, m20, m02 = mat
18 
19  if m00 == 0:
20  return 0, 0, 0, 0, 0
21 
22  # COM
23  xc = int (m10 / m00)
24  yc = int (m01 / m00)
25 
26  # centered matrix (central moments)
27  u00 = m00
28  u20 = m20 - m10**2 / m00
29  u02 = m02 - m01**2 / m00
30  u11 = m11 - m10 * m01 / m00
31 
32  # eigen values
33  delta = 4 * u11**2 + (u20 - u02)**2
34  I1 = (u20 + u02 + sqrt(delta)) / 2
35  I2 = (u20 + u02 - sqrt(delta)) / 2
36 
37  theta = 0.5 * atan2(-2 * u11, (u20 - u02))
38 
39  # Equivalent rectangle
40  # I1 = a**2 * S / 12, I2 = b**2 * S / 12
41  a = int (sqrt(12 * I1 / u00))
42  b = int (sqrt(12 * I2 / u00))
43 
44  return xc, yc, a, b, theta
45 
46 # Compute Blobs
47 blobs = computeBlobs(imLbl)
48 
49 # Compute Inertia Matrices
50 
51 mats = blobsMoments(imIn, blobs)
52 bboxes = blobsBoundBox(imLbl)
53 imDraw = Image(imIn)
54 
55 print("Label\tA\tB\tTheta")
56 for b in blobs.keys():
57  mat = xc, yc, A, B, theta = fitRectangle(mats[b])
58  print(str(b) + "\t" + str(A) + "\t" + str(B) + "\t" + str(theta))
59  dx = A / 2 * cos(pi - theta)
60  dy = A / 2 * sin(pi - theta)
61  drawLine(imDraw, int(xc - dx), int(yc - dy), int(xc + dx), int(yc + dy), b)
62  dx = B / 2 * sin(theta)
63  dy = B / 2 * cos(theta)
64  drawLine(imDraw, int(xc - dx), int(yc - dy), int(xc + dx), int(yc + dy), b)
65 imIn.getViewer().drawOverlay(imDraw)
RES_T sqrt(const Image< T1 > &imIn, Image< T2 > &imOut)
sqrt() - square root of an image
Definition: DImageArith.hpp:926
map< T, Blob > computeBlobs(const Image< T > &imIn, bool onlyNonZero=true)
Create a map of blobs from a labeled image.
Definition: DBlob.hpp:98
map< labelT, Vector_double > blobsMoments(const Image< T > &imIn, map< labelT, Blob > &blobs, bool central=false)
blobsMoments() - Measure blobs image moments (faster with blobs pre-generated).
Definition: DBlobMeasures.hpp:280
map< T, vector< size_t > > blobsBoundBox(const Image< T > &imLbl, const bool onlyNonZero=true)
blobsBoundBox() - Measure bounding boxes of labeled image.
Definition: DBlobMeasures.hpp:239
RES_T drawOverlay(const Image< T > &imToDraw, Image< T > &imOut)
Draw overlay.
Definition: DImage.hpp:516
RES_T drawLine(Image< T > &im, int x0, int y0, int x1, int y1, T value=ImDtTypes< T >::max())
Draws a line between two points P0(x0,y0) and P1(x1,y1).
Definition: DImageDraw.hpp:58
RES_T threshold(const Image< T > &imIn, T minVal, T maxVal, T_out trueVal, T_out falseVal, Image< T_out > &imOut)
threshold() - Image threshold
Definition: DImageHistogram.hpp:269
size_t label(const Image< T1 > &imIn, Image< T2 > &imOut, const StrElt &se=DEFAULT_SE)
label() - Image labelization
Definition: DMorphoLabel.hpp:564
RES_T topHat(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE)
topHat() - Top-Hat
Definition: DMorphoResidues.hpp:115