1from smilPython import *
2
3
4class customMosaicToGraphFunct(mosaicToGraphFunct_UINT16_UINT16):
5 def initialize(self, imMos, dummy, se):
6 mosaicToGraphFunct_UINT16_UINT16.initialize(self, imMos, dummy, se)
7 self.edges = self.graph.getEdges()
8 self.borderValues = []
9 return RES_OK
10 def processPixel(self, i, relOffList):
11 pixSum = 0.
12 curVal = self.imMosaic[i]
13 for nb in relOffList:
14 nbVal = self.imMosaic[i + nb]
15 if curVal!=nbVal:
16
17 eInd = self.graph.findEdge(curVal, nbVal)
18 if eInd==-1:
19 if type(self.imNodeValues)!=type(None):
20 self.graph.addNode(curVal, self.imNodeValues[i])
21 self.graph.addNode(nbVal, self.imNodeValues[i+nb])
22 if type(self.imEdgeValues)!=type(None):
23 self.borderValues.append([self.imEdgeValues[i]])
24 self.graph.addEdge(curVal, nbVal, 0, False)
25 else:
26 if type(self.imEdgeValues)!=type(None):
27 self.borderValues[eInd].append(self.imEdgeValues[i])
28 def finalize(self, imMos, dummy, se):
29 mosaicToGraphFunct_UINT16_UINT16.finalize(self, imMos, dummy, se)
30 for i in range(len(self.borderValues)):
31 values = self.borderValues[i]
32 self.edges[i].weight = sum(values) / len(values)
33 return RES_OK
34
35
36im1 = Image("https://smil.cmm.minesparis.psl.eu/images/mosaic.png")
37im2 = Image(im1)
38im3 = Image(im1)
39imMos = Image(im1, "UINT16")
40imArea = Image(imMos)
41imSeg = Image(imMos)
42
43label(im1, imMos)
44labelWithArea(im1, imArea)
45
46func = customMosaicToGraphFunct()
47g = func(imMos, imArea)
48
49drawGraph(imMos, g, imSeg)
50imMos.getViewer().drawOverlay(imSeg)
51
52g.removeNodeEdges(3)
53graphToMosaic(imMos, g, imSeg)
54
55im1.show()
56imMos.showLabel()
57imSeg.showLabel()