Area-open followed by the hmax

001. from code.na_ni import*
002. from code.parent_s import*
003. import numpy as np
004. import time
005. import matplotlib.pyplot as plt
006. import ia636
007. import cv2
008. 
009. 
010. #number of repetitions
011. nrepetitions = 1
012. 
013. # number of image concatenations
014. nconcatenations = 5
015. 
016. #Images to be tested, all must have the same initial size
017. imgs_path = '/awmedia/www/media/Attachments/homeroberto/maxtreeicip2015_height_test/sample_images/'
018. imgs = [ os.path.join(imgs_path,f) for f in os.listdir(imgs_path) if os.path.isfile(os.path.join(imgs_path,f)) ]
019. a = len(imgs)
020. 
021. 
022. value = 200.1
023. h_value = 10.1
024. Bc = np.ones((3,3), dtype = bool) # structuring element
025. 
026. #Arrays to store the results
027. sizes = np.zeros(nconcatenations, dtype = np.float)
028. par_s = np.zeros((a,nconcatenations), dtype = np.float)
029. na_ni = np.zeros((a,nconcatenations), dtype = np.float)
030. na_ni_p =  np.zeros((a,nconcatenations), dtype = np.float)
031. 
032. for (kk,img_name) in enumerate(imgs):
033.     img = adreadgray(img_name)
034.     img = cv2.resize(img, (1024, 1024), interpolation = cv2.INTER_NEAREST)
035.     img[0,:] = 0
036.     img[-1,:] = 0
037.     img[:,0] = 0
038.     img[:,-1] = 0
039.     temp = np.zeros((3,nrepetitions), dtype = np.float)
040. 
041.     for i in xrange(1,nconcatenations+1):
042. 
043.         if i == 1:
044.             pass
045.         else:
046.             img = np.concatenate((img,img), axis = i%2)
047.         for k in xrange(nrepetitions):
048. 
049.             t1 = time.time()
050.             par,S_rev = max_tree_par_S(img,Bc)
051.             t1 = time.time() - t1
052.             t = time.time()
053.             area2 = compute_area(S_rev,par)
054.             out = direct_filter(int(value),S_rev,par,img,area2)
055.             height = compute_height(S_rev,par,out)
056.             out2 = direct_filter(int(h_value),S_rev,par,out,height)
057. 
058.             t = time.time() - t
059.             temp[0,k] = t
060.             del par
061.             del S_rev
062.             del area2
063.             del out
064.             t = time.time()
065.             mxt = MaxTreeICIP2015(img,Bc)
066.             mxt.contractDR2(mxt.node_array[2,:] > value)
067.             height = mxt.computeHeight()
068.             mxt.contractDR2(height > h_value)
069.             out = mxt.getImage2()
070.             t = time.time() -t - t1
071.             del mxt
072.             del out
073.             temp[1,k] = t
074. 
075. 
076.             t = time.time()
077.             mxt2 = MaxTreeICIP2015(img,Bc)
078.             mxt2.contractDR(mxt2.node_array[2,:] > value)
079.             height = mxt2.computeHeight()
080.             mxt2.contractDR(height > h_value)
081.             out2 = mxt2.getImage()
082. 
083.             t = time.time() -t - t1
084.             del mxt2
085.             del out2
086.             temp[2,k] = t
087.         sizes[i-1] = img.size
088.         par_s[kk,i-1] = temp[0].mean()
089.         na_ni_p[kk,i-1] = temp[1].mean()
090.         na_ni[kk,i-1] = temp[2].mean()
091. 
092. 
093. print "Avereage processing time parent/S:", par_s.mean(axis = 0)
094. print "Avereage processing time NA/NI:",na_ni.mean(axis = 0)
095. print "Avereage processing time NA/NI OpenMP:", na_ni_p.mean(axis = 0)
096. 
097. fig = plt.figure(figsize = (8,5),dpi = 900, facecolor= 'w')
098. ax = fig.add_subplot(1,1,1)
099. plt.plot(sizes, par_s.mean(axis = 0) ,  marker='^', linestyle = '-', markersize=12,linewidth = 2.0, label = 'Parent/S ')
100. plt.plot(sizes, na_ni_p.mean(axis = 0) ,  marker='>',linestyle = '--', markersize=12,linewidth = 2.0, label = 'Node array/Node index OpenMP ')
101. plt.plot(sizes, na_ni.mean(axis = 0) ,  marker='x', linestyle = '-.', markersize=12,linewidth = 2.0, label = 'Node array/Node index')
102. 
103. plt.xlabel("Number of pixels",fontweight='bold',fontsize = 12)
104. plt.ylabel("Average processing time (s)",fontweight='bold',fontsize = 12)
105. plt.title("Area-open followed by the hmax filter")
106. plt.grid()
107. ax.set_xscale('log')
108. ax.set_yscale('log')
109. plt.axis('tight')
110. legend = ax.legend(loc='top left', shadow=True)
111. adshow(ia636.iafig2img(fig))
Avereage processing time parent/S: [ 0.05467001  0.12763483  0.29966765  0.79398052  1.77685885]
Avereage processing time NA/NI: [ 0.04595503  0.08829798  0.23201994  0.5637192   1.32010043]
Avereage processing time NA/NI OpenMP: [ 0.02773358  0.04625726  0.10023843  0.39118917  0.95971518]