From 73fd94356670847b3c56e4fb626865887071d8d7 Mon Sep 17 00:00:00 2001
From: Cyrille Valladeau <cyrille.valladeau@c-s.fr>
Date: Tue, 17 May 2011 18:42:04 +0200
Subject: [PATCH] BUG: fix bug 295

---
 Testing/Code/BasicFilters/CMakeLists.txt      |   8 --
 .../BasicFilters/otbBasicFiltersTests9.cxx    |   1 -
 ...tVectorImageFilterWithStreamingShrink.cxx} |  11 +-
 Testing/Fa/CMakeLists.txt                     |  13 +-
 Utilities/otbedison/segm/msImageProcessor.cpp | 116 +++++++++---------
 5 files changed, 74 insertions(+), 75 deletions(-)
 rename Testing/{Code/BasicFilters/otbMeanShiftVectorImageFilter_BUG295.cxx => Fa/000295-MeanShiftVectorImageFilterWithStreamingShrink.cxx} (83%)

diff --git a/Testing/Code/BasicFilters/CMakeLists.txt b/Testing/Code/BasicFilters/CMakeLists.txt
index 06ef64ddf6..6aaa8c665a 100644
--- a/Testing/Code/BasicFilters/CMakeLists.txt
+++ b/Testing/Code/BasicFilters/CMakeLists.txt
@@ -1272,13 +1272,6 @@ ADD_TEST(bfTvMeanShiftVectorImageFilterNotStreamedNotThreaded ${BASICFILTERS_TES
 	16 16 10 1.0 0 0
 	)
 
-ADD_TEST(bfTvMeanShiftVectorImageFilter_BUG295 ${BASICFILTERS_TESTS9}
-        otbMeanShiftVectorImageFilter_BUG295
-	${INPUTDATA}/qb_RoadExtract2sub200x200.tif
-	${TEMP}/bfMeanShiftVectorImageFilter_BUG295.tif
-        20 50 500 2
-	)
-
 ADD_TEST(bfTuFunctionToImageFilterNew ${BASICFILTERS_TESTS9}
         otbFunctionToImageFilterNew
 )
@@ -2260,7 +2253,6 @@ otbMeanShiftImageFilterNew.cxx
 otbMeanShiftImageFilter.cxx
 otbMeanShiftVectorImageFilterNew.cxx
 otbMeanShiftVectorImageFilter.cxx
-otbMeanShiftVectorImageFilter_BUG295.cxx
 otbFunctionToImageFilterNew.cxx
 otbFunctionToImageFilter.cxx
 otbBinaryImageDensityFunctionNew.cxx
diff --git a/Testing/Code/BasicFilters/otbBasicFiltersTests9.cxx b/Testing/Code/BasicFilters/otbBasicFiltersTests9.cxx
index 50ba387ccd..69c2de1561 100644
--- a/Testing/Code/BasicFilters/otbBasicFiltersTests9.cxx
+++ b/Testing/Code/BasicFilters/otbBasicFiltersTests9.cxx
@@ -30,7 +30,6 @@ void RegisterTests()
   REGISTER_TEST(otbMeanShiftImageFilter);
   REGISTER_TEST(otbMeanShiftVectorImageFilterNew);
   REGISTER_TEST(otbMeanShiftVectorImageFilter);
-  REGISTER_TEST(otbMeanShiftVectorImageFilter_BUG295);
   REGISTER_TEST(otbFunctionToImageFilterNew);
   REGISTER_TEST(otbFunctionToImageFilter);
   REGISTER_TEST(otbBinaryImageDensityFunctionNew);
diff --git a/Testing/Code/BasicFilters/otbMeanShiftVectorImageFilter_BUG295.cxx b/Testing/Fa/000295-MeanShiftVectorImageFilterWithStreamingShrink.cxx
similarity index 83%
rename from Testing/Code/BasicFilters/otbMeanShiftVectorImageFilter_BUG295.cxx
rename to Testing/Fa/000295-MeanShiftVectorImageFilterWithStreamingShrink.cxx
index df178cb229..a0828ad22f 100644
--- a/Testing/Code/BasicFilters/otbMeanShiftVectorImageFilter_BUG295.cxx
+++ b/Testing/Fa/000295-MeanShiftVectorImageFilterWithStreamingShrink.cxx
@@ -23,14 +23,13 @@
 #include "otbStreamingShrinkImageFilter.h"
 
 
-int otbMeanShiftVectorImageFilter_BUG295(int argc, char * argv[])
+int main(int argc, char * argv[])
 {
   const char *       infname                = argv[1];
-  const char *       filteredfname          = argv[2];
-  const unsigned int spatialRadius          = atoi(argv[3]);
-  const double       rangeRadius            = atof(argv[4]);
-  const unsigned int minRegionSize          = atoi(argv[5]);
-  unsigned int       factor                 = atoi(argv[6]);
+  const unsigned int spatialRadius          = atoi(argv[2]);
+  const double       rangeRadius            = atof(argv[3]);
+  const unsigned int minRegionSize          = atoi(argv[4]);
+  unsigned int       factor                 = atoi(argv[5]);
 
   const unsigned int Dimension = 2;
   typedef float                                           PixelType;
diff --git a/Testing/Fa/CMakeLists.txt b/Testing/Fa/CMakeLists.txt
index a9f51c0006..36406c8b56 100644
--- a/Testing/Fa/CMakeLists.txt
+++ b/Testing/Fa/CMakeLists.txt
@@ -22,7 +22,7 @@ SET(IO_TESTS     ${CXX_TEST_PATH}/otbIOTests9)
 SET(IO_TESTS2     ${CXX_TEST_PATH}/otbIOTests7)
 SET(VISU_TESTS   ${CXX_TEST_PATH}/otbVisuTests1)
 
-# -------       FAs traitees -----------------------------------
+# -------       FAs fixed -----------------------------------
 
 # ---  FA 00005 ENVI (GDAL) image multi-canal ---
 ADD_TEST(FA-00005-io_EnviMultiBande_RW ${IO_TESTS}
@@ -188,7 +188,13 @@ ADD_TEST(FA-00060-le_Map_Activation ${CXX_TEST_PATH}/MapActivation
         ${TEMP}/FA-00013-le-som.hdr
 	)
 
-# -------       Fin des FAs traitees -----------------------------------
+# ---  FA 000295 : Pb Mean Shift clustering module  ---
+ADD_TEST(000295-MeanShiftVectorImageFilterWithStreamingShrink ${CXX_TEST_PATH}/000295-MeanShiftVectorImageFilterWithStreamingShrink
+	${INPUTDATA}/qb_RoadExtract2sub200x200.tif
+        20 50 500 10
+	)
+
+# -------       End of fixed FAs -----------------------------------
 
 ADD_TEST(FA-00070-vi_Streaming_Stat ${CXX_TEST_PATH}/StreamingStat
 	${EXAMPLES_DATA}/qb_RoadExtract.tif
@@ -275,4 +281,7 @@ TARGET_LINK_LIBRARIES(PolygonsVectorization OTBIO OTBCommon)
 ADD_EXECUTABLE(0000169-fftcomplextocomplex  0000169-fftcomplextocomplex.cxx)
 TARGET_LINK_LIBRARIES(0000169-fftcomplextocomplex OTBIO OTBCommon)
 
+ADD_EXECUTABLE(000295-MeanShiftVectorImageFilterWithStreamingShrink 000295-MeanShiftVectorImageFilterWithStreamingShrink.cxx)
+TARGET_LINK_LIBRARIES(000295-MeanShiftVectorImageFilterWithStreamingShrink OTBIO OTBCommon)
+
 ENDIF( NOT OTB_DISABLE_CXX_TESTING AND BUILD_TESTING )
diff --git a/Utilities/otbedison/segm/msImageProcessor.cpp b/Utilities/otbedison/segm/msImageProcessor.cpp
index a983d6f6d6..80951df9ac 100644
--- a/Utilities/otbedison/segm/msImageProcessor.cpp
+++ b/Utilities/otbedison/segm/msImageProcessor.cpp
@@ -2793,7 +2793,7 @@ void msImageProcessor::Prune(int minRegion)
 
 			//*******************************************************************************
 
-			if(modePointCounts[i] < minRegion)
+			if(modePointCounts[i] < minRegion && raList[i].next != NULL)
 			{
 				//update minRegionCount to indicate that a region
 				//having area less than minRegion was found
@@ -2803,63 +2803,63 @@ void msImageProcessor::Prune(int minRegion)
 				//region adjacency list of the ith region...
 				neighbor	= raList[i].next;
 				
-				//calculate the distance between the mode of the ith
-				//region and that of the neighboring region...
-				candidate		= neighbor->label;
-				minSqDistance	= SqDistance(i, candidate);
-				
-				//traverse region adjacency list of region i and select
-				//a candidate region
-				neighbor	= neighbor->next;
-				while(neighbor)
-				{
-
-					//calculate the square distance between region i
-					//and current neighbor...
-					neighborDistance = SqDistance(i, neighbor->label);
-
-					//if this neighbors square distance to region i is less
-					//than minSqDistance, then select this neighbor as the
-					//candidate region for region i
-					if(neighborDistance < minSqDistance)
-					{
-						minSqDistance	= neighborDistance;
-						candidate		= neighbor->label;
-					}
-
-					//traverse region list of region i
-					neighbor	= neighbor->next;
-
-				}
-
-				//join region i with its candidate region:
-
-				// (1) find the canonical element of region i
-				iCanEl		= i;
-				while(raList[iCanEl].label != iCanEl)
-					iCanEl		= raList[iCanEl].label;
-
-				// (2) find the canonical element of neighboring region
-				neighCanEl	= candidate;
-				while(raList[neighCanEl].label != neighCanEl)
-					neighCanEl	= raList[neighCanEl].label;
-
-				// if the canonical elements of are not the same then assign
-				// the canonical element having the smaller label to be the parent
-				// of the other region...
-				if(iCanEl < neighCanEl)
-					raList[neighCanEl].label	= iCanEl;
-				else
-				{
-					//must replace the canonical element of previous
-					//parent as well
-					raList[raList[iCanEl].label].label	= neighCanEl;
-
-					//re-assign canonical element
-					raList[iCanEl].label				= neighCanEl;
-				}
-			}
-		}
+                                //calculate the distance between the mode of the ith
+                                //region and that of the neighboring region...
+                                candidate		= neighbor->label;
+                                minSqDistance	= SqDistance(i, candidate);
+                                
+                                //traverse region adjacency list of region i and select
+                                //a candidate region
+                                neighbor	= neighbor->next;
+                                while(neighbor)
+                                  {
+                                    
+                                    //calculate the square distance between region i
+                                    //and current neighbor...
+                                    neighborDistance = SqDistance(i, neighbor->label);
+                                    
+                                    //if this neighbors square distance to region i is less
+                                    //than minSqDistance, then select this neighbor as the
+                                    //candidate region for region i
+                                    if(neighborDistance < minSqDistance)
+                                      {
+                                        minSqDistance	= neighborDistance;
+                                        candidate		= neighbor->label;
+                                      }
+                                    
+                                    //traverse region list of region i
+                                    neighbor	= neighbor->next;
+                                    
+                                  }
+                                
+                                //join region i with its candidate region:
+                                
+                                // (1) find the canonical element of region i
+                                iCanEl		= i;
+                                while(raList[iCanEl].label != iCanEl)
+                                  iCanEl		= raList[iCanEl].label;
+                                    
+                                // (2) find the canonical element of neighboring region
+                                neighCanEl	= candidate;
+                                while(raList[neighCanEl].label != neighCanEl)
+                                  neighCanEl	= raList[neighCanEl].label;
+                                
+                                // if the canonical elements of are not the same then assign
+                                // the canonical element having the smaller label to be the parent
+                                // of the other region...
+                                if(iCanEl < neighCanEl)
+                                  raList[neighCanEl].label	= iCanEl;
+                                else
+                                  {
+                                    //must replace the canonical element of previous
+                                    //parent as well
+                                    raList[raList[iCanEl].label].label	= neighCanEl;
+                                    
+                                    //re-assign canonical element
+                                    raList[iCanEl].label				= neighCanEl;
+                                  }
+                        }
+                }
 
 		// Step (3):
 		
-- 
GitLab