00001 #define TEAGN_ENABLE_STDOUT_LOG
00002
00003 #include <TePDIExamplesBase.hpp>
00004
00005 #include <TeAgnostic.h>
00006 #include <TePDIUtils.hpp>
00007 #include <TePDIRegGrowSeg.hpp>
00008 #include <TePDIBaatz.hpp>
00009 #include <TeInitRasterDecoders.h>
00010 #include <TeProgress.h>
00011 #include <TeStdIOProgress.h>
00012
00013 #include <shapefil.h>
00014
00015 #include <time.h>
00016
00017 bool exportPS2SHP( const TePolygonSet& ps,
00018 const std::string& base_file_name )
00019 {
00020
00021 std::string dbfFilename = base_file_name + ".dbf";
00022 std::string shpFilename = base_file_name + ".shp";
00023
00024
00025 TeAttributeList attList;
00026
00027 TeAttribute at;
00028 at.rep_.type_ = TeSTRING;
00029 at.rep_.numChar_ = 10;
00030 at.rep_.name_ = "object_id_";
00031 at.rep_.isPrimaryKey_ = true;
00032
00033 attList.push_back(at);
00034
00035
00036
00037 DBFHandle hDBF = DBFCreate( dbfFilename.c_str() );
00038 TEAGN_TRUE_OR_RETURN( ( hDBF != 0 ), "DBF file creation error" );
00039
00040
00041
00042 TeAttributeList::iterator it=attList.begin();
00043 while ( it != attList.end() )
00044 {
00045 TeAttribute at = (*it);
00046 string atName = at.rep_.name_;
00047
00048
00049
00050 if (at.rep_.type_ == TeSTRING )
00051 {
00052 TEAGN_TRUE_OR_THROW(
00053 ( DBFAddField( hDBF, atName.c_str(), FTString, at.rep_.numChar_, 0 )
00054 != -1 ), "Error writing TeSTRING attribute" );
00055 }
00056 else if (at.rep_.type_ == TeINT)
00057 {
00058 TEAGN_TRUE_OR_THROW(
00059 ( DBFAddField( hDBF, atName.c_str(), FTInteger, 10, 0 ) != -1 ),
00060 "Error writing TeINT attribute" );
00061 }
00062 else if (at.rep_.type_ == TeREAL)
00063 {
00064 TEAGN_TRUE_OR_THROW(
00065 ( DBFAddField( hDBF, atName.c_str(), FTDouble, 10, 5 ) != -1 ),
00066 "Error writing TeREAL attribute" );
00067
00068 }
00069 else if (at.rep_.type_ == TeDATETIME)
00070 {
00071 TEAGN_TRUE_OR_THROW(
00072 ( DBFAddField( hDBF, atName.c_str(), FTDate, 8, 0 ) != -1 ),
00073 "Error writing TeDATETIME attribute" );
00074 }
00075
00076 ++it;
00077 }
00078
00079
00080
00081 SHPHandle hSHP = SHPCreate( shpFilename.c_str(), SHPT_POLYGON );
00082 if( hSHP == 0 ) {
00083 TEAGN_LOGERR( "DBF file creation error" );
00084 DBFClose( hDBF );
00085 return false;
00086 }
00087
00088
00089
00090 int iRecord = 0;
00091 int totpoints = 0;
00092 double *padfX, *padfY;
00093 SHPObject *psObject;
00094 int posXY, npoints, nelem;
00095 int nVertices;
00096 int* panParts;
00097
00098 TePolygonSet::iterator itps;
00099 TePolygon poly;
00100
00101 for (itps = ps.begin() ; itps != ps.end() ; itps++ ) {
00102 poly=(*itps);
00103 totpoints = 0;
00104 nVertices = poly.size();
00105 for (unsigned int n=0; n<poly.size();n++) {
00106 totpoints += poly[n].size();
00107 }
00108
00109 panParts = (int *) malloc(sizeof(int) * nVertices);
00110 padfX = (double *) malloc(sizeof(double) * totpoints);
00111 padfY = (double *) malloc(sizeof(double) * totpoints);
00112 posXY = 0;
00113 nelem = 0;
00114
00115 for (unsigned int l=0; l<poly.size(); ++l) {
00116 if (l==0) {
00117 if (TeOrientation(poly[l]) == TeCOUNTERCLOCKWISE) {
00118 TeReverseLine(poly[l]);
00119 }
00120 } else {
00121 if (TeOrientation(poly[l]) == TeCLOCKWISE) {
00122 TeReverseLine(poly[l]);
00123 }
00124 }
00125
00126 npoints = poly[l].size();
00127 panParts[nelem]=posXY;
00128
00129 for (int m=0; m<npoints; m++ ) {
00130 padfX[posXY] = poly[l][m].x_;
00131 padfY[posXY] = poly[l][m].y_;
00132 posXY++;
00133 }
00134
00135 nelem++;
00136 }
00137
00138 psObject = SHPCreateObject( SHPT_POLYGON, -1, nelem, panParts, NULL,
00139 posXY, padfX, padfY, NULL, NULL );
00140
00141 int shpRes = SHPWriteObject( hSHP, -1, psObject );
00142 TEAGN_TRUE_OR_THROW( ( shpRes != -1 ),
00143 "Unable to create a shape write object" )
00144
00145 SHPDestroyObject( psObject );
00146 free( panParts );
00147 free( padfX );
00148 free( padfY );
00149
00150
00151 for (unsigned int j=0; j<attList.size();j++) {
00152 if ( attList[j].rep_.type_ == TeSTRING ) {
00153 DBFWriteStringAttribute(hDBF, iRecord, j, poly.objectId().c_str() );
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 }
00165
00166 iRecord++;
00167 }
00168
00169 DBFClose( hDBF );
00170 SHPClose( hSHP );
00171
00172 return true;
00173 }
00174
00175
00176 bool checkNumberCells(TePDITypes::TePDIRasterPtrType outRaster, long nbCell)
00177 {
00178 double val;
00179 long max=0;
00180 for (int i=0 ; i<outRaster->params().nlines_ ; i++)
00181 for (int j=0 ; j<outRaster->params().ncols_ ; j++){
00182 outRaster->getElement(j,i,val,0);
00183 if (val>max)
00184 max=(long)val;
00185 }
00186 if (max!=nbCell)
00187 return false;
00188
00189 return true;
00190 }
00191
00192
00193 void TePDIRegGrowSeg_test()
00194 {
00195 TePDIParameters params;
00196
00197 TePDITypes::TePDIRasterPtrType inRaster( new TeRaster(
00198 std::string( TEPDIEXAMPLESRESPATH "cbers_b2_crop.tif" ), 'r' ) );
00199 TEAGN_TRUE_OR_THROW( inRaster->init(), "Unable to init inRaster" );
00200 params.SetParameter( "input_image", inRaster );
00201
00202 TePDITypes::TePDIRasterPtrType outRaster;
00203 TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( outRaster,
00204 1, 1, 1, true, TeUNSIGNEDLONG, 0 ), "RAM Raster Alloc error" );
00205 params.SetParameter( "output_image", outRaster );
00206
00207 TePDITypes::TePDIRasterPtrType dummy_ptr;
00208 params.SetParameter( "exclusion_image", dummy_ptr );
00209 params.SetParameter( "euc_treshold", (double)20 );
00210 params.SetParameter( "area_min", (int)15 );
00211
00212 TePDITypes::TePDIPolSetMapPtrType output_polsets(
00213 new TePDITypes::TePDIPolSetMapType );
00214 params.SetParameter( "output_polsets", output_polsets );
00215
00216 TePDIRegGrowSeg segmenter;
00217
00218 TEAGN_TRUE_OR_THROW( segmenter.Reset(params), "Reset failed" );
00219
00220 double initTime = ((double)clock()) / ((double)CLOCKS_PER_SEC) ;
00221
00222 TEAGN_TRUE_OR_THROW( segmenter.Apply(), "Apply error" );
00223
00224 double endTime = ((double)clock()) / ((double)CLOCKS_PER_SEC) ;
00225
00226 TEAGN_WATCH( endTime - initTime );
00227
00228 TEAGN_TRUE_OR_THROW( TePDIUtils::TeRaster2Geotiff( outRaster,
00229 TEPDIEXAMPLESBINPATH "Segmentation_RegionGrowing_test.tif", TeUNSIGNEDCHAR ),
00230 "GeoTIF generation error" );
00231
00232 TePDITypes::TePDIPolSetMapType::iterator it = output_polsets->begin();
00233 TePDITypes::TePDIPolSetMapType::iterator it_end = output_polsets->end();
00234
00235 unsigned int pols_number = 0;
00236
00237 while( it != it_end ) {
00238 TEAGN_TRUE_OR_THROW( exportPS2SHP( it->second,
00239 TEPDIEXAMPLESBINPATH "Segmentation_RegionGrowingPols_ps" +
00240 Te2String( (int)it->first ) ), "Polygonset export error" )
00241
00242 pols_number += it->second.size();
00243
00244 ++it;
00245 }
00246
00247 TEAGN_CHECK_EPS( output_polsets->size(), 23, 0.0,
00248 "Invalid generated polygon set size" );
00249 TEAGN_CHECK_EPS( pols_number, 23, 0.0,
00250 "Invalid generated polygon set size" );
00251
00252
00253
00254
00255
00256 TEAGN_TRUE_OR_THROW( checkNumberCells(outRaster, 23),
00257 "Check number of cell Error" );
00258 }
00259
00260 void TePDIBaatz_test()
00261 {
00262 TePDIParameters params;
00263
00264 TePDITypes::TePDIRasterPtrType inRaster( new TeRaster(
00265 std::string( TEPDIEXAMPLESRESPATH "cbers_rgb342_crop1.tif" ), 'r' ) );
00266 TEAGN_TRUE_OR_THROW( inRaster->init(), "Unable to init inRaster" );
00267 params.SetParameter( "input_image", inRaster );
00268
00269 std::vector<unsigned> input_channels;
00270 input_channels.push_back( 0 );
00271 input_channels.push_back( 1 );
00272 input_channels.push_back( 2 );
00273 params.SetParameter( "input_channels", input_channels );
00274
00275 TePDITypes::TePDIRasterPtrType outRaster;
00276 TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( outRaster,
00277 1, 1, 1, true, TeUNSIGNEDLONG, 0 ), "RAM Raster Alloc error" );
00278 params.SetParameter( "output_image", outRaster );
00279 params.SetParameter( "scale", (float) 30 );
00280 params.SetParameter( "compactness", (float) 0.4 );
00281 params.SetParameter( "color", (float) 0.5 );
00282
00283 vector<float> input_weights;
00284 input_weights.push_back( (float) 0.7 );
00285 input_weights.push_back( (float) 0.2 );
00286 input_weights.push_back( (float) 0.1 );
00287 params.SetParameter( "input_weights", input_weights );
00288
00289 TePDITypes::TePDIPolSetMapPtrType output_polsets(
00290 new TePDITypes::TePDIPolSetMapType );
00291 params.SetParameter( "output_polsets", output_polsets );
00292
00293 TePDIBaatz segmenter;
00294
00295 TEAGN_TRUE_OR_THROW( segmenter.Reset(params), "Reset failed" );
00296
00297 double initTime = ((double)clock()) / ((double)CLOCKS_PER_SEC) ;
00298
00299 TEAGN_TRUE_OR_THROW( segmenter.Apply(), "Apply error" );
00300
00301 double endTime = ((double)clock()) / ((double)CLOCKS_PER_SEC) ;
00302
00303 TEAGN_WATCH( endTime - initTime );
00304
00305 TEAGN_TRUE_OR_THROW( TePDIUtils::TeRaster2Geotiff( outRaster,
00306 TEPDIEXAMPLESBINPATH "Segmentation_Baatz_test.tif", TeUNSIGNEDCHAR ),
00307 "GeoTIF generation error" );
00308
00309 TePDITypes::TePDIPolSetMapType::iterator it = output_polsets->begin();
00310 TePDITypes::TePDIPolSetMapType::iterator it_end = output_polsets->end();
00311 TePolygonSet pols;
00312 while( it != it_end )
00313 {
00314 pols.copyElements(it->second);
00315 ++it;
00316 }
00317 exportPS2SHP( pols, TEPDIEXAMPLESBINPATH "Segmentation_BaatzPols_ps");
00318 }
00319
00320
00321 int main()
00322 {
00323 TEAGN_LOGMSG( "Test started." );
00324
00325 try{
00326 TeStdIOProgress pi;
00327 TeProgress::setProgressInterf( dynamic_cast< TeProgressBase* >( &pi ) );
00328
00329 TeInitRasterDecoders();
00330
00331 TePDIBaatz_test();
00332 TePDIRegGrowSeg_test();
00333 }
00334 catch( const TeException& e ){
00335 TEAGN_LOGERR( "Test Failed - " + e.message() );
00336 return EXIT_FAILURE;
00337 }
00338
00339 TEAGN_LOGMSG( "Test OK." );
00340 return EXIT_SUCCESS;
00341 }
00342