SMIL  0.9.1
custom_graph_operator.py


1 from smilPython import *
2 
3 ## Custom mosaicToGraph functor which takes the mean pixel value along the border between two basins instead of the min value
4 class 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  # check if a corresponding edge already exists
17  eInd = self.graph.findEdge(curVal, nbVal)
18  if eInd==-1: # not found -> create a new one
19  if type(self.imNodeValues)!=type(None): # If an image with node values is given
20  self.graph.addNode(curVal, self.imNodeValues[i])
21  self.graph.addNode(nbVal, self.imNodeValues[i+nb])
22  if type(self.imEdgeValues)!=type(None): # If an image with edge values is given
23  self.borderValues.append([self.imEdgeValues[i]])
24  self.graph.addEdge(curVal, nbVal, 0, False) # False -> avoids to check if the edge already exists (done previoulsy)
25  else: # edge already exists
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 
36 im1 = Image("http://smil.cmm.mines-paristech.fr/images/mosaic.png")
37 im2 = Image(im1)
38 im3 = Image(im1)
39 imMos = Image(im1, "UINT16")
40 imArea = Image(imMos)
41 imSeg = Image(imMos)
42 
43 label(im1, imMos)
44 labelWithArea(im1, imArea)
45 
46 func = customMosaicToGraphFunct()
47 g = func(imMos, imArea)
48 
49 drawGraph(imMos, g, imSeg)
50 imMos.getViewer().drawOverlay(imSeg)
51 
52 g.removeNodeEdges(3)
53 graphToMosaic(imMos, g, imSeg)
54 
55 im1.show()
56 imMos.showLabel()
57 imSeg.showLabel()