from fulguro import *
def benchmark(function,nbiter,*args):
import time
t=time.time()
for i in range(nbiter):
function(*args)
t2=time.time()
return ((t2-t)*1000)/nbiter
def quasiDistance(im_indicatrice, im_associe, imin, connexity):
nhb = FLGR_Data2D(imin.spp,imin.type,connexity)
imw1 = FLGR_Data2D(imin)
imw2 = FLGR_Data2D(imin)
imtemp1 = FLGR_Data2D(imin)
flgr2d_copy(imw1,imin)
flgr2d_clear_all(im_indicatrice)
flgr2d_clear_all(im_associe)
vol1 = flgr2d_measure_volume(imw1)[0]
vol2 = vol1+1
i=0
while vol2>vol1:
i += 1
vol2 = vol1
flgr2d_erode(imw2, imw1, nhb)
flgr2d_arith_sub(imw1, imw1, imw2)
flgr2d_replace(imtemp1,imw1,">=",im_associe,i,0);
flgr2d_arith_sup(im_indicatrice, imtemp1, im_indicatrice)
flgr2d_arith_sup(im_associe, imw1, im_associe)
flgr2d_copy(imw1,imw2)
vol1 = flgr2d_measure_volume(imw1)[0]
def descente(imout,imin,connexity):
nhb = FLGR_Data2D(imin.spp,imin.type,connexity)
imw1 = FLGR_Data2D(imin)
imw2 = FLGR_Data2D(imin)
imtemp1 = FLGR_Data2D(imin)
imtemp2 = FLGR_Data2D(imin)
flgr2d_copy(imw1,imin)
vol1 = flgr2d_measure_volume(imw1)[0]
vol2 = vol1 + 1
i=0
while vol2>vol1:
i = i + 1
vol2 = vol1
flgr2d_erode(imw2, imw1, nhb)
flgr2d_arith_sub(imtemp1,imw1,imw2)
flgr2d_arith_add(imtemp2,imw2,1)
flgr2d_arith_inf(imw1,imtemp2,imw1)
vol1 = flgr2d_measure_volume(imw1)[0]
flgr2d_copy(imout,imw1)
imin = flgr2d_load_pgm('../../images/gray/cameraman.pgm')
imqdist1 = FLGR_Data2D(imin)
imresidu = FLGR_Data2D(imin)
imqdist2 = FLGR_Data2D(imin)
print "Quasi distance time : ",benchmark(quasiDistance,20,imqdist1,imresidu,imin,FLGR_8_CONNEX),"ms"
print "Descente time : ",benchmark(descente,20,imqdist2,imqdist1,FLGR_8_CONNEX),"ms"
flgr2d_save_pgm(imqdist1,"quasidistance_perche_fulguro")
flgr2d_save_pgm(imqdist2,"quasidistance_fulguro")
flgr2d_save_pgm(imresidu,"quasidistance_residu_fulguro")
flgr_display(imin,"imin",
imqdist2,'quasidistance',
imresidu,'residu')