[pktools] 256/375: working on dsm2dtm in Filter2d.h, error for south to north...

Bas Couwenberg sebastic at xs4all.nl
Wed Dec 3 21:54:19 UTC 2014


This is an automated email from the git hooks/post-receive script.

sebastic-guest pushed a commit to branch upstream-master
in repository pktools.

commit 076ab5dc8e3ed00d4f2326cf57ccf4b0ecbeaba8
Author: Pieter Kempeneers <kempenep at gmail.com>
Date:   Mon May 5 18:19:01 2014 +0200

    working on dsm2dtm in Filter2d.h, error for south to north...
---
 src/algorithms/Filter2d.h | 78 +++++++++++++++++++++++++----------------------
 src/apps/pkfilterdem.cc   | 72 ++++++++++++++++++++++++-------------------
 2 files changed, 82 insertions(+), 68 deletions(-)

diff --git a/src/algorithms/Filter2d.h b/src/algorithms/Filter2d.h
index 54aeee0..c623b07 100644
--- a/src/algorithms/Filter2d.h
+++ b/src/algorithms/Filter2d.h
@@ -825,14 +825,17 @@ template<class T> unsigned long int Filter2d::morphology(const Vector2d<T>& inpu
 	    ++nmasked;
 	}
       }
-      if(nmasked<nlimit){
+      if(nmasked<=nlimit){
 	++nchange;
 	//reset pixel in outputMask
 	outputMask[y][x]=0;
       }
       else{
 	//reset pixel height in tmpDSM
-	inBuffer[(dimY-1)/2][x]=stat.mymin(neighbors);
+	sort(neighbors.begin(),neighbors.end());
+	assert(neighbors.size()>1);
+	inBuffer[(dimY-1)/2][x]=neighbors[1];
+	/* inBuffer[(dimY-1)/2][x]=stat.mymin(neighbors); */
       }
     }
     progress=(1.0+y);
@@ -916,14 +919,17 @@ template<class T> unsigned long int Filter2d::morphology(const Vector2d<T>& inpu
 	    ++nmasked;
 	}
       }
-      if(nmasked<nlimit){
+      if(nmasked<=nlimit){
 	++nchange;
 	//reset pixel in outputMask
 	outputMask[y][x]=0;
       }
       else{
 	//reset pixel height in tmpDSM
-	inBuffer[(dimY-1)/2][x]=stat.mymin(neighbors);
+	sort(neighbors.begin(),neighbors.end());
+	assert(neighbors.size()>1);
+	inBuffer[(dimY-1)/2][x]=neighbors[1];
+	/* inBuffer[(dimY-1)/2][x]=stat.mymin(neighbors); */
       }
     }
     progress=(1.0+y);
@@ -956,30 +962,26 @@ template<class T> unsigned long int Filter2d::morphology(const Vector2d<T>& inpu
   if(outputMask.size()!=inputDSM.nRows())
     outputMask.resize(inputDSM.nRows());
   int indexI=0;
-  int indexJ=0;
-  //initialize last half of inBuffer
-  for(int j=-(dimY-1)/2;j<=dimY/2;++j){
+  int indexJ=inputDSM.nRows()-1;
+  //initialize first half of inBuffer
+  for(int j=inputDSM.nRows()-dimY/2;j<inputDSM.nRows();--j){
     for(int i=0;i<inputDSM.nCols();++i)
       inBuffer[indexJ][i]=tmpDSM[abs(j)][i];
     ++indexJ;
   }
   for(int y=tmpDSM.nRows()-1;y>=0;--y){
-    if(y){//inBuffer already initialized for y=0
-      //erase first line from inBuffer
-      inBuffer.erase(inBuffer.begin());
-      //read extra line and push back to inBuffer if not out of bounds
-      if(y+dimY/2<tmpDSM.nRows()){
+    if(y<tmpDSM.nRows()-1){//inBuffer already initialized for y=tmpDSM.nRows()-1
+      //erase last line from inBuffer
+      inBuffer.erase(inBuffer.end()-1);
+      //read extra line and insert to inBuffer if not out of bounds
+      if(y-dimY/2>0){
         //allocate buffer
-        inBuffer.push_back(inBuffer.back());
+        inBuffer.insert(inBuffer.begin(),inBuffer.back());
         for(int i=0;i<tmpDSM.nCols();++i)
-          inBuffer[inBuffer.size()-1][i]=tmpDSM[y+dimY/2][i];
+          inBuffer[0][i]=tmpDSM[y-dimY/2][i];
       }
       else{
-        int over=y+dimY/2-tmpDSM.nRows();
-        int index=(inBuffer.size()-1)-over;
-        assert(index>=0);
-        assert(index<inBuffer.size());
-        inBuffer.push_back(inBuffer[index]);
+        inBuffer.insert(inBuffer.begin(),inBuffer[abs(y-dimY/2)]);
       }
     }
     for(int x=tmpDSM.nCols()-1;x>=0;--x){
@@ -1007,14 +1009,17 @@ template<class T> unsigned long int Filter2d::morphology(const Vector2d<T>& inpu
 	    ++nmasked;
 	}
       }
-      if(nmasked<nlimit){
+      if(nmasked<=nlimit){
 	++nchange;
 	//reset pixel in outputMask
 	outputMask[y][x]=0;
       }
       else{
 	//reset pixel height in tmpDSM
-	inBuffer[(dimY-1)/2][x]=stat.mymin(neighbors);
+	sort(neighbors.begin(),neighbors.end());
+	assert(neighbors.size()>1);
+	inBuffer[(dimY-1)/2][x]=neighbors[1];
+	/* inBuffer[(dimY-1)/2][x]=stat.mymin(neighbors); */
       }
     }
     progress=(1.0+y);
@@ -1048,29 +1053,25 @@ template<class T> unsigned long int Filter2d::morphology(const Vector2d<T>& inpu
     outputMask.resize(inputDSM.nRows());
   int indexI=0;
   int indexJ=0;
-  //initialize last half of inBuffer
-  for(int j=-(dimY-1)/2;j<=dimY/2;++j){
+  //initialize first half of inBuffer
+  for(int j=inputDSM.nRows()-dimY/2;j<inputDSM.nRows();--j){
     for(int i=0;i<inputDSM.nCols();++i)
       inBuffer[indexJ][i]=tmpDSM[abs(j)][i];
     ++indexJ;
   }
   for(int y=tmpDSM.nRows()-1;y>=0;--y){
-    if(y){//inBuffer already initialized for y=0
-      //erase first line from inBuffer
-      inBuffer.erase(inBuffer.begin());
-      //read extra line and push back to inBuffer if not out of bounds
-      if(y+dimY/2<tmpDSM.nRows()){
+    if(y<tmpDSM.nRows()-1){//inBuffer already initialized for y=0
+      //erase last line from inBuffer
+      inBuffer.erase(inBuffer.end()-1);
+      //read extra line and insert to inBuffer if not out of bounds
+      if(y-dimY/2>0){
         //allocate buffer
-        inBuffer.push_back(inBuffer.back());
+        inBuffer.insert(inBuffer.begin(),inBuffer.back());
         for(int i=0;i<tmpDSM.nCols();++i)
-          inBuffer[inBuffer.size()-1][i]=tmpDSM[y+dimY/2][i];
+          inBuffer[0][i]=tmpDSM[y-dimY/2][i];
       }
       else{
-        int over=y+dimY/2-tmpDSM.nRows();
-        int index=(inBuffer.size()-1)-over;
-        assert(index>=0);
-        assert(index<inBuffer.size());
-        inBuffer.push_back(inBuffer[index]);
+        inBuffer.insert(inBuffer.begin(),inBuffer[abs(y-dimY/2)]);
       }
     }
     for(int x=0;x<tmpDSM.nCols();++x){
@@ -1098,14 +1099,17 @@ template<class T> unsigned long int Filter2d::morphology(const Vector2d<T>& inpu
 	    ++nmasked;
 	}
       }
-      if(nmasked<nlimit){
+      if(nmasked<=nlimit){
 	++nchange;
 	//reset pixel in outputMask
 	outputMask[y][x]=0;
       }
       else{
 	//reset pixel height in tmpDSM
-	inBuffer[(dimY-1)/2][x]=stat.mymin(neighbors);
+	sort(neighbors.begin(),neighbors.end());
+	assert(neighbors.size()>1);
+	inBuffer[(dimY-1)/2][x]=neighbors[1];
+	/* inBuffer[(dimY-1)/2][x]=stat.mymin(neighbors); */
       }
     }
     progress=(1.0+y);
diff --git a/src/apps/pkfilterdem.cc b/src/apps/pkfilterdem.cc
index b96e06c..62f5d0b 100644
--- a/src/apps/pkfilterdem.cc
+++ b/src/apps/pkfilterdem.cc
@@ -161,11 +161,12 @@ int main(int argc,char **argv) {
   unsigned long int nchange=1;
   if(postFilter_opt[0]=="vito"){
     //todo: fill empty pixels
-    // hThreshold_opt.resize(3);
+    // hThreshold_opt.resize(4);
     // hThreshold_opt[0]=0.7;
     // hThreshold_opt[1]=0.3;
     // hThreshold_opt[2]=0.1;
-    vector<int> nlimit(3);
+    // hThreshold_opt[2]=-0.2;
+    vector<int> nlimit(4);
     nlimit[0]=2;
     nlimit[1]=3;
     nlimit[2]=4;
@@ -184,40 +185,39 @@ int main(int argc,char **argv) {
 	  tmpMask[irow][icol]=1;//1=surface, 0=terrain
       if(verbose_opt[0])
 	cout << "filtering NWSE" << endl;
-      theFilter.dsm2dtm_nwse(inputData,tmpMask,hThreshold_opt[iheight],nlimit[iheight],dim_opt[0]);
-
-      // //from here
-
+      //from here
       // Vector2d<double> tmpDSM(inputData);
-      // double noDataValue=0;
-
-      // unsigned long int nchange=0;
       // int dimX=dim_opt[0];
       // int dimY=dim_opt[0];
       // assert(dimX);
       // assert(dimY);
       // statfactory::StatFactory stat;
       // Vector2d<double> inBuffer(dimY,inputData.nCols());
-      // // if(tmpMask.size()!=inputData.nRows())
-      // // 	tmpMask.resize(inputData.nRows());
+      // if(tmpData.size()!=inputData.nRows())
+      // 	tmpData.resize(inputData.nRows());
       // int indexI=0;
-      // int indexJ=0;
+      // int indexJ=inputData.nRows()-1;
+      // // int indexJ=0;
       // //initialize last half of inBuffer
       // for(int j=-(dimY-1)/2;j<=dimY/2;++j){
       // 	for(int i=0;i<inputData.nCols();++i)
       // 	  inBuffer[indexJ][i]=tmpDSM[abs(j)][i];
-      // 	++indexJ;
+      // 	--indexJ;
+      // 	// ++indexJ;
       // }
-      // for(int y=0;y<tmpDSM.nRows();++y){
+      // for(int y=tmpDSM.nRows()-1;y>=0;--y){
       // 	if(y){//inBuffer already initialized for y=0
       // 	  //erase first line from inBuffer
-      // 	  inBuffer.erase(inBuffer.begin());
+      // 	  inBuffer.erase(inBuffer.end()-1);
+      // 	  // inBuffer.erase(inBuffer.begin());
       // 	  //read extra line and push back to inBuffer if not out of bounds
       // 	  if(y+dimY/2<tmpDSM.nRows()){
       // 	    //allocate buffer
-      // 	    inBuffer.push_back(inBuffer.back());
-      // 	    for(int i=0;i<tmpDSM.nCols();++i)
-      // 	      inBuffer[inBuffer.size()-1][i]=tmpDSM[y+dimY/2][i];
+      // 	    // inBuffer.push_back(inBuffer.back());
+      // 	    inBuffer.insert(inBuffer.begin(),*(inBuffer.begin()));
+      // 	    for(int i=0;i<tmpDSM.nCols();++i) 
+      // 	      inBuffer[0][i]=tmpDSM[y-dimY/2][i];
+      // 	      // inBuffer[inBuffer.size()-1][i]=tmpDSM[y+dimY/2][i];
       // 	  }
       // 	  else{
       // 	    int over=y+dimY/2-tmpDSM.nRows();
@@ -227,8 +227,10 @@ int main(int argc,char **argv) {
       // 	    inBuffer.push_back(inBuffer[index]);
       // 	  }
       // 	}
-      // 	for(int x=0;x<tmpDSM.nCols();++x){
+      // 	for(int x=tmpDSM.nCols()-1;x>=0;--x){
       // 	  double centerValue=inBuffer[(dimY-1)/2][x];
+      // 	  //test
+      // 	  cout << "pixel ("  << x << "," << y << "): " << centerValue << endl;
       // 	  short nmasked=0;
       // 	  std::vector<double> neighbors;
       // 	  for(int j=-(dimY-1)/2;j<=dimY/2;++j){
@@ -246,41 +248,49 @@ int main(int argc,char **argv) {
       // 	      else
       // 		indexJ=(dimY-1)/2+j;
       // 	      double difference=(centerValue-inBuffer[indexJ][indexI]);
+      // 	      //test
+      // 	      cout << "centerValue-inBuffer[" << indexJ << "][" << indexI << "]=" << centerValue << " - " << inBuffer[indexJ][indexI] << " = " << difference << endl;
       // 	      if(i||j)//skip centerValue
       // 		neighbors.push_back(inBuffer[indexJ][indexI]);
       // 	      if(difference>hThreshold_opt[iheight])
       // 		++nmasked;
       // 	    }
       // 	  }
-      // 	  if(nmasked<nlimit[iheight]){
+      // 	  //test
+      // 	  cout << "pixel " << x << ", " << y << ": nmasked is " << nmasked << endl;
+      // 	  if(nmasked<=nlimit[iheight]){
       // 	    ++nchange;
-      // 	    //reset pixel in tmpMask
-      // 	    tmpMask[y][x]=0;
+      // 	    //reset pixel in outputMask
+      // 	    tmpData[y][x]=0;
+      // 	    //test
+      // 	    cout << "pixel " << x << ", " << y << " is ground" << endl;
       // 	  }
       // 	  else{
       // 	    //reset pixel height in tmpDSM
-      // 	    inBuffer[(dimY-1)/2][x]=stat.mymin(neighbors);
+      // 	    sort(neighbors.begin(),neighbors.end());
+      // 	    assert(neighbors.size()>1);
+      // 	    inBuffer[(dimY-1)/2][x]=neighbors[1];
+      // 	    //test
+      // 	    cout << "pixel " << x << ", " << y << " is surface, reset DSM to " << neighbors[1] << endl;
+      // 	    /* inBuffer[(dimY-1)/2][x]=stat.mymin(neighbors); */
       // 	  }
       // 	}
       // }
       //to here
 
-      tmpData.setMask(tmpMask,0,0);
+      theFilter.dsm2dtm_nwse(inputData,tmpData,hThreshold_opt[iheight],nlimit[iheight],dim_opt[0]);
       if(verbose_opt[0])
       	cout << "filtering NESW" << endl;
-      theFilter.dsm2dtm_nesw(inputData,tmpMask,hThreshold_opt[iheight],nlimit[iheight],dim_opt[0]);
-      tmpData.setMask(tmpMask,0,0);
+      theFilter.dsm2dtm_nesw(inputData,tmpData,hThreshold_opt[iheight],nlimit[iheight],dim_opt[0]);
       if(verbose_opt[0])
       	cout << "filtering SENW" << endl;
-      theFilter.dsm2dtm_senw(inputData,tmpMask,hThreshold_opt[iheight],nlimit[iheight],dim_opt[0]);
-      tmpData.setMask(tmpMask,0,0);
+      theFilter.dsm2dtm_senw(inputData,tmpData,hThreshold_opt[iheight],nlimit[iheight],dim_opt[0]);
       if(verbose_opt[0])
       	cout << "filtering SWNE" << endl;
-      theFilter.dsm2dtm_swne(inputData,tmpMask,hThreshold_opt[iheight],nlimit[iheight],dim_opt[0]);
-      // set tmpMask to finalMask
-      tmpData.setMask(tmpMask,0,0);
+      theFilter.dsm2dtm_swne(inputData,tmpData,hThreshold_opt[iheight],nlimit[iheight],dim_opt[0]);
     }
     outputData=tmpData;
+    //todo: interpolate
     //outputData.setMask(tmpData,1,0);
   }    
   else if(postFilter_opt[0]=="etew_min"){

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-grass/pktools.git



More information about the Pkg-grass-devel mailing list