00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025
00026 #include <flgrCoreData.h>
00027 #include <flgrCoreDataIO.h>
00028 #include <flgrCoreNhbManage.h>
00029 #include <flgrCoreCopy.h>
00030 #include <flgrCoreDispatch.h>
00031
00032 #include <flgrArith.h>
00033
00034 #include "flgrMorphoLabel.h"
00035
00045 #define lbl(dtype,dtypename) \
00046 int *stackRow, *stackCol; \
00047 int *stackRowPush, *stackColPush; \
00048 int *stackRowPop, *stackColPop; \
00049 dtype *rowLabel, *rowSrc; \
00050 dtype *rowLabel_current; \
00051 \
00052 FLGR_Data2D *nhbOdd; \
00053 FLGR_Data2D *nhbEven; \
00054 FLGR_Data2D *nhb; \
00055 FLGR_NhbBox2D *extr; \
00056 dtype *pextr; \
00057 int *list_x; \
00058 int *list_y; \
00059 dtype label=0; \
00060 int labelOverflow=0; \
00061 int size_y = imglabel->size_y; \
00062 int size_x = imglabel->size_x; \
00063 int i,j,u,v,m,n,k,l,t; \
00064 int currentPixel; \
00065 \
00066 \
00067 \
00068 flgr2d_clear_all(imglabel); \
00069 \
00070 nhb=flgr2d_create_neighborhood_from_connexity(1,dtypename,connexity); \
00071 flgr2d_set_data_array_##dtype((dtype **) nhb->array,1,1,0); \
00072 \
00073 nhbOdd = flgr2d_create_neighborhood_from(nhb); \
00074 nhbEven = flgr2d_create_neighborhood_from(nhb); \
00075 \
00076 flgr2d_fill_nhbs_for_6_connexity(nhbEven,nhbOdd,nhb,FLGR_NHB_NO_SYM); \
00077 \
00078 flgr2d_destroy(nhb); \
00079 \
00080 extr = flgr2d_create_neighbor_box(nhbOdd); \
00081 pextr=(dtype*) extr->list_data_val[0]; \
00082 list_x = extr->list_coord_x[0]; \
00083 list_y = extr->list_coord_y[0]; \
00084 \
00085 stackRow = (int*) malloc(sizeof(int)*size_y*size_x); \
00086 stackCol = (int*) malloc(sizeof(int)*size_y*size_x); \
00087 stackRowPush = stackRowPop = stackRow; \
00088 stackColPush = stackColPop = stackCol; \
00089 \
00090 for(i=0 ; i<size_y ; i++) { \
00091 \
00092 rowLabel = imglabel->array[i]; \
00093 rowSrc = imgsrc->array[i]; \
00094 \
00095 nhb = (((i%2)==1) ? nhbOdd : nhbEven); \
00096 \
00097 for(j=0 ; j<size_x ; j++) { \
00098 \
00099 if((rowLabel[j]==0) && (rowSrc[j]!=0)) { \
00100 \
00101 currentPixel=rowSrc[j]; \
00102 \
00103 label = (label + 1); \
00104 if(label==0) { \
00105 label=1; \
00106 labelOverflow=1; \
00107 } \
00108 \
00109 rowLabel[j]=label; \
00110 \
00111 flgr2d_get_neighborhood_##dtype(extr,imgsrc,nhb, i, j); \
00112 \
00113 for(t=0 ; t<extr->size[0] ; t++) { \
00114 u=list_y[t]; \
00115 v=list_x[t]; \
00116 m=i+u-1; \
00117 n=j+v-1; \
00118 rowLabel_current = imglabel->array[m]; \
00119 if(pextr[t]==currentPixel) { \
00120 *stackRowPush=m; \
00121 stackRowPush++; \
00122 *stackColPush=n; \
00123 stackColPush++; \
00124 rowLabel_current[n]=label; \
00125 } \
00126 } \
00127 \
00128 while(stackRowPush!=stackRow) { \
00129 stackRowPush--; \
00130 stackColPush--; \
00131 m=(*stackRowPush); \
00132 n=(*stackColPush); \
00133 \
00134 nhb = (((m%2)==1) ? nhbOdd : nhbEven); \
00135 \
00136 flgr2d_get_neighborhood_##dtype(extr,imgsrc,nhb,m,n); \
00137 \
00138 for(t=0 ; t<extr->size[0] ; t++) { \
00139 u=list_y[t]; \
00140 v=list_x[t]; \
00141 k=m+u-1; \
00142 l=n+v-1; \
00143 rowLabel_current = (dtype *) imglabel->array[k]; \
00144 if((pextr[t]==currentPixel)&&(rowLabel_current[l]==0)) { \
00145 *stackRowPush=k; \
00146 stackRowPush++; \
00147 *stackColPush=l; \
00148 stackColPush++; \
00149 rowLabel_current[l]=label; \
00150 } \
00151 } \
00152 \
00153 } \
00154 } \
00155 } \
00156 } \
00157 \
00158 flgr2d_destroy(nhbOdd); \
00159 flgr2d_destroy(nhbEven); \
00160 flgr2d_destroy_neighbor_box(extr); \
00161 free(stackRow); \
00162 free(stackCol); \
00163 return label;
00164
00165
00166
00167 int flgr2d_label_fgBIT(FLGR_Data2D *imglabel, FLGR_Data2D *imgsrc, FLGR_Connexity connexity) {
00168 flgr_no_define_type_function(FLGR_BIT);
00169 return FLGR_RET_NOT_IMPLEMENTED;
00170 }
00171
00172 int flgr2d_label_fgUINT8(FLGR_Data2D *imglabel, FLGR_Data2D *imgsrc, FLGR_Connexity connexity) {
00173 lbl(fgUINT8,FLGR_UINT8);
00174 }
00175
00176
00177 int flgr2d_label_fgUINT16(FLGR_Data2D *imglabel, FLGR_Data2D *imgsrc, FLGR_Connexity connexity) {
00178 lbl(fgUINT16,FLGR_UINT16);
00179 }
00180
00181
00182 int flgr2d_label_fgUINT32(FLGR_Data2D *imglabel, FLGR_Data2D *imgsrc, FLGR_Connexity connexity) {
00183 lbl(fgUINT32,FLGR_UINT32);
00184 }
00185
00186 int flgr2d_label_fgINT8(FLGR_Data2D *imglabel, FLGR_Data2D *imgsrc, FLGR_Connexity connexity) {
00187 flgr_no_define_type_function(FLGR_INT8);
00188 return FLGR_RET_NOT_IMPLEMENTED;
00189 }
00190
00191 int flgr2d_label_fgINT16(FLGR_Data2D *imglabel, FLGR_Data2D *imgsrc, FLGR_Connexity connexity) {
00192 flgr_no_define_type_function(FLGR_INT16);
00193 return FLGR_RET_NOT_IMPLEMENTED;
00194 }
00195
00196 int flgr2d_label_fgINT32(FLGR_Data2D *imglabel, FLGR_Data2D *imgsrc, FLGR_Connexity connexity) {
00197 flgr_no_define_type_function(FLGR_INT32);
00198 return FLGR_RET_NOT_IMPLEMENTED;
00199 }
00200
00201 int flgr2d_label_fgFLOAT32(FLGR_Data2D *imglabel, FLGR_Data2D *imgsrc, FLGR_Connexity connexity) {
00202 flgr_no_define_type_function(FLGR_FLOAT32);
00203 return FLGR_RET_NOT_IMPLEMENTED;
00204 }
00205
00206 int flgr2d_label_fgFLOAT64(FLGR_Data2D *imglabel, FLGR_Data2D *imgsrc, FLGR_Connexity connexity) {
00207 flgr_no_define_type_function(FLGR_FLOAT64);
00208 return FLGR_RET_NOT_IMPLEMENTED;
00209 }
00210
00211
00212
00214
00220
00221 int flgr2d_label(FLGR_Data2D *imglabel, FLGR_Data2D *imgsrc, FLGR_Connexity connexity) {
00222 int ret;
00223
00224
00225
00226 if((imglabel==NULL) || (imgsrc==NULL)){
00227 POST_ERROR("Null objects!\n");
00228 return FLGR_RET_NULL_OBJECT;
00229 }
00230
00231 if(imglabel->spp!=1) {
00232 POST_ERROR("Multi Sample Per Pixel not supported\n");
00233 return FLGR_RET_UNDEFINED_ERROR;
00234 }
00235
00236 if((ret=flgr2d_is_data_same_attributes(imglabel,imgsrc,__FUNCTION__))!=FLGR_RET_OK) return ret;
00237
00238 FLGR_DISPATCH_FUNCTION(FLGR_RET_TYPE_UNKNOWN,imglabel->type,flgr2d_label,imglabel,imgsrc,connexity);
00239
00240 }
00241
00242
00243
00244