00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 import time
00013
00014 from fulguro import *
00015
00016
00017 def benchmark(function,nbiter,*args):
00018 t=time.time()
00019 for i in range(nbiter):
00020 function(*args)
00021 t2=time.time()
00022 return ((t2-t)*1000)/nbiter
00023
00024
00025
00026
00027
00028
00029 def geodesic_rec_dilate(immarker,immask,nhb):
00030 if nhb.connexity==FLGR_8_CONNEX:
00031 nhb = FLGR_Data2D(3,3,nhb.spp,immarker.type,FLGR_RECT,FLGR_8_CONNEX)
00032
00033 elif nhb.connexity==FLGR_6_CONNEX:
00034 nhb = FLGR_Data2D(3,3,nhb.spp,immarker.type,FLGR_HEX,FLGR_6_CONNEX)
00035
00036 else:
00037 nhb = FLGR_Data2D(3,3,nhb.spp,immarker.type,FLGR_CROSS,FLGR_4_CONNEX);
00038
00039 imtmp1 = FLGR_Data2D(immarker)
00040 imtmp2 = FLGR_Data2D(immarker)
00041
00042 flgr2d_copy(imtmp2,immarker)
00043 flgr2d_dilate(imtmp1,imtmp2,nhb)
00044 flgr2d_arith_inf(imtmp2,immask,imtmp1)
00045
00046 vol1 = flgr2d_measure_volume(immarker)[0]
00047 vol2 = flgr2d_measure_volume(imtmp2)[0]
00048
00049 while (vol1 != vol2):
00050 vol1 = vol2
00051 flgr2d_copy(immarker,imtmp2)
00052 flgr2d_dilate(imtmp1,imtmp2,nhb)
00053 flgr2d_arith_inf(imtmp2,immask,imtmp1)
00054 vol2 = flgr2d_measure_volume(imtmp2)[0]
00055
00056
00057 def geodesic_rec_dilate2(immarker,immask,nhb):
00058 imtmp = FLGR_Data2D(immarker)
00059
00060 while not(imtmp == immarker):
00061 flgr2d_copy(imtmp,immarker)
00062 flgr2d_geodesic_dilate_8_connexity(immarker, immask)
00063
00064
00065
00066
00067
00068 def geodesic_rec_erode(immarker,immask,nhb):
00069 if nhb.connexity==FLGR_8_CONNEX:
00070 nhb = FLGR_Data2D(3,3,nhb.spp,immarker.type,FLGR_RECT,FLGR_8_CONNEX)
00071
00072 elif nhb.connexity==FLGR_6_CONNEX:
00073 nhb = FLGR_Data2D(3,3,nhb.spp,immarker.type,FLGR_HEX,FLGR_6_CONNEX)
00074
00075 else:
00076 nhb = FLGR_Data2D(3,3,nhb.spp,immarker.type,FLGR_CROSS,FLGR_4_CONNEX);
00077
00078 imtmp1 = FLGR_Data2D(immarker)
00079 imtmp2 = FLGR_Data2D(immarker)
00080
00081 flgr2d_copy(imtmp2,immarker)
00082 flgr2d_erode(imtmp1,imtmp2,nhb)
00083 flgr2d_arith_sup(imtmp2,immask,imtmp1)
00084
00085 while not(immarker == imtmp2):
00086 flgr2d_copy(immarker,imtmp2)
00087 flgr2d_erode(imtmp1,imtmp2,nhb)
00088 flgr2d_arith_sup(imtmp2,immask,imtmp1)
00089
00090 def geodesic_rec_erode2(immarker,immask,nhb):
00091 imtmp = FLGR_Data2D(immarker)
00092
00093 while not(imtmp == immarker):
00094 flgr2d_copy(imtmp,immarker)
00095 flgr2d_geodesic_erode_8_connexity(immarker, immask)
00096
00097
00098
00099
00100 def geodesic_rec_open(imdest,imin,nhb):
00101 flgr2d_erode(imdest,imin,nhb)
00102 geodesic_rec_dilate(imdest,imin,nhb)
00103
00104
00105
00106
00107 def geodesic_rec_close(imdest,imin,nhb):
00108 flgr2d_dilate(imdest,imin,nhb)
00109 geodesic_rec_erode(imdest,imin,nhb)
00110
00111
00112
00113
00114 def geodesic_rec_open_tophat(imdest,imin,nhb):
00115 geodesic_rec_open(imdest,imin,nhb)
00116 flgr2d_arith_sub(imdest,imin,imdest)
00117
00118
00119
00120
00121 def geodesic_rec_close_tophat(imdest,imin,nhb):
00122 geodesic_rec_close(imdest,imin,nhb)
00123 flgr2d_arith_sub(imdest,imdest,imin)
00124
00125
00126 def printImage(imin):
00127 a = flgr2d_export_raw(imin)
00128 w = imin.size_x
00129 for i in range(imin.size_y-1):
00130 print a[i*w:(i+1)*w]
00131 print
00132
00133
00134
00135
00136
00137
00138 imin = flgr2d_load_pgm('../../images/gray/lena.pgm')
00139 imDest1 = FLGR_Data2D(imin)
00140 imDest2 = FLGR_Data2D(imin)
00141 imDest3 = FLGR_Data2D(imin)
00142 imDest4 = FLGR_Data2D(imin)
00143 imDest5 = FLGR_Data2D(imin)
00144 nhb = FLGR_Data2D(11,11,imin.spp,imin.type,FLGR_RECT,FLGR_8_CONNEX)
00145
00146
00147
00148 flgr2d_erode(imDest1, imin, nhb)
00149 flgr2d_copy(imDest2,imDest1)
00150 flgr2d_copy(imDest3,imDest1)
00151 flgr2d_copy(imDest4,imDest1)
00152 flgr2d_copy(imDest5,imDest1)
00153
00154
00155
00156 speed = benchmark(flgr2d_geodesic_reconstruct_dual,20,imDest1,imin,nhb.connexity)
00157 print 'Fast Dual Geodesic reconstruction time :',speed,'ms'
00158
00159 speed = benchmark(flgr2d_geodesic_reconstruct_dilate,20,imDest2,imin,nhb.connexity)
00160 print 'Fast Geodesic reconstruction by dilation time :',speed,'ms'
00161
00162 speed = benchmark(geodesic_rec_dilate,100,imDest3,imin,nhb)
00163 print 'Naive Geodesic reconstruction time :',speed,'ms'
00164
00165 speed = benchmark(geodesic_rec_dilate2,100,imDest4,imin,nhb)
00166 print 'Naive partial C layer Geodesic reconstruction time :',speed,'ms'
00167
00168 speed = benchmark(flgr2d_geodesic_reconstruct_dilate_parallel,100,imDest5,imin,nhb.connexity)
00169 print 'Naive C layer Geodesic reconstruction time :',speed,'ms'
00170
00171
00172
00173
00174 if imDest1==imDest2 and imDest2==imDest3 and imDest3==imDest4 and imDest4==imDest5:
00175 print "Images are equals :)"
00176 else:
00177 print "Images are not equals :("
00178
00179
00180
00181 flgr_display(imDest1,"fast_dual",imDest2,"fast",imDest3,"naive",imDest4,"naive_partial_c",imDest5,"naive_c")