00001
00002
00003
00004 #include <flgrCoreTypes.h>
00005 #include <flgrCoreErrors.h>
00006 #include <flgrCoreData.h>
00007 #include <flgrCoreIO.h>
00008 #include <flgrCoreCompare.h>
00009 #include <flgrCoreReplace.h>
00010 #include <flgrCoreThreads.h>
00011 #include <flgrImageIO.h>
00012 #include <flgrMeasureBase.h>
00013 #include <flgrMorphoBase.h>
00014 #include <flgrMorphoWatershed.h>
00015 #include <flgrMorphoGeodesy.h>
00016 #include <flgrMorphoLabel.h>
00017 #include <flgrMorphoDistance.h>
00018 #include <time.h>
00019
00020
00028 #define BENCH_FUCTION(nbtime,textinfo,function,...) \
00029 { \
00030 time_t before_t, after_t; \
00031 clock_t before_c, after_c; \
00032 int i; \
00033 \
00034 before_t = time(NULL); \
00035 before_c = clock(); \
00036 \
00037 for(i=0;i<nbtime;i++) { \
00038 function(__VA_ARGS__); \
00039 } \
00040 \
00041 after_t = time(NULL); \
00042 after_c = clock(); \
00043 \
00044 printf(textinfo " System time : %d us, CPU time : %d us \n", \
00045 (int) (((after_t-before_t)*1000000)/nbtime), \
00046 (int) ((after_c-before_c)/(CLOCKS_PER_SEC/1000000)/nbtime)); \
00047 }
00048
00049
00050
00051
00052
00053
00054
00055 int thread_watershed(void *param) {
00056 FLGR_ThreadsArg *arg = (FLGR_ThreadsArg *) param;
00057 FLGR_Data2D *label = (FLGR_Data2D *) arg->argv[0];
00058 FLGR_Data2D *src = (FLGR_Data2D *) arg->argv[1];
00059 FLGR_Type *type = (FLGR_Type*) arg->argv[2];
00060
00061
00062 flgr2d_watershed(label, src, *type);
00063
00064
00065 return 0;
00066 }
00067
00068
00069
00070
00071 void flgr2d_threaded_4_watershed(FLGR_Data2D *imLabel1, FLGR_Data2D *imin1, FLGR_Connexity *type1,
00072 FLGR_Data2D *imLabel2, FLGR_Data2D *imin2, FLGR_Connexity *type2,
00073 FLGR_Data2D *imLabel3, FLGR_Data2D *imin3, FLGR_Connexity *type3,
00074 FLGR_Data2D *imLabel4, FLGR_Data2D *imin4, FLGR_Connexity *type4) {
00075
00076 FLGR_ThreadsArgList *arglist = flgr_threads_create_argument_list(4);
00077
00078 flgr_threads_set_argument(arglist, 0, 3, imLabel1, imin1, type1);
00079 flgr_threads_set_argument(arglist, 1, 3, imLabel2, imin2, type2);
00080 flgr_threads_set_argument(arglist, 2, 3, imLabel2, imin2, type2);
00081 flgr_threads_set_argument(arglist, 3, 3, imLabel2, imin2, type2);
00082
00083 flgr_threads_start(arglist, 4, 1, thread_watershed);
00084
00085 flgr_threads_destroy_argument_list(arglist);
00086
00087 }
00088
00089
00090
00091
00092
00093
00094 int main(void) {
00095 FLGR_Data2D *imin,*imtmp,*imFilter,*imDistance,*imMarker,*imMaxima,*imWatershed;
00096 FLGR_Data2D *imLabel,*imLabel2,*imLabel3,*imLabel4;
00097 FLGR_Data2D *nhb1,*nhb2,*nhb3,*nhb4,*nhb5;
00098
00099 FLGR_Vector *vec_min, *vec_max;
00100 FLGR_Vector *vec_hmin;
00101
00102 imin = flgr2d_load_pgm("../../images/gray/beans.pgm");
00103 imtmp = flgr2d_create_from(imin);
00104 imFilter = flgr2d_create_from(imin);
00105 imDistance = flgr2d_create_from(imin);
00106 imMarker = flgr2d_create_from(imin);
00107 imMaxima = flgr2d_create_from(imin);
00108
00109 imLabel = flgr2d_create_from(imin);
00110 imLabel2 = flgr2d_create_from(imin);
00111 imLabel3 = flgr2d_create_from(imin);
00112 imLabel4 = flgr2d_create_from(imin);
00113
00114 imWatershed = flgr2d_create_from(imin);
00115
00116 vec_min = flgr_vector_create(imin->spp,imin->type);
00117 vec_max = flgr_vector_create(imin->spp,imin->type);
00118 vec_hmin = flgr_vector_create(imin->spp,imin->type);
00119 flgr_vector_populate_from_string(vec_hmin,"1");
00120
00121
00122 nhb1 = flgr2d_create_neighborhood(3,3,imin->spp,imin->type,FLGR_RECT,FLGR_8_CONNEX);
00123 nhb2 = flgr2d_create_neighborhood(5,5,imin->spp,imin->type,FLGR_RECT,FLGR_8_CONNEX);
00124 nhb3 = flgr2d_create_neighborhood(7,7,imin->spp,imin->type,FLGR_RECT,FLGR_8_CONNEX);
00125 nhb4 = flgr2d_create_neighborhood(9,9,imin->spp,imin->type,FLGR_RECT,FLGR_8_CONNEX);
00126 nhb5 = flgr2d_create_neighborhood(11,11,imin->spp,imin->type,FLGR_RECT,FLGR_8_CONNEX);
00127
00128 flgr2d_replace_I_with_S_S_I(imin,imin,"==","255","254",imin);
00129
00130
00131 BENCH_FUCTION(100000,"Open Filter",flgr2d_open,imtmp,imin,nhb1);
00132 flgr2d_close(imFilter,imtmp,nhb1);
00133
00134
00135 flgr2d_replace_I_with_S_S_S(imtmp,imFilter,"<","128","255","0");
00136 flgr2d_distance(imDistance,imtmp,nhb1->connexity);
00137
00138
00139 flgr2d_regional_hmaxima(imMaxima,imDistance,vec_hmin,nhb1->connexity);
00140 flgr2d_replace_I_with_S_S_S(imtmp,imMaxima,">","0","255","0");
00141 flgr2d_dilate(imMaxima,imtmp,nhb5);
00142
00143
00144 flgr2d_label(imLabel, imMaxima, nhb1->connexity);
00145 flgr2d_label(imLabel2, imMaxima, nhb1->connexity);
00146
00147
00148 BENCH_FUCTION(1000,"Watershed",flgr2d_watershed,imLabel, imin, nhb1->connexity);
00149
00150 BENCH_FUCTION(1000,"4 Threads 4 Watersheds",
00151 flgr2d_threaded_4_watershed,
00152 imLabel, imin, &(nhb1->connexity),
00153 imLabel2, imin, &(nhb1->connexity),
00154 imLabel3, imin, &(nhb1->connexity),
00155 imLabel4, imin, &(nhb1->connexity));
00156
00157
00158 flgr2d_watershed_build_line(imWatershed, imLabel, nhb1->connexity);
00159 flgr2d_replace_I_with_S_S_I(imWatershed,imWatershed,"==","1","255",imin);
00160
00161 flgr2d_save_pgm(imFilter,"filter.pgm",5);
00162 flgr2d_save_pgm(imDistance,"distance.pgm",5);
00163 flgr2d_save_pgm(imMaxima,"maxima.pgm",5);
00164 flgr2d_save_pgm(imLabel,"label.pgm",5);
00165 flgr2d_save_pgm(imWatershed,"watershed.pgm",5);
00166
00167 flgr2d_destroy(imin);
00168 flgr2d_destroy(imtmp);
00169 flgr2d_destroy(imFilter);
00170 flgr2d_destroy(imDistance);
00171 flgr2d_destroy(imMarker);
00172 flgr2d_destroy(imMaxima);
00173 flgr2d_destroy(imLabel);
00174 flgr2d_destroy(imLabel2);
00175 flgr2d_destroy(imLabel3);
00176 flgr2d_destroy(imLabel4);
00177 flgr2d_destroy(imWatershed);
00178 flgr2d_destroy(nhb1);
00179 flgr2d_destroy(nhb2);
00180 flgr2d_destroy(nhb3);
00181 flgr2d_destroy(nhb4);
00182 flgr2d_destroy(nhb5);
00183
00184 flgr_vector_destroy(vec_max);
00185 flgr_vector_destroy(vec_min);
00186 flgr_vector_destroy(vec_hmin);
00187
00188 return 0;
00189 }