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 #include <math.h>
00026 #include <flgrCoreIO.h>
00027 #include <flgrCoreDispatch.h>
00028 #include <flgrCoreMalloc.h>
00029 #include <flgrCoreVector.h>
00030 #include <flgrCoreArithVector.h>
00031 #include <flgrArith.h>
00032 #include <flgrMeasureBase.h>
00033 #include "flgrHistogram.h"
00034
00035
00036 #define FLGR_MACRO_NORMALIZE1D(dtype) \
00037 FLGR_Vector *min = flgr_vector_create(boundmin->spp,boundmin->type); \
00038 FLGR_Vector *max = flgr_vector_create(boundmin->spp,boundmin->type); \
00039 FLGR_Vector *tmp = flgr_vector_create(boundmin->spp,boundmin->type); \
00040 \
00041 \
00042 \
00043 flgr1d_measure_min_max_##dtype(datsrc,min,max); \
00044 \
00045 flgr1d_arith_const_sub_##dtype(datdest,datsrc,min); \
00046 \
00047 flgr_vector_sub(max,max,min); \
00048 flgr_vector_sub(tmp,boundmax,boundmin); \
00049 flgr_vector_div(max,tmp,max); \
00050 \
00051 flgr1d_arith_const_mult_##dtype(datdest,datsrc,max); \
00052 \
00053 flgr1d_arith_const_add_##dtype(datdest,datdest,boundmin); \
00054 \
00055 flgr_vector_destroy(min); \
00056 flgr_vector_destroy(max); \
00057 flgr_vector_destroy(tmp); \
00058 \
00059 return
00060
00061
00062 void flgr1d_histogram_normalize_fgBIT(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00063 flgr_no_define_type_function(FLGR_BIT);
00064 }
00065
00066 void flgr1d_histogram_normalize_fgUINT8(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00067 FLGR_MACRO_NORMALIZE1D(fgUINT8);
00068 }
00069
00070 void flgr1d_histogram_normalize_fgUINT16(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00071 FLGR_MACRO_NORMALIZE1D(fgUINT16);
00072 }
00073
00074 void flgr1d_histogram_normalize_fgUINT32(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00075 FLGR_MACRO_NORMALIZE1D(fgUINT32);
00076 }
00077
00078 void flgr1d_histogram_normalize_fgINT8(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00079 FLGR_MACRO_NORMALIZE1D(fgINT8);
00080 }
00081
00082 void flgr1d_histogram_normalize_fgINT16(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00083 FLGR_MACRO_NORMALIZE1D(fgINT16);
00084 }
00085
00086 void flgr1d_histogram_normalize_fgINT32(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00087 FLGR_MACRO_NORMALIZE1D(fgINT32);
00088 }
00089
00090 void flgr1d_histogram_normalize_fgFLOAT32(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00091 FLGR_MACRO_NORMALIZE1D(fgFLOAT32);
00092 }
00093
00094 void flgr1d_histogram_normalize_fgFLOAT64(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00095 FLGR_MACRO_NORMALIZE1D(fgFLOAT64);
00096 }
00097
00098 FLGR_Ret flgr1d_histogram_normalize(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, FLGR_Vector *boundmin, FLGR_Vector * boundmax) {
00099 FLGR_Ret ret;
00100
00101
00102
00103 if((datdest==NULL) || (datsrc==NULL)){
00104 POST_ERROR("Null objects!\n");
00105 return FLGR_RET_NULL_OBJECT;
00106 }
00107
00108 if((ret = flgr1d_is_data_same_attributes(datdest,datsrc,__FUNCTION__)) != FLGR_RET_OK) return ret;
00109
00110 FLGR_DISPATCH_PROCEDURE(datdest->type, flgr1d_histogram_normalize, datdest, datsrc, boundmin, boundmax);
00111
00112 return FLGR_RET_OK;
00113 }
00114
00115
00116
00117 FLGR_Ret flgr1d_histogram_normalize_str(FLGR_Data1D *datdest, FLGR_Data1D *datsrc, char *boundmin, char *boundmax) {
00118 int ret;
00119 FLGR_Vector *vector_min;
00120 FLGR_Vector *vector_max;
00121
00122
00123
00124 if((datsrc==NULL) || (datdest==NULL)) {
00125 POST_ERROR("Null objects!\n");
00126 return FLGR_RET_NULL_OBJECT;
00127 }
00128
00129 vector_min = flgr_vector_create(datsrc->spp,datsrc->type);
00130 vector_max = flgr_vector_create(datsrc->spp,datsrc->type);
00131
00132 if((ret=flgr_vector_import_string(vector_min, boundmin)) != FLGR_RET_OK) {
00133 POST_ERROR("String is not valid ""%s""\n",boundmin);
00134 flgr_vector_destroy(vector_min);
00135 return ret;
00136 }
00137
00138 if((ret=flgr_vector_import_string(vector_max, boundmax)) != FLGR_RET_OK) {
00139 POST_ERROR("String is not valid ""%s""\n",boundmax);
00140 flgr_vector_destroy(vector_max);
00141 return ret;
00142 }
00143
00144
00145 ret = flgr1d_histogram_normalize(datdest,datsrc,vector_min,vector_max);
00146
00147 flgr_vector_destroy(vector_min);
00148 flgr_vector_destroy(vector_max);
00149
00150 return ret;
00151
00152 }
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 #define FLGR_MACRO_NORMALIZE2D(dtype) \
00174 FLGR_Vector *min = flgr_vector_create(boundmin->spp,boundmin->type); \
00175 FLGR_Vector *max = flgr_vector_create(boundmin->spp,boundmin->type); \
00176 FLGR_Vector *tmp = flgr_vector_create(boundmin->spp,boundmin->type); \
00177 \
00178 \
00179 \
00180 flgr2d_measure_min_max_##dtype(datsrc,min,max); \
00181 \
00182 flgr2d_arith_const_sub_##dtype(datdest,datsrc,min); \
00183 \
00184 flgr_vector_sub(max,max,min); \
00185 flgr_vector_sub(tmp,boundmax,boundmin); \
00186 flgr_vector_div(max,tmp,max); \
00187 \
00188 flgr2d_arith_const_mult_##dtype(datdest,datsrc,max); \
00189 \
00190 flgr2d_arith_const_add_##dtype(datdest,datdest,boundmin); \
00191 \
00192 flgr_vector_destroy(min); \
00193 flgr_vector_destroy(max); \
00194 flgr_vector_destroy(tmp); \
00195 \
00196 return
00197
00198
00199 void flgr2d_histogram_normalize_fgBIT(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00200 flgr_no_define_type_function(FLGR_BIT);
00201 }
00202
00203 void flgr2d_histogram_normalize_fgUINT8(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00204 FLGR_MACRO_NORMALIZE2D(fgUINT8);
00205 }
00206
00207 void flgr2d_histogram_normalize_fgUINT16(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00208 FLGR_MACRO_NORMALIZE2D(fgUINT16);
00209 }
00210
00211 void flgr2d_histogram_normalize_fgUINT32(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00212 FLGR_MACRO_NORMALIZE2D(fgUINT32);
00213 }
00214
00215 void flgr2d_histogram_normalize_fgINT8(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00216 FLGR_MACRO_NORMALIZE2D(fgINT8);
00217 }
00218
00219 void flgr2d_histogram_normalize_fgINT16(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00220 FLGR_MACRO_NORMALIZE2D(fgINT16);
00221 }
00222
00223 void flgr2d_histogram_normalize_fgINT32(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00224 FLGR_MACRO_NORMALIZE2D(fgINT32);
00225 }
00226
00227 void flgr2d_histogram_normalize_fgFLOAT32(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00228 FLGR_MACRO_NORMALIZE2D(fgFLOAT32);
00229 }
00230
00231 void flgr2d_histogram_normalize_fgFLOAT64(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, FLGR_Vector *boundmin, FLGR_Vector *boundmax) {
00232 FLGR_MACRO_NORMALIZE2D(fgFLOAT64);
00233 }
00234
00235 FLGR_Ret flgr2d_histogram_normalize(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, FLGR_Vector *boundmin, FLGR_Vector * boundmax) {
00236 FLGR_Ret ret;
00237
00238
00239
00240 if((datdest==NULL) || (datsrc==NULL)){
00241 POST_ERROR("Null objects!\n");
00242 return FLGR_RET_NULL_OBJECT;
00243 }
00244
00245 if((ret = flgr2d_is_data_same_attributes(datdest,datsrc,__FUNCTION__)) != FLGR_RET_OK) return ret;
00246
00247 FLGR_DISPATCH_PROCEDURE(datdest->type, flgr2d_histogram_normalize, datdest, datsrc, boundmin, boundmax);
00248
00249 return FLGR_RET_OK;
00250 }
00251
00252
00253
00254 FLGR_Ret flgr2d_histogram_normalize_str(FLGR_Data2D *datdest, FLGR_Data2D *datsrc, char *boundmin, char *boundmax) {
00255 int ret;
00256 FLGR_Vector *vector_min;
00257 FLGR_Vector *vector_max;
00258
00259
00260
00261 if((datsrc==NULL) || (datdest==NULL)) {
00262 POST_ERROR("Null objects!\n");
00263 return FLGR_RET_NULL_OBJECT;
00264 }
00265
00266 vector_min = flgr_vector_create(datsrc->spp,datsrc->type);
00267 vector_max = flgr_vector_create(datsrc->spp,datsrc->type);
00268
00269 if((ret=flgr_vector_import_string(vector_min, boundmin)) != FLGR_RET_OK) {
00270 POST_ERROR("String is not valid ""%s""\n",boundmin);
00271 flgr_vector_destroy(vector_min);
00272 return ret;
00273 }
00274
00275 if((ret=flgr_vector_import_string(vector_max, boundmax)) != FLGR_RET_OK) {
00276 POST_ERROR("String is not valid ""%s""\n",boundmax);
00277 flgr_vector_destroy(vector_max);
00278 return ret;
00279 }
00280
00281
00282 ret = flgr2d_histogram_normalize(datdest,datsrc,vector_min,vector_max);
00283
00284 flgr_vector_destroy(vector_min);
00285 flgr_vector_destroy(vector_max);
00286
00287
00288 return ret;
00289
00290 }
00291
00292
00293
00294
00295 #define FLGR2D_MACRO_GET_HISTOGRAM(dtype,dtypeHist) \
00296 dtypeHist *phisto = (dtypeHist *) histogram->array; \
00297 dtypeHist tmp1; \
00298 dtypeHist tmp2; \
00299 dtype val; \
00300 dtype *psrc; \
00301 int i,j,k; \
00302 int spp = datsrc->spp; \
00303 int linelen = datsrc->size_x*spp; \
00304 \
00305 flgr1d_clear_all(histogram); \
00306 \
00307 for(i=0 ; i<datsrc->size_y ; i++) { \
00308 psrc = (dtype*) datsrc->array[i]; \
00309 \
00310 for(k=0 ; k<spp ; k++) { \
00311 \
00312 for(j=0 ; j<linelen ; j+=spp) { \
00313 val = flgr_get_array_##dtype(psrc, j+k); \
00314 tmp1 = ((dtypeHist) (val))*spp+k; \
00315 tmp2 = flgr_get_array_##dtypeHist(phisto,tmp1) + 1; \
00316 flgr_set_array_##dtypeHist(phisto,tmp1,tmp2); \
00317 } \
00318 \
00319 } \
00320 \
00321 } \
00322 \
00323 return
00324
00325
00326 void flgr2d_get_histogram_fgBIT(FLGR_Data1D *histogram, FLGR_Data2D *datsrc) {
00327 flgr_no_define_type_function(FLGR_BIT);
00328 }
00329 void flgr2d_get_histogram_fgUINT8(FLGR_Data1D *histogram, FLGR_Data2D *datsrc) {
00330 if(histogram->type == FLGR_UINT32) {
00331 FLGR2D_MACRO_GET_HISTOGRAM(fgUINT8,fgUINT32);
00332 }else if(histogram->type == FLGR_UINT64) {
00333 FLGR2D_MACRO_GET_HISTOGRAM(fgUINT8,fgUINT64);
00334 }else {
00335 POST_ERROR("Histogram type must be fgUINT32 or fgUINT64\n");
00336 }
00337 }
00338 void flgr2d_get_histogram_fgUINT16(FLGR_Data1D *histogram, FLGR_Data2D *datsrc) {
00339 if(histogram->type == FLGR_UINT32) {
00340 FLGR2D_MACRO_GET_HISTOGRAM(fgUINT16,fgUINT32);
00341 }else if(histogram->type == FLGR_UINT64) {
00342 FLGR2D_MACRO_GET_HISTOGRAM(fgUINT16,fgUINT64);
00343 }else {
00344 POST_ERROR("Histogram type must be fgUINT32 or fgUINT64\n");
00345 }
00346 }
00347 void flgr2d_get_histogram_fgUINT32(FLGR_Data1D *histogram, FLGR_Data2D *datsrc) {
00348 flgr_no_define_type_function(FLGR_UINT32);
00349 }
00350 void flgr2d_get_histogram_fgINT8(FLGR_Data1D *histogram, FLGR_Data2D *datsrc) {
00351 flgr_no_define_type_function(FLGR_INT8);
00352 }
00353 void flgr2d_get_histogram_fgINT16(FLGR_Data1D *histogram, FLGR_Data2D *datsrc) {
00354 flgr_no_define_type_function(FLGR_INT16);
00355 }
00356 void flgr2d_get_histogram_fgINT32(FLGR_Data1D *histogram, FLGR_Data2D *datsrc) {
00357 flgr_no_define_type_function(FLGR_INT32);
00358 }
00359 void flgr2d_get_histogram_fgFLOAT32(FLGR_Data1D *histogram, FLGR_Data2D *datsrc) {
00360 flgr_no_define_type_function(FLGR_FLOAT32);
00361 }
00362 void flgr2d_get_histogram_fgFLOAT64(FLGR_Data1D *histogram, FLGR_Data2D *datsrc) {
00363 flgr_no_define_type_function(FLGR_FLOAT64);
00364 }
00365
00366 FLGR_Ret flgr2d_get_histogram(FLGR_Data1D *histogram, FLGR_Data2D *datsrc) {
00367 int ret;
00368
00369
00370
00371 if((datsrc==NULL) || (histogram==NULL)) {
00372 POST_ERROR("Null objects!\n");
00373 return FLGR_RET_NULL_OBJECT;
00374 }
00375
00376 if(datsrc->spp != histogram->spp) {
00377 POST_ERROR("Histogram and image have different pixel vector sizes");
00378 return FLGR_RET_VECTOR_SIZE_DIFFERENT;
00379 }
00380
00381 if(datsrc->type == FLGR_UINT8) {
00382 if(histogram->length<256) {
00383 POST_ERROR("histogram length must be at least 256 for UINT8 images\n");
00384 return FLGR_RET_SIZE_ERROR;
00385 }
00386 }else if(datsrc->type == FLGR_UINT16) {
00387 if(histogram->length<65536) {
00388 POST_ERROR("histogram length must be at least 65536 for UINT16 images\n");
00389 return FLGR_RET_SIZE_ERROR;
00390 }
00391 }
00392
00393 FLGR_DISPATCH_PROCEDURE(datsrc->type, flgr2d_get_histogram, histogram,datsrc);
00394
00395 return ret;
00396
00397 }
00398
00399
00400
00401
00402
00403
00404 void flgr2d_get_otsu_threshold(FLGR_Vector *vecth, FLGR_Data2D *datsrc) {
00405
00406 }