draw1DImageTree

MainPage

Description: This method draws the max-tree or component tree of 1D images with grey-levels varying between 0 and 7.

Signature: MaxTreeAlpha.draw1DImageTree(tree = 'mt')

Input:
  • tree: str, option 'mt' will draw the max-tree, if option is 'ct' it will draw the component tree.

Output:

Python Implementation

  1 import numpy as np
  2 import adpil
  3 import cv2
  4 
  5 def draw1DImageTree(self, tree = 'mt'):
  6   pixels_size = 25
  7   P = self.node_array[0,:]
  8   H = self.node_array[2,:]
  9   Ymin = self.node_array[9,:]
 10   Ymax = self.node_array[10,:]
 11   Xmin = self.node_array[6,:]
 12   Xmax = self.node_array[7,:]
 13 
 14   hmin = H.min()
 15   nglevels = H.max() - hmin  + 1  # Number of grey-levels in the 1D input image
 16   npixels = Ymax.max() + 1
 17 
 18   # Pre-allocating an 2d-array to store the tree image
 19   tree_image = np.ones((nglevels,npixels), dtype = 'uint8')*255
 20   # Pre-allocating an 2d-array to store the centroids of the tree nodes
 21   nodes_image = np.zeros((nglevels,npixels), dtype = 'uint8')
 22   tree_connections = []
 23   connection = [0,0,0,0]
 24   composite_nodes = []
 25   self.getSubBranches(0)
 26 
 27   for i in xrange(self._cum_sb_hist.size-1, 0,-1):
 28     sb =  self._sb[self._cum_sb_hist[i-1]:self._cum_sb_hist[i]]
 29     for j in sb:
 30       h = H[j]
 31       p = P[j]
 32       hp = H[p] # Grey-level of the parent
 33 
 34       ymin,ymax = Ymin[j],Ymax[j]
 35       LC = self.recConnectedComponent(j).flatten() # Recovered level component
 36       tree_image[(h - hmin),ymin:ymax+1]= LC[ymin:ymax+1]*h*35.0
 37 
 38       ys = (ymin+ymax+1)/2
 39 
 40       connection[0] = h - hmin
 41       connection[1] = ys
 42       nodes_image[(h-hmin),ys] = 2
 43 
 44       if hp != (h-1):
 45         composite_nodes.append([h-hmin,ys])
 46         for k in xrange(hp+1,h):
 47           tree_image[k - hmin,ymin:ymax+1] = LC[ymin:ymax+1]*k*35.0
 48           if tree == 'ct':
 49             nodes_image[k-hmin,ys] = 2
 50             connection[2] = hp - hmin + 1
 51             connection[3] = ys
 52             tree_connections.append(connection[:]) # [:]?
 53             connection[0] = hp - hmin + 1
 54             connection[1] = ys
 55       connection[2] = hp - hmin
 56       connection[3] =  (Ymin[p]+Ymax[p]+1)/2
 57       tree_connections.append(connection[:])
 58 
 59   tree_image = np.repeat(tree_image,pixels_size, axis = 1)
 60   tree_image = np.repeat(tree_image,pixels_size, axis = 0)
 61 
 62   new_nodes_image = np.zeros((nglevels*pixels_size,npixels*pixels_size), dtype = 'uint8')
 63   new_nodes_image[pixels_size/2::pixels_size,pixels_size/2::pixels_size] = nodes_image
 64 
 65   new_nodes_imagek = (new_nodes_image == 2).astype(np.uint8)
 66   disk = np.ones((5,5),np.uint8)
 67   new_nodes_imagek = cv2.dilate(new_nodes_imagek,disk,iterations = 1) > 0
 68 
 69 
 70   if tree == 'mt':
 71     bool_img = tree_image != 255
 72     bool_img2 = bool_img.copy()
 73     bool_img2[::2,:] = 0
 74     for kk in xrange(nglevels-1):
 75       bool_img[kk*pixels_size:(kk+1)*pixels_size,:] = bool_img[kk*pixels_size:(kk+1)*pixels_size,:] - \
 76                                                      bool_img2[(kk+1)*pixels_size:(kk+2)*pixels_size,:]
 77       tree_image[~bool_img] = 255
 78 
 79   tree_image = np.array([tree_image,tree_image,tree_image])
 80   tree_image[0][new_nodes_imagek] = 255
 81   tree_image[1][new_nodes_imagek] = 0
 82   tree_image[2][new_nodes_imagek] = 0
 83 
 84 
 85   if tree == 'mt':
 86     r = 9
 87     t = np.arange(0,1.01,.01)*2*np.pi
 88     for ii in composite_nodes[1:]:
 89       x,y = pixels_size/2+pixels_size*ii[1],pixels_size/2+pixels_size*ii[0]
 90       xc = (np.round(r*np.cos(t)) + x).astype(int)
 91       yc = (np.round(r*np.sin(t)) + y).astype(int)
 92       tree_image[0,yc,xc] = 255
 93       tree_image[1,yc,xc] = 0
 94       tree_image[2,yc,xc] = 0
 95 
 96   draw = adpil.adDraw(tree_image, rgb = 1)
 97   for jj in tree_connections:
 98     p1 = (pixels_size/2+pixels_size*jj[1],pixels_size/2+pixels_size*jj[0],\
 99           pixels_size/2+pixels_size*jj[3],pixels_size/2+pixels_size*jj[2])
100     draw.line(p1,fill = (255,0,0))
101   tree_image = draw.get_image()[:,::-1,:]
102 
103 
104   tree_image[2,:,::pixels_size] = 0
105   tree_image[2,::pixels_size,:] = 0
106   tree_image[0,::pixels_size,:] = 255
107   tree_image[0,:,::pixels_size] = 255
108   tree_image[1,::pixels_size,:] = 255
109   tree_image[1,:,::pixels_size] = 255
110   tree_image[2,:,-1] = 0
111   tree_image[2,-1,:] = 0
112   tree_image[0,-1,:] = 255
113   tree_image[0,:,-1] =255
114   tree_image[1,-1,:] = 255
115   tree_image[1,:,-1] = 255
116   text = adpil.adread('/awmedia/www/media/Attachments/iamxt/MainPage/grey_levels.png')
117   return np.concatenate((text[:,pixels_size*(10-nglevels):,:],tree_image), axis = 2)

Example

 1 from max_tree_alpha import MaxTreeAlpha
 2 import numpy as np
 3 
 4 
 5 img1d = np.array([[0, 6, 2, 5, 1, 1, 3, 3, 1, 0]], dtype = np.uint8)
 6 Bc = np.ones((1,3), dtype = bool)
 7 mxt = MaxTreeAlpha(img1d,Bc)
 8 mt = mxt.draw1DImageTree()
 9 ct = mxt.draw1DImageTree('ct')
10 
11 adshow(mt,"Max-tree of the image [0,3,2,2,1,0,1,5,5,6]")
12 adshow(ct,"Component tree of the image [0,3,2,2,1,0,1,5,5,6]")

Max-tree of the image [0,3,2,2,1,0,1,5,5,6]

Component tree of the image [0,3,2,2,1,0,1,5,5,6]