00001 #define TEAGN_ENABLE_STDOUT_LOG
00002
00003 #include <TePDIExamplesBase.hpp>
00004
00005 #include <TePDIHistogram.hpp>
00006
00007 #include <TePDIUtils.hpp>
00008
00009 #include <TeAgnostic.h>
00010
00011 #include <TeProgress.h>
00012 #include <TeStdIOProgress.h>
00013
00014
00015
00016 #include <TeRaster.h>
00017 #include <TeDataTypes.h>
00018 #include <TeInitRasterDecoders.h>
00019
00020
00021 void display_histogram( TePDIHistogram& hist )
00022 {
00023 TePDIHistogram::iterator it;
00024 it = hist.begin();
00025
00026 while( it != hist.end() ) {
00027 TEAGN_LOGMSG( it->first );
00028 TEAGN_LOGMSG( it->second );
00029 ++it;
00030 }
00031 }
00032
00033 bool check_histogram_order( TePDIHistogram& hist )
00034 {
00035 TePDIHistogram::iterator it1;
00036 TePDIHistogram::iterator it2;
00037 it1 = hist.begin();
00038 it2 = it1;
00039 ++it2;
00040
00041 while( it2 != hist.end() ) {
00042 if( (*it1) > (*it2) ) {
00043 return false;
00044 }
00045 ++it1;
00046 ++it2;
00047 }
00048
00049 return true;
00050 }
00051
00052 void discrete_histogram_test_1()
00053 {
00054 TePDITypes::TePDIRasterPtrType raster;
00055 TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( raster, 1, 3, 1, false,
00056 TeUNSIGNEDCHAR, 0 ), "Unable to create raster" );
00057
00058 raster->setElement( 0, 0, 1 );
00059 raster->setElement( 0, 1, 2 );
00060 raster->setElement( 0, 2, 3 );
00061
00062 TePDIHistogram hist;
00063
00064 TePDIHistogram::iterator it;
00065
00066
00067
00068 TEAGN_TRUE_OR_THROW( hist.reset( raster, 0, 0, TeBoxPixelIn ),
00069 "Unable to create histogram" );
00070
00071 TEAGN_TRUE_OR_THROW( check_histogram_order( hist ),
00072 "Histogram order error" );
00073
00074
00075
00076 TEAGN_TRUE_OR_THROW( ( hist.size() == 3 ), "Invalid histogram size" );
00077
00078 TEAGN_CHECK_EPS( hist.getTotalCount(), 3, 0, "Invalid total count" );
00079
00080 TEAGN_TRUE_OR_THROW( hist.IsDiscrete(), "Discrete verification error" );
00081 TEAGN_TRUE_OR_THROW( hist.hasFixedStep(), "Step verification error" );
00082
00083 it = hist.begin();
00084
00085 TEAGN_CHECK_EQUAL( it->first, 1. , "" );
00086 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00087
00088 ++it;
00089
00090 TEAGN_CHECK_EQUAL( it->first, 2. , "" );
00091 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00092
00093 ++it;
00094
00095 TEAGN_CHECK_EQUAL( it->first, 3. , "" );
00096 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00097
00098 TEAGN_CHECK_EQUAL( hist.GetMinLevel(), 1., "" );
00099 TEAGN_CHECK_EQUAL( hist.GetMaxLevel(), 3., "" );
00100
00101 TEAGN_CHECK_EQUAL( hist.GetMinCount(), 1, "" );
00102 TEAGN_CHECK_EQUAL( hist.GetMaxCount(), 1, "" );
00103
00104 it = hist.begin();
00105 TePDIHistogram::iterator it_end = hist.end();
00106 unsigned int pixelsnmb = 0;
00107 while( it != it_end ) {
00108 pixelsnmb += it->second;
00109 ++it;
00110 }
00111 TEAGN_TRUE_OR_THROW( pixelsnmb == 3, "Invalid pixels number" );
00112 }
00113
00114 void interpolated_histogram_test_1()
00115 {
00116 TePDITypes::TePDIRasterPtrType raster;
00117 TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( raster, 1, 5, 1, false,
00118 TeDOUBLE, 0 ), "Unable to create raster" );
00119
00120 raster->setElement( 0, 0, 1 );
00121 raster->setElement( 0, 1, 1.9 );
00122 raster->setElement( 0, 2, 2 );
00123 raster->setElement( 0, 3, 2.1 );
00124 raster->setElement( 0, 4, 3 );
00125
00126 TePDIHistogram hist;
00127
00128 TePDIHistogram::iterator it;
00129
00130
00131
00132 TEAGN_TRUE_OR_THROW( hist.reset( raster, 0, 0, TeBoxPixelIn ),
00133 "Unable to create histogram" );
00134
00135 TEAGN_WATCH( hist.toString() );
00136
00137 TEAGN_TRUE_OR_THROW( check_histogram_order( hist ),
00138 "Histogram order error" );
00139
00140
00141
00142 TEAGN_TRUE_OR_THROW( ( hist.size() == 3 ), "Invalid histogram size" );
00143
00144 TEAGN_CHECK_EPS( hist.getTotalCount(), 5, 0, "Invalid total count" );
00145
00146
00147
00148
00149 TEAGN_TRUE_OR_THROW( hist.IsDiscrete(),
00150 "Discrete verification error" );
00151
00152 TEAGN_TRUE_OR_THROW( hist.hasFixedStep(), "Step verification error" );
00153
00154 it = hist.begin();
00155
00156 TEAGN_CHECK_EQUAL( it->first, 1. , "" );
00157 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00158
00159 ++it;
00160
00161 TEAGN_CHECK_EQUAL( it->first, 2. , "" );
00162 TEAGN_CHECK_EQUAL( it->second, 3 , "" );
00163
00164 ++it;
00165
00166 TEAGN_CHECK_EQUAL( it->first, 3. , "" );
00167 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00168
00169 TEAGN_CHECK_EQUAL( hist.GetMinLevel(), 1., "" );
00170 TEAGN_CHECK_EQUAL( hist.GetMaxLevel(), 3., "" );
00171
00172 TEAGN_CHECK_EQUAL( hist.GetMinCount(), 1, "" );
00173 TEAGN_CHECK_EQUAL( hist.GetMaxCount(), 3, "" );
00174
00175 it = hist.begin();
00176 TePDIHistogram::iterator it_end = hist.end();
00177 unsigned int pixelsnmb = 0;
00178 while( it != it_end ) {
00179 pixelsnmb += it->second;
00180 ++it;
00181 }
00182 TEAGN_TRUE_OR_THROW( pixelsnmb == 5, "Invalid pixels number" );
00183 }
00184
00185
00186 void discrete_histogram_test_2()
00187 {
00188 TePDITypes::TePDIRasterPtrType raster;
00189 TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( raster, 1, 3, 1, false,
00190 TeUNSIGNEDCHAR, 0 ), "Unable to create raster" );
00191
00192 raster->setElement( 0, 0, 1 );
00193 raster->setElement( 0, 1, 2 );
00194 raster->setElement( 0, 2, 3 );
00195
00196 TePDIHistogram hist;
00197
00198 TePDIHistogram::iterator it;
00199
00200
00201
00202 TEAGN_TRUE_OR_THROW( hist.reset( raster, 0, 0, TeBoxPixelIn ),
00203 "Unable to create histogram" );
00204
00205 TEAGN_WATCH( hist.toString() );
00206
00207 TEAGN_TRUE_OR_THROW( check_histogram_order( hist ),
00208 "Histogram order error" );
00209
00210 TEAGN_TRUE_OR_THROW( ( hist.size() == 3 ), "Invalid histogram size" );
00211
00212 TEAGN_CHECK_EPS( hist.getTotalCount(), 3, 0, "Invalid total count" );
00213
00214 TEAGN_TRUE_OR_THROW( hist.IsDiscrete(), "Discrete verification error" );
00215 TEAGN_TRUE_OR_THROW( hist.hasFixedStep(), "Step verification error" );
00216
00217 it = hist.begin();
00218
00219 TEAGN_CHECK_EQUAL( it->first, 1. , "" );
00220 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00221
00222 ++it;
00223
00224 TEAGN_CHECK_EQUAL( it->first, 2. , "" );
00225 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00226
00227 ++it;
00228
00229 TEAGN_CHECK_EQUAL( it->first, 3. , "" );
00230 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00231
00232 TEAGN_CHECK_EQUAL( hist.GetMinLevel(), 1., "" );
00233 TEAGN_CHECK_EQUAL( hist.GetMaxLevel(), 3., "" );
00234
00235 TEAGN_CHECK_EQUAL( hist.GetMinCount(), 1, "" );
00236 TEAGN_CHECK_EQUAL( hist.GetMaxCount(), 1, "" );
00237
00238 it = hist.begin();
00239 TePDIHistogram::iterator it_end = hist.end();
00240 unsigned int pixelsnmb = 0;
00241 while( it != it_end ) {
00242 pixelsnmb += it->second;
00243 ++it;
00244 }
00245 TEAGN_TRUE_OR_THROW( pixelsnmb == 3, "Invalid pixels number" );
00246 }
00247
00248
00249 void discrete_histogram_test_16bits()
00250 {
00251 TePDITypes::TePDIRasterPtrType raster;
00252 TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( raster, 1, 3, 1, false,
00253 TeUNSIGNEDSHORT, 0 ), "Unable to create raster" );
00254
00255 raster->setElement( 0, 0, 1 );
00256 raster->setElement( 0, 1, 2 );
00257 raster->setElement( 0, 2, 3 );
00258
00259 TePDIHistogram hist;
00260
00261 TePDIHistogram::iterator it;
00262
00263
00264
00265 TEAGN_TRUE_OR_THROW( hist.reset( raster, 0, 0, TeBoxPixelIn ),
00266 "Unable to create histogram" );
00267
00268 TEAGN_TRUE_OR_THROW( check_histogram_order( hist ),
00269 "Histogram order error" );
00270
00271
00272
00273 TEAGN_TRUE_OR_THROW( ( hist.size() == 3 ), "Invalid histogram size" );
00274
00275 TEAGN_CHECK_EPS( hist.getTotalCount(), 3, 0, "Invalid total count" );
00276
00277 TEAGN_TRUE_OR_THROW( hist.IsDiscrete(), "Discrete verification error" );
00278 TEAGN_TRUE_OR_THROW( hist.hasFixedStep(), "Step verification error" );
00279
00280 it = hist.begin();
00281
00282 TEAGN_CHECK_EQUAL( it->first, 1. , "" );
00283 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00284
00285 ++it;
00286
00287 TEAGN_CHECK_EQUAL( it->first, 2. , "" );
00288 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00289
00290 ++it;
00291
00292 TEAGN_CHECK_EQUAL( it->first, 3. , "" );
00293 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00294
00295 TEAGN_CHECK_EQUAL( hist.GetMinLevel(), 1., "" );
00296 TEAGN_CHECK_EQUAL( hist.GetMaxLevel(), 3., "" );
00297
00298 TEAGN_CHECK_EQUAL( hist.GetMinCount(), 1, "" );
00299 TEAGN_CHECK_EQUAL( hist.GetMaxCount(), 1, "" );
00300
00301 it = hist.begin();
00302 TePDIHistogram::iterator it_end = hist.end();
00303 unsigned int pixelsnmb = 0;
00304 while( it != it_end ) {
00305 pixelsnmb += it->second;
00306 ++it;
00307 }
00308 TEAGN_TRUE_OR_THROW( pixelsnmb == 3, "Invalid pixels number" );
00309 }
00310
00311
00312 void normal_levels_test2()
00313 {
00314 TePDITypes::TePDIRasterPtrType raster;
00315 TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( raster, 1, 1, 2, false,
00316 TeUNSIGNEDCHAR, 0 ), "Unable to create raster" );
00317
00318 raster->setElement( 0, 0, 1 );
00319 raster->setElement( 1, 0, 2 );
00320
00321 TePDIHistogram hist;
00322
00323 TePDIHistogram::iterator it;
00324
00325
00326
00327 TEAGN_TRUE_OR_THROW( hist.reset( raster, 0, 2, TeBoxPixelIn ),
00328 "Unable to create histogram" );
00329
00330 TEAGN_TRUE_OR_THROW( check_histogram_order( hist ),
00331 "Histogram order error" );
00332
00333
00334
00335 TEAGN_TRUE_OR_THROW( ( hist.size() == 2 ), "Invalid histogram size" );
00336
00337 TEAGN_CHECK_EPS( hist.getTotalCount(), 2, 0, "Invalid total count" );
00338
00339 TEAGN_TRUE_OR_THROW( hist.hasFixedStep(), "Step verification error" );
00340
00341 it = hist.begin();
00342
00343 TEAGN_CHECK_EPS( it->first, 1., 0., "" );
00344 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00345
00346 ++it;
00347
00348 TEAGN_CHECK_EPS( it->first, 2., 0., "" );
00349 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00350
00351 TEAGN_CHECK_EPS( hist.GetMinLevel(), 1., 0, "" );
00352 TEAGN_CHECK_EPS( hist.GetMaxLevel(), 2., 0, "" );
00353
00354 TEAGN_CHECK_EPS( hist.GetMinCount(), 1., 0, "" );
00355 TEAGN_CHECK_EPS( hist.GetMaxCount(), 1., 0, "" );
00356
00357 it = hist.begin();
00358 TePDIHistogram::iterator it_end = hist.end();
00359 unsigned int pixelsnmb = 0;
00360 while( it != it_end ) {
00361 pixelsnmb += it->second;
00362 ++it;
00363 }
00364 TEAGN_TRUE_OR_THROW( pixelsnmb == 2, "Invalid pixels number" );
00365 }
00366
00367
00368 void normal_levels_test3()
00369 {
00370 TePDITypes::TePDIRasterPtrType raster;
00371 TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( raster, 1, 1, 3, false,
00372 TeUNSIGNEDCHAR, 0 ), "Unable to create raster" );
00373
00374 raster->setElement( 0, 0, 1 );
00375 raster->setElement( 1, 0, 2 );
00376 raster->setElement( 2, 0, 3 );
00377
00378 TePDIHistogram hist;
00379
00380 TePDIHistogram::iterator it;
00381
00382
00383
00384 TEAGN_TRUE_OR_THROW( hist.reset( raster, 0, 3, TeBoxPixelIn ),
00385 "Unable to create histogram" );
00386
00387 TEAGN_TRUE_OR_THROW( check_histogram_order( hist ),
00388 "Histogram order error" );
00389
00390
00391
00392 TEAGN_TRUE_OR_THROW( ( hist.size() == 3 ), "Invalid histogram size" );
00393
00394 TEAGN_CHECK_EPS( hist.getTotalCount(), 3, 0, "Invalid total count" );
00395
00396 TEAGN_TRUE_OR_THROW( hist.hasFixedStep(), "Step verification error" );
00397
00398 it = hist.begin();
00399
00400 TEAGN_CHECK_EPS( it->first, 1., 0., "" );
00401 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00402
00403 ++it;
00404
00405 TEAGN_CHECK_EPS( it->first, 2., 0., "" );
00406 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00407
00408 ++it;
00409
00410 TEAGN_CHECK_EPS( it->first, 3., 0., "" );
00411 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00412
00413
00414 TEAGN_CHECK_EPS( hist.GetMinLevel(), 1., 0, "" );
00415 TEAGN_CHECK_EPS( hist.GetMaxLevel(), 3., 0, "" );
00416
00417 TEAGN_CHECK_EPS( hist.GetMinCount(), 1., 0, "" );
00418 TEAGN_CHECK_EPS( hist.GetMaxCount(), 1., 0, "" );
00419
00420 it = hist.begin();
00421 TePDIHistogram::iterator it_end = hist.end();
00422 unsigned int pixelsnmb = 0;
00423 while( it != it_end ) {
00424 pixelsnmb += it->second;
00425 ++it;
00426 }
00427 TEAGN_TRUE_OR_THROW( pixelsnmb == 3, "Invalid pixels number" );
00428 }
00429
00430
00431 void normal_levels_test4()
00432 {
00433 TePDITypes::TePDIRasterPtrType raster;
00434 TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( raster, 1, 2, 2, false,
00435 TeUNSIGNEDCHAR, 0 ), "Unable to create raster" );
00436
00437 raster->setElement( 0, 0, 1 );
00438 raster->setElement( 1, 0, 2 );
00439 raster->setElement( 0, 1, 3 );
00440 raster->setElement( 1, 1, 4 );
00441
00442 TePDIHistogram hist;
00443
00444 TePDIHistogram::iterator it;
00445
00446
00447
00448 TEAGN_TRUE_OR_THROW( hist.reset( raster, 0, 4, TeBoxPixelIn ),
00449 "Unable to create histogram" );
00450
00451 TEAGN_TRUE_OR_THROW( check_histogram_order( hist ),
00452 "Histogram order error" );
00453
00454
00455
00456 TEAGN_TRUE_OR_THROW( ( hist.size() == 4 ), "Invalid histogram size" );
00457
00458 TEAGN_CHECK_EPS( hist.getTotalCount(), 4, 0, "Invalid total count" );
00459
00460 TEAGN_TRUE_OR_THROW( hist.hasFixedStep(), "Step verification error" );
00461
00462 it = hist.begin();
00463
00464 TEAGN_CHECK_EPS( it->first, 1., 0., "" );
00465 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00466
00467 ++it;
00468
00469 TEAGN_CHECK_EPS( it->first, 2., 0., "" );
00470 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00471
00472 ++it;
00473
00474 TEAGN_CHECK_EPS( it->first, 3., 0., "" );
00475 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00476
00477 ++it;
00478
00479 TEAGN_CHECK_EPS( it->first, 4., 0., "" );
00480 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00481
00482 TEAGN_CHECK_EPS( hist.GetMinLevel(), 1., 0, "" );
00483 TEAGN_CHECK_EPS( hist.GetMaxLevel(), 4., 0, "" );
00484
00485 TEAGN_CHECK_EPS( hist.GetMinCount(), 1., 0, "" );
00486 TEAGN_CHECK_EPS( hist.GetMaxCount(), 1., 0, "" );
00487
00488 it = hist.begin();
00489 TePDIHistogram::iterator it_end = hist.end();
00490 unsigned int pixelsnmb = 0;
00491 while( it != it_end ) {
00492 pixelsnmb += it->second;
00493 ++it;
00494 }
00495 TEAGN_TRUE_OR_THROW( pixelsnmb == 4, "Invalid pixels number" );
00496 }
00497
00498
00499 void auto_levels_test()
00500 {
00501 TePDITypes::TePDIRasterPtrType raster;
00502 TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( raster, 1, 2, 2, false,
00503 TeUNSIGNEDCHAR, 0 ), "Unable to create raster" );
00504
00505 raster->setElement( 0, 0, 1 );
00506 raster->setElement( 1, 0, 2 );
00507 raster->setElement( 0, 1, 3 );
00508 raster->setElement( 1, 1, 4 );
00509
00510 TePDIHistogram hist;
00511
00512 TePDIHistogram::iterator it;
00513
00514 TEAGN_TRUE_OR_THROW( hist.reset( raster, 0, 0, TeBoxPixelIn ),
00515 "Unable to create histogram" );
00516
00517 TEAGN_TRUE_OR_THROW( check_histogram_order( hist ),
00518 "Histogram order error" );
00519
00520 TEAGN_TRUE_OR_THROW( ( hist.size() == 4 ), "Invalid histogram size" );
00521
00522 TEAGN_CHECK_EPS( hist.getTotalCount(), 4, 0, "Invalid total count" );
00523
00524 TEAGN_TRUE_OR_THROW( hist.hasFixedStep(), "Step verification error" );
00525
00526 it = hist.begin();
00527
00528 TEAGN_CHECK_EPS( it->first, 1., 0., "" );
00529 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00530
00531 ++it;
00532
00533 TEAGN_CHECK_EPS( it->first, 2., 0., "" );
00534 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00535
00536 ++it;
00537
00538 TEAGN_CHECK_EPS( it->first, 3., 0., "" );
00539 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00540
00541 ++it;
00542
00543 TEAGN_CHECK_EPS( it->first, 4., 0., "" );
00544 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00545
00546 TEAGN_CHECK_EPS( hist.GetMinLevel(), 1, 0, "" );
00547 TEAGN_CHECK_EPS( hist.GetMaxLevel(), 4., 0, "" );
00548
00549 TEAGN_CHECK_EPS( hist.GetMinCount(), 1., 0, "" );
00550 TEAGN_CHECK_EPS( hist.GetMaxCount(), 1., 0, "" );
00551
00552 it = hist.begin();
00553 TePDIHistogram::iterator it_end = hist.end();
00554 unsigned int pixelsnmb = 0;
00555 while( it != it_end ) {
00556 pixelsnmb += it->second;
00557 ++it;
00558 }
00559 TEAGN_TRUE_OR_THROW( pixelsnmb == 4, "Invalid pixels number" );
00560 }
00561
00562
00563 void discretize_test()
00564 {
00565 TePDITypes::TePDIRasterPtrType raster;
00566 TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( raster, 1, 2, 2, false,
00567 TeUNSIGNEDCHAR, 0 ), "Unable to create raster" );
00568
00569 raster->setElement( 0, 0, 1 );
00570 raster->setElement( 1, 0, 2 );
00571 raster->setElement( 0, 1, 3 );
00572 raster->setElement( 1, 1, 4 );
00573
00574 TePDIHistogram hist;
00575
00576 TePDIHistogram::iterator it;
00577
00578 TEAGN_TRUE_OR_THROW( hist.reset( raster, 0, 6, TeBoxPixelIn ),
00579 "Unable to create histogram" );
00580
00581 TEAGN_WATCH( hist.toString() )
00582
00583 TEAGN_TRUE_OR_THROW( check_histogram_order( hist ),
00584 "Histogram order error" );
00585
00586 TEAGN_TRUE_OR_THROW( ( hist.size() == 4 ), "Invalid histogram size" );
00587
00588 TEAGN_CHECK_EPS( hist.getTotalCount(), 4, 0, "Invalid total count" );
00589
00590 TEAGN_TRUE_OR_THROW( ( ! hist.hasFixedStep() ),
00591 "Step verification error" );
00592
00593
00594
00595 it = hist.begin();
00596
00597 TEAGN_CHECK_EPS( it->first, 1., 0., "" );
00598 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00599
00600 ++it;
00601
00602 TEAGN_CHECK_EPS( it->first, 2.20, 0., "" );
00603 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00604
00605 ++it;
00606
00607 TEAGN_CHECK_EPS( it->first, 2.8, 0.0000001, "" );
00608 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00609
00610 ++it;
00611
00612 TEAGN_CHECK_EPS( it->first, 4, 0.0000001, "" );
00613 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00614
00615
00616 TEAGN_CHECK_EPS( hist.GetMinLevel(), 1., 0, "" );
00617 TEAGN_CHECK_EPS( hist.GetMaxLevel(), 4., 0, "" );
00618
00619 TEAGN_CHECK_EPS( hist.GetMinCount(), 1., 0, "" );
00620 TEAGN_CHECK_EPS( hist.GetMaxCount(), 1., 0, "" );
00621
00622 it = hist.begin();
00623 TePDIHistogram::iterator it_end = hist.end();
00624 unsigned int pixelsnmb = 0;
00625 while( it != it_end ) {
00626 pixelsnmb += it->second;
00627 ++it;
00628 }
00629 TEAGN_TRUE_OR_THROW( pixelsnmb == 4, "Invalid pixels number" );
00630
00631
00632 TEAGN_TRUE_OR_THROW( ! hist.IsDiscrete(),
00633 "Discretize verification error" );
00634 TEAGN_TRUE_OR_THROW( hist.Discretize(), "Unable to discretize" );
00635 TEAGN_TRUE_OR_THROW( hist.IsDiscrete(), "Discretize verification error" );
00636 TEAGN_WATCH( hist.toString() )
00637
00638 TEAGN_TRUE_OR_THROW( check_histogram_order( hist ),
00639 "Histogram order error" );
00640 TEAGN_TRUE_OR_THROW( hist.hasFixedStep(), "Step verification error" );
00641
00642
00643
00644 TEAGN_TRUE_OR_THROW( ( hist.size() == 4 ), "Invalid histogram size" );
00645
00646 TEAGN_CHECK_EPS( hist.getTotalCount(), 4, 0, "Invalid total count" );
00647
00648 it = hist.begin();
00649
00650 TEAGN_CHECK_EPS( it->first, 1., 0., "" );
00651 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00652
00653 ++it;
00654
00655 TEAGN_CHECK_EPS( it->first, 2., 0., "" );
00656 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00657
00658 ++it;
00659
00660 TEAGN_CHECK_EPS( it->first, 3., 0., "" );
00661 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00662
00663 ++it;
00664
00665 TEAGN_CHECK_EPS( it->first, 4., 0., "" );
00666 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00667
00668 TEAGN_CHECK_EPS( hist.GetMinLevel(), 1., 0, "" );
00669 TEAGN_CHECK_EPS( hist.GetMaxLevel(), 4., 0, "" );
00670
00671 TEAGN_CHECK_EPS( hist.GetMinCount(), 1., 0, "" );
00672 TEAGN_CHECK_EPS( hist.GetMaxCount(), 1., 0, "" );
00673
00674 it = hist.begin();
00675 it_end = hist.end();
00676 pixelsnmb = 0;
00677 while( it != it_end ) {
00678 pixelsnmb += it->second;
00679 ++it;
00680 }
00681 TEAGN_TRUE_OR_THROW( pixelsnmb == 4, "Invalid pixels number" );
00682 }
00683
00684
00685 void performance_test()
00686 {
00687 TePDITypes::TePDIRasterPtrType inRaster( new TeRaster(
00688 std::string( TEPDIEXAMPLESRESPATH "cbers_b2_crop.tif" ),
00689 'r' ) );
00690 TEAGN_TRUE_OR_THROW( inRaster->init(), "Unable to init inRaster" );
00691
00692 TePDIHistogram hist;
00693
00694 TEAGN_TRUE_OR_THROW( hist.reset( inRaster, 0, 0, TeBoxPixelIn ),
00695 "Unable to create histogram" );
00696
00697 TEAGN_WATCH( hist.toString() )
00698
00699 TEAGN_TRUE_OR_THROW( check_histogram_order( hist ),
00700 "Histogram order error" );
00701
00702 TEAGN_CHECK_EPS( hist.size(), 180, 0, "Invalid histogram size" );
00703
00704 TEAGN_CHECK_EPS( hist.getTotalCount(), 720657, 0, "Invalid total count" );
00705 }
00706
00707
00708 void operator_equal_from_map_test()
00709 {
00710 std::map< double, unsigned int > simple_map1;
00711 simple_map1[ 0.0 ] = 1;
00712 simple_map1[ 1.0 ] = 2;
00713 simple_map1[ 2.0 ] = 3;
00714 simple_map1[ 3.0 ] = 4;
00715
00716 TePDIHistogram hist1;
00717 hist1 = simple_map1;
00718
00719 TEAGN_TRUE_OR_THROW( ( hist1.size() == 4 ), "Invalid histogram size" );
00720
00721 TEAGN_CHECK_EPS( hist1.getTotalCount(), 10, 0, "Invalid total count" );
00722
00723 TEAGN_TRUE_OR_THROW( check_histogram_order( hist1 ),
00724 "Histogram order error" );
00725 TEAGN_CHECK_EPS( hist1.GetMinLevel(), 0, 0, "" );
00726 TEAGN_CHECK_EPS( hist1.GetMaxLevel(), 3, 0, "" );
00727 TEAGN_CHECK_EPS( hist1.GetMinCount(), 1, 0, "" );
00728 TEAGN_CHECK_EPS( hist1.GetMaxCount(), 4, 0, "" );
00729 TEAGN_TRUE_OR_THROW( hist1.hasFixedStep(), "Step verification error" );
00730
00731 {
00732 TePDIHistogram::iterator it = hist1.begin();
00733
00734 TEAGN_CHECK_EPS( it->first, 0., 0., "" );
00735 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00736
00737 ++it;
00738
00739 TEAGN_CHECK_EPS( it->first, 1., 0., "" );
00740 TEAGN_CHECK_EPS( it->second, 2., 0., "" );
00741
00742 ++it;
00743
00744 TEAGN_CHECK_EPS( it->first, 2., 0., "" );
00745 TEAGN_CHECK_EPS( it->second, 3., 0., "" );
00746
00747 ++it;
00748
00749 TEAGN_CHECK_EPS( it->first, 3., 0., "" );
00750 TEAGN_CHECK_EPS( it->second, 4., 0., "" );
00751 }
00752
00753
00754
00755 std::map< double, unsigned int > simple_map2;
00756 simple_map2[ 0.0 ] = 1;
00757 simple_map2[ 1.1 ] = 2;
00758 simple_map2[ 2.0 ] = 3;
00759 simple_map2[ 3.0 ] = 4;
00760
00761 TePDIHistogram hist2;
00762 hist2 = simple_map2;
00763
00764
00765
00766 TEAGN_TRUE_OR_THROW( ( hist2.size() == 4 ), "Invalid histogram size" );
00767
00768 TEAGN_CHECK_EPS( hist2.getTotalCount(), 10, 0, "Invalid total count" );
00769
00770 TEAGN_TRUE_OR_THROW( check_histogram_order( hist2 ),
00771 "Histogram order error" );
00772 TEAGN_CHECK_EPS( hist2.GetMinLevel(), 0, 0, "" );
00773 TEAGN_CHECK_EPS( hist2.GetMaxLevel(), 3, 0, "" );
00774 TEAGN_CHECK_EPS( hist2.GetMinCount(), 1, 0, "" );
00775 TEAGN_CHECK_EPS( hist2.GetMaxCount(), 4, 0, "" );
00776 TEAGN_TRUE_OR_THROW( ( hist2.hasFixedStep() ), "Step verification error" );
00777
00778 {
00779 TePDIHistogram::iterator it = hist2.begin();
00780
00781 TEAGN_CHECK_EPS( it->first, 0., 0., "" );
00782 TEAGN_CHECK_EPS( it->second, 1., 0., "" );
00783
00784 ++it;
00785
00786 TEAGN_CHECK_EPS( it->first, 1., 0., "" );
00787 TEAGN_CHECK_EPS( it->second, 2., 0., "" );
00788
00789 ++it;
00790
00791 TEAGN_CHECK_EPS( it->first, 2., 0., "" );
00792 TEAGN_CHECK_EPS( it->second, 3., 0., "" );
00793
00794 ++it;
00795
00796 TEAGN_CHECK_EPS( it->first, 3., 0., "" );
00797 TEAGN_CHECK_EPS( it->second, 4., 0., "" );
00798 }
00799 }
00800
00801
00802 void histogram_from_TeCHAR_test()
00803 {
00804 TePDITypes::TePDIRasterPtrType raster;
00805 TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( raster, 1, 3, 1, false,
00806 TeCHAR, 0 ), "Unable to create raster" );
00807
00808 raster->setElement( 0, 0, -1 );
00809 raster->setElement( 0, 1, 0 );
00810 raster->setElement( 0, 2, 1 );
00811
00812 TePDIHistogram hist;
00813
00814 TePDIHistogram::iterator it;
00815
00816
00817
00818 TEAGN_TRUE_OR_THROW( hist.reset( raster, 0, 0, TeBoxPixelIn ),
00819 "Unable to create histogram" );
00820
00821 TEAGN_TRUE_OR_THROW( check_histogram_order( hist ),
00822 "Histogram order error" );
00823
00824
00825
00826 TEAGN_TRUE_OR_THROW( ( hist.size() == 3 ), "Invalid histogram size" );
00827
00828 TEAGN_CHECK_EPS( hist.getTotalCount(), 3, 0, "Invalid total count" );
00829
00830 it = hist.begin();
00831
00832 TEAGN_CHECK_EQUAL( it->first, -1. , "" );
00833 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00834
00835 ++it;
00836
00837 TEAGN_CHECK_EQUAL( it->first, 0. , "" );
00838 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00839
00840 ++it;
00841
00842 TEAGN_CHECK_EQUAL( it->first, 1. , "" );
00843 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00844
00845 TEAGN_TRUE_OR_THROW( hist.IsDiscrete(), "Discrete verification error" );
00846 TEAGN_TRUE_OR_THROW( hist.hasFixedStep(), "Step verification error" );
00847
00848 TEAGN_CHECK_EQUAL( hist.GetMinLevel(), -1., "" );
00849 TEAGN_CHECK_EQUAL( hist.GetMaxLevel(), 1., "" );
00850
00851 TEAGN_CHECK_EQUAL( hist.GetMinCount(), 1, "" );
00852 TEAGN_CHECK_EQUAL( hist.GetMaxCount(), 1, "" );
00853
00854 it = hist.begin();
00855 TePDIHistogram::iterator it_end = hist.end();
00856 unsigned int pixelsnmb = 0;
00857 while( it != it_end ) {
00858 pixelsnmb += it->second;
00859 ++it;
00860 }
00861 TEAGN_TRUE_OR_THROW( pixelsnmb == 3, "Invalid pixels number" );
00862 }
00863
00864
00865 void histogram_from_TeSHORT_test()
00866 {
00867 TePDITypes::TePDIRasterPtrType raster;
00868 TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( raster, 1, 3, 1, false,
00869 TeSHORT, 0 ), "Unable to create raster" );
00870
00871 raster->setElement( 0, 0, -1 );
00872 raster->setElement( 0, 1, 0 );
00873 raster->setElement( 0, 2, 1 );
00874
00875 TePDIHistogram hist;
00876
00877 TePDIHistogram::iterator it;
00878
00879
00880
00881 TEAGN_TRUE_OR_THROW( hist.reset( raster, 0, 0, TeBoxPixelIn ),
00882 "Unable to create histogram" );
00883
00884 TEAGN_TRUE_OR_THROW( check_histogram_order( hist ),
00885 "Histogram order error" );
00886
00887
00888
00889 TEAGN_TRUE_OR_THROW( ( hist.size() == 3 ), "Invalid histogram size" );
00890
00891 TEAGN_CHECK_EPS( hist.getTotalCount(), 3, 0, "Invalid total count" );
00892
00893 it = hist.begin();
00894
00895 TEAGN_CHECK_EQUAL( it->first, -1. , "" );
00896 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00897
00898 ++it;
00899
00900 TEAGN_CHECK_EQUAL( it->first, 0. , "" );
00901 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00902
00903 ++it;
00904
00905 TEAGN_CHECK_EQUAL( it->first, 1. , "" );
00906 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00907
00908 TEAGN_TRUE_OR_THROW( hist.IsDiscrete(), "Discrete verification error" );
00909 TEAGN_TRUE_OR_THROW( hist.hasFixedStep(), "Step verification error" );
00910
00911 TEAGN_CHECK_EQUAL( hist.GetMinLevel(), -1., "" );
00912 TEAGN_CHECK_EQUAL( hist.GetMaxLevel(), 1., "" );
00913
00914 TEAGN_CHECK_EQUAL( hist.GetMinCount(), 1, "" );
00915 TEAGN_CHECK_EQUAL( hist.GetMaxCount(), 1, "" );
00916
00917 it = hist.begin();
00918 TePDIHistogram::iterator it_end = hist.end();
00919 unsigned int pixelsnmb = 0;
00920 while( it != it_end ) {
00921 pixelsnmb += it->second;
00922 ++it;
00923 }
00924 TEAGN_TRUE_OR_THROW( pixelsnmb == 3, "Invalid pixels number" );
00925 }
00926
00927
00928 void discrete_histogram_with_external_polygonset_test()
00929 {
00930 TePDITypes::TePDIRasterPtrType raster;
00931 TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( raster, 1, 3, 1, false,
00932 TeUNSIGNEDCHAR, 0 ), "Unable to create raster" );
00933
00934 raster->setElement( 0, 0, 1 );
00935 raster->setElement( 0, 1, 2 );
00936 raster->setElement( 0, 2, 3 );
00937
00938 TePDIHistogram hist;
00939
00940 TePDIHistogram::iterator it;
00941
00942
00943
00944 TeSharedPtr< TePolygonSet > polsetptr( new TePolygonSet );
00945 TeBox rasterbox( raster->params().boundingBox() );
00946 polsetptr->add( polygonFromBox( rasterbox ) );
00947
00948
00949
00950 TEAGN_TRUE_OR_THROW( hist.reset( raster, 0, 0,
00951 TeBoxPixelIn, polsetptr ),
00952 "Unable to create histogram" );
00953
00954 TEAGN_TRUE_OR_THROW( check_histogram_order( hist ),
00955 "Histogram order error" );
00956
00957
00958
00959 TEAGN_TRUE_OR_THROW( ( hist.size() == 3 ), "Invalid histogram size" );
00960
00961 TEAGN_CHECK_EPS( hist.getTotalCount(), 3, 0, "Invalid total count" );
00962
00963 TEAGN_TRUE_OR_THROW( hist.IsDiscrete(), "Discrete verification error" );
00964 TEAGN_TRUE_OR_THROW( hist.hasFixedStep(), "Step verification error" );
00965
00966 it = hist.begin();
00967
00968 TEAGN_CHECK_EQUAL( it->first, 1. , "" );
00969 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00970
00971 ++it;
00972
00973 TEAGN_CHECK_EQUAL( it->first, 2. , "" );
00974 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00975
00976 ++it;
00977
00978 TEAGN_CHECK_EQUAL( it->first, 3. , "" );
00979 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
00980
00981 TEAGN_CHECK_EQUAL( hist.GetMinLevel(), 1., "" );
00982 TEAGN_CHECK_EQUAL( hist.GetMaxLevel(), 3., "" );
00983
00984 TEAGN_CHECK_EQUAL( hist.GetMinCount(), 1, "" );
00985 TEAGN_CHECK_EQUAL( hist.GetMaxCount(), 1, "" );
00986
00987 it = hist.begin();
00988 TePDIHistogram::iterator it_end = hist.end();
00989 unsigned int pixelsnmb = 0;
00990 while( it != it_end ) {
00991 pixelsnmb += it->second;
00992 ++it;
00993 }
00994 TEAGN_TRUE_OR_THROW( pixelsnmb == 3, "Invalid pixels number" );
00995 }
00996
00997 void palette_based_histogram_test()
00998 {
00999
01000
01001 TeRasterParams params;
01002 params.setNLinesNColumns( 3, 1 );
01003 params.nBands( 1 );
01004 params.setDataType( TeUNSIGNEDCHAR );
01005 params.setPhotometric( TeRasterParams::TePallete, -1 );
01006 params.lutr_.push_back( 1 );
01007 params.lutr_.push_back( 2 );
01008 params.lutr_.push_back( 4 );
01009
01010 TePDITypes::TePDIRasterPtrType raster;
01011 TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( raster, params,
01012 TePDIUtils::TePDIUtilsAutoMemPol ), "Unable to create raster" );
01013
01014 TEAGN_TRUE_OR_THROW( raster->setElement( 0, 0, 0 ), "setElement error" );
01015 TEAGN_TRUE_OR_THROW( raster->setElement( 0, 1, 1 ), "setElement error" );
01016 TEAGN_TRUE_OR_THROW( raster->setElement( 0, 2, 2 ), "setElement error" );
01017
01018 TePDIHistogram hist;
01019
01020 TePDIHistogram::iterator it;
01021
01022
01023
01024 TEAGN_TRUE_OR_THROW( hist.reset( raster, 0, 0,
01025 TeBoxPixelIn ),
01026 "Unable to create histogram" );
01027
01028 TEAGN_WATCH( hist.toString() )
01029
01030 TEAGN_TRUE_OR_THROW( check_histogram_order( hist ),
01031 "Histogram order error" );
01032
01033
01034
01035 TEAGN_TRUE_OR_THROW( ( hist.size() == 3 ), "Invalid histogram size" );
01036
01037 TEAGN_CHECK_EPS( hist.getTotalCount(), 3, 0, "Invalid total count" );
01038
01039 TEAGN_TRUE_OR_THROW( hist.IsDiscrete(), "Discrete verification error" );
01040
01041 it = hist.begin();
01042
01043 TEAGN_CHECK_EQUAL( it->first, 1. , "" );
01044 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
01045
01046 ++it;
01047
01048 TEAGN_CHECK_EQUAL( it->first, 2. , "" );
01049 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
01050
01051 ++it;
01052
01053 TEAGN_CHECK_EQUAL( it->first, 4. , "" );
01054 TEAGN_CHECK_EQUAL( it->second, 1 , "" );
01055
01056
01057 TEAGN_CHECK_EQUAL( hist.GetMinLevel(), 1., "" );
01058 TEAGN_CHECK_EQUAL( hist.GetMaxLevel(), 4., "" );
01059
01060 TEAGN_CHECK_EQUAL( hist.GetMinCount(), 1, "" );
01061 TEAGN_CHECK_EQUAL( hist.GetMaxCount(), 1, "" );
01062
01063 it = hist.begin();
01064 TePDIHistogram::iterator it_end = hist.end();
01065 unsigned int pixelsnmb = 0;
01066 while( it != it_end ) {
01067 pixelsnmb += it->second;
01068 ++it;
01069 }
01070 TEAGN_TRUE_OR_THROW( pixelsnmb == 3, "Invalid pixels number" );
01071 }
01072
01073
01074 int main()
01075 {
01076 TEAGN_LOGMSG( "Test started." );
01077
01078 try{
01079 TeInitRasterDecoders();
01080
01081 TeStdIOProgress pi;
01082 TeProgress::setProgressInterf( dynamic_cast< TeProgressBase* >( &pi ) );
01083
01084 discrete_histogram_test_1();
01085 discrete_histogram_test_2();
01086 discrete_histogram_test_16bits();
01087 interpolated_histogram_test_1();
01088 normal_levels_test2();
01089 normal_levels_test3();
01090 normal_levels_test4();
01091 auto_levels_test();
01092 discretize_test();
01093 performance_test();
01094 operator_equal_from_map_test();
01095 histogram_from_TeCHAR_test();
01096 histogram_from_TeSHORT_test();
01097 discrete_histogram_with_external_polygonset_test();
01098 palette_based_histogram_test();
01099 }
01100 catch( const TeException& excpt ){
01101 TEAGN_LOGERR( excpt.message() )
01102 return EXIT_FAILURE;
01103 }
01104
01105 TEAGN_LOGMSG( "Test OK." );
01106 return EXIT_SUCCESS;
01107 }
01108