00001
00002
00003
00004
00005
00006
00011 #include <flgrCoreTypes.h>
00012 #include <flgrCoreErrors.h>
00013 #include <flgrCoreData.h>
00014 #include <flgrCoreCompare.h>
00015 #include <flgrCoreVector.h>
00016 #include <flgrArith.h>
00017 #include <flgrImageIO.h>
00018 #include <flgrMorphoBase.h>
00019
00020 #include <time.h>
00021
00022 #define BENCH_FUNCTION(nbtime,textinfo,function,...) \
00023 { \
00024 time_t before_t, after_t; \
00025 clock_t before_c, after_c; \
00026 int i; \
00027 \
00028 before_t = time(NULL); \
00029 before_c = clock(); \
00030 \
00031 for(i=0;i<nbtime;i++) { \
00032 function(__VA_ARGS__); \
00033 } \
00034 \
00035 after_t = time(NULL); \
00036 after_c = clock(); \
00037 \
00038 printf(textinfo " System time : %d us, CPU time : %d us \n", \
00039 (int) (((after_t-before_t)*1000000)/nbtime), \
00040 (int) ((after_c-before_c)/(CLOCKS_PER_SEC/1000000)/nbtime)); \
00041 }
00042
00043
00044
00045
00046
00047
00048
00049 FLGR_Ret geodesic_rec_dilate(FLGR_Data2D *immarker, FLGR_Data2D *immask, FLGR_Data2D *nhbc) {
00050 FLGR_Data2D *nhb,*imtmp1,*imtmp2;
00051 FLGR_Ret ret;
00052
00053 nhb = flgr2d_create_neighborhood_from_connexity(immarker->spp,immarker->type, nhbc->connexity);
00054
00055 imtmp1 = flgr2d_create_pixmap(immarker->size_y,immarker->size_x,immarker->spp,immarker->type);
00056 imtmp2 = flgr2d_create_pixmap(immarker->size_y,immarker->size_x,immarker->spp,immarker->type);
00057
00058 if((ret=flgr2d_copy(imtmp2,immarker))!=FLGR_RET_OK) goto fail;
00059 if((ret=flgr2d_dilate(imtmp1,imtmp2,nhb))!=FLGR_RET_OK) goto fail;
00060 if((ret=flgr2d_arith_inf(imtmp2,immask,imtmp1))!=FLGR_RET_OK) goto fail;
00061
00062 while(flgr2d_compare(immarker,"==",imtmp2)==0) {
00063 if((ret=flgr2d_copy(immarker,imtmp2))!=FLGR_RET_OK) goto fail;
00064 if((ret=flgr2d_dilate(imtmp1,imtmp2,nhb))!=FLGR_RET_OK) goto fail;
00065 if((ret=flgr2d_arith_inf(imtmp2,immask,imtmp1))!=FLGR_RET_OK) goto fail;
00066 }
00067
00068 fail:
00069 flgr2d_destroy(nhb);
00070 flgr2d_destroy(imtmp1);
00071 flgr2d_destroy(imtmp2);
00072
00073 return ret;
00074
00075 }
00076
00077
00078
00079
00080 FLGR_Ret geodesic_rec_erode(FLGR_Data2D *immarker, FLGR_Data2D *immask, FLGR_Data2D *nhbc) {
00081 FLGR_Data2D *nhb,*imtmp1,*imtmp2;
00082 FLGR_Ret ret;
00083
00084 nhb = flgr2d_create_neighborhood_from_connexity(immarker->spp,immarker->type, nhbc->connexity);
00085
00086 imtmp1 = flgr2d_create_pixmap(immarker->size_y,immarker->size_x,immarker->spp,immarker->type);
00087 imtmp2 = flgr2d_create_pixmap(immarker->size_y,immarker->size_x,immarker->spp,immarker->type);
00088
00089 if((ret=flgr2d_copy(imtmp2,immarker))!=FLGR_RET_OK) goto fail;
00090 if((ret=flgr2d_erode(imtmp1,imtmp2,nhb))!=FLGR_RET_OK) goto fail;
00091 if((ret=flgr2d_arith_sup(imtmp2,immask,imtmp1))!=FLGR_RET_OK) goto fail;
00092
00093 while(flgr2d_compare(immarker,"==",imtmp2)==0) {
00094 if((ret=flgr2d_copy(immarker,imtmp2))!=FLGR_RET_OK) goto fail;
00095 if((ret=flgr2d_erode(imtmp1,imtmp2,nhb))!=FLGR_RET_OK) goto fail;
00096 if((ret=flgr2d_arith_sup(imtmp2,immask,imtmp1))!=FLGR_RET_OK) goto fail;
00097 }
00098
00099 fail:
00100 flgr2d_destroy(nhb);
00101 flgr2d_destroy(imtmp1);
00102 flgr2d_destroy(imtmp2);
00103
00104 return ret;
00105
00106 }
00107
00108
00109
00110
00111 FLGR_Ret geodesic_rec_open(FLGR_Data2D *imdest,FLGR_Data2D *imin,FLGR_Data2D *nhb) {
00112 FLGR_Ret ret;
00113
00114 if((ret=flgr2d_erode(imdest,imin,nhb))!=FLGR_RET_OK) return ret;
00115 ret=geodesic_rec_dilate(imdest,imin,nhb);
00116
00117 return ret;
00118 }
00119
00120
00121
00122
00123 FLGR_Ret geodesic_rec_close(FLGR_Data2D *imdest,FLGR_Data2D *imin,FLGR_Data2D *nhb) {
00124 FLGR_Ret ret;
00125
00126 if((ret=flgr2d_dilate(imdest,imin,nhb))!=FLGR_RET_OK) return ret;
00127 ret=geodesic_rec_erode(imdest,imin,nhb);
00128
00129 return ret;
00130 }
00131
00132
00133
00134
00135
00136 FLGR_Ret geodesic_rec_open_tophat(FLGR_Data2D *imdest,FLGR_Data2D *imin,FLGR_Data2D *nhb) {
00137 FLGR_Ret ret;
00138
00139 if((ret=geodesic_rec_open(imdest,imin,nhb))!=FLGR_RET_OK) return ret;
00140 ret=flgr2d_arith_sub(imdest,imin,imdest);
00141
00142 return ret;
00143 }
00144
00145
00146
00147
00148 FLGR_Ret geodesic_rec_close_tophat(FLGR_Data2D *imdest,FLGR_Data2D *imin,FLGR_Data2D *nhb) {
00149 FLGR_Ret ret;
00150
00151 if((ret=geodesic_rec_close(imdest,imin,nhb))!=FLGR_RET_OK) return ret;
00152 ret=flgr2d_arith_sub(imdest,imdest,imin);
00153
00154 return ret;
00155 }
00156
00157
00158
00159
00160 int main(void) {
00161 FLGR_Data2D *imin;
00162 FLGR_Data2D *imDest1;
00163 FLGR_Data2D *imDest2;
00164 FLGR_Data2D *imDest3;
00165 FLGR_Data2D *imDest4;
00166 FLGR_Data2D *nhb;
00167 FLGR_Data2D *nhb2;
00168
00169 imin = flgr2d_load_pgm("../../images/gray/lena.pgm");
00170
00171 imDest1 = flgr2d_create_pixmap_from(imin);
00172 imDest2 = flgr2d_create_pixmap_from(imin);
00173 imDest3 = flgr2d_create_pixmap_from(imin);
00174 imDest4 = flgr2d_create_pixmap_from(imin);
00175 nhb = flgr2d_create_neighborhood(9,9,1,FLGR_UINT8,FLGR_HEX,FLGR_6_CONNEX);
00176 nhb2 = flgr2d_create_neighborhood_from_connexity(1,FLGR_UINT8,FLGR_8_CONNEX);
00177
00178
00179 flgr2d_dilate(imDest1, imin, nhb);
00180 flgr2d_copy(imDest2,imDest1);
00181 flgr2d_copy(imDest3,imDest1);
00182
00183 BENCH_FUNCTION(100000,"Erode 3x3 benchmark", flgr2d_erode,imDest4,imin,nhb2);
00184
00185 BENCH_FUNCTION(100,"Fast Dual Geodesic reconstruction",
00186 flgr2d_geodesic_reconstruct_dual,imDest1,imin,nhb->connexity);
00187
00188 BENCH_FUNCTION(100,"Fast Geodesic reconstruction by closing",
00189 flgr2d_geodesic_reconstruct_erode,imDest2,imin,nhb->connexity);
00190
00191 BENCH_FUNCTION(100,"Naive Geodesic reconstruction by closing",
00192 geodesic_rec_erode,imDest3,imin,nhb);
00193
00194 flgr2d_save_pgm(imDest1,"fast_dual.pgm",5);
00195 flgr2d_save_pgm(imDest2,"fast.pgm",5);
00196 flgr2d_save_pgm(imDest3,"dual.pgm",5);
00197
00198 flgr2d_destroy(imin);
00199 flgr2d_destroy(imDest1);
00200 flgr2d_destroy(imDest2);
00201 flgr2d_destroy(imDest3);
00202 flgr2d_destroy(imDest4);
00203 flgr2d_destroy(nhb);
00204 flgr2d_destroy(nhb2);
00205 }
00206
00207