[GRASS-SVN] r57237 - grass/trunk/imagery/i.segment

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Jul 21 01:54:51 PDT 2013


Author: mmetz
Date: 2013-07-21 01:54:51 -0700 (Sun, 21 Jul 2013)
New Revision: 57237

Modified:
   grass/trunk/imagery/i.segment/create_isegs.c
   grass/trunk/imagery/i.segment/open_files.c
Log:
i.segment: fix best neighbor

Modified: grass/trunk/imagery/i.segment/create_isegs.c
===================================================================
--- grass/trunk/imagery/i.segment/create_isegs.c	2013-07-21 08:38:51 UTC (rev 57236)
+++ grass/trunk/imagery/i.segment/create_isegs.c	2013-07-21 08:54:51 UTC (rev 57237)
@@ -288,6 +288,8 @@
 		
 		if (Ri.id < 0)
 		    continue;
+		if (Ri.id == 0)
+		    G_fatal_error("Zero segment id at row %d, col %d", Ri.row, Ri.col);
 
 		/* find segment neighbors */
 		/* find Ri's best neighbor, clear candidate flag */
@@ -345,9 +347,6 @@
 		        (globals->nn >= 8 && Rk.count <= globals->nn))
 		    candidates_only = FALSE;
 		}
-		
-		if (Rk.id == 0)
-		    pathflag = FALSE;
 
 		while (pathflag) {
 		    pathflag = FALSE;
@@ -380,19 +379,12 @@
 
 			/* not mutually best neighbors */
 			if (Rk_similarity != Ri_similarity) {
-			    /* important for fp precision limit
-			     * because region stats may be calculated 
-			     * in two slightly different ways */
-			    if (Ri_similarity - Rk_similarity > EPSILON)
 				do_merge = 0;
 			}
 			/* Ri has only one neighbor, merge */
 			if (Ri_nn == 1 && Rk_nn > 1)
 			    do_merge = 1;
 
-			if (Rk.id == 0)
-			    do_merge = 0;
-
 			/* adjust threshold */
 			if (do_merge) {
 			    int smaller = Rk.count;
@@ -443,7 +435,8 @@
 			    search_neighbors(&Ri, &Ri_rs, Ri_ngbrs, &Ri_similarity,
 					     &Rk, &Rk_rs, globals);
 
-			    if (Ri_nn > 0 && compare_double(Ri_similarity, threshold) == -1) {
+			    if (Rk.id != 0 && Ri_nn > 0 && 
+			        compare_double(Ri_similarity, threshold) == -1) {
 
 				pathflag = TRUE;
 				/* candidates_only:
@@ -468,6 +461,11 @@
 			    if (Rk.id < 1)
 				pathflag = FALSE;
 
+			    if (Rk_bestn.id == 0) {
+				G_debug(4, "Rk's best neighour is zero");
+				pathflag = FALSE;
+			    }
+
 			    if (pathflag) {
 				
 				/* clear candidate flag for Rk */
@@ -686,7 +684,7 @@
     nbtree_clear(Ri_ngbrs);
     n_ngbrs = 0;
     /* TODO: add size of largest region to reg_tree, use this as min */
-    Rk->count = globals->ncells;
+    Rk->count = globals->ncells + 1;
     Rk->id = Rk_rs->id = 0;
 
     /* go through segment, spreading outwards from head */
@@ -906,7 +904,7 @@
     double val = 0.;
     int n = globals->nbands - 1;
 
-    /* squared euclidean distance, sum the square differences for each dimension */
+    /* squared manhattan distance, sum the differences for each dimension */
     do {
 	val += Ri->mean[n] - Rk->mean[n];
     } while (n--);
@@ -1018,8 +1016,16 @@
 
     G_debug(4, "search_neighbors");
 
+    if (Ri->id != Ri_rs->id)
+	G_fatal_error("Ri = %d but Ri_rs = %d", Ri->id, Ri_rs->id);
+    if (Ri->id <= 0)
+	G_fatal_error("Ri is %d", Ri->id);
+    if (Ri_rs->id <= 0)
+	G_fatal_error("Ri_rs is %d", Ri_rs->id);
+
     nbtree_init_trav(&travngbr, Ri_ngbrs);
     Rk->count = globals->ncells + 1;
+    Rk->id = Rk_rs->id = 0;
 
     while ((next = nbtree_traverse(&travngbr))) {
 	tempsim = (globals->calculate_similarity)(Ri, next, globals);
@@ -1047,20 +1053,10 @@
     }
     Rk_rs->id = Rk->id;
 
-    /* faster, but with fp error:
-     * calculate sum from mean and count */
-    /*
-    Rk_rs->count = Rk->count;
-    memcpy(Rk_rs->mean, Rk->mean, globals->datasize);
-    i = globals->nbands - 1;
-    do {
-	Rk_rs->sum[i] = Rk_rs->mean[i] * Rk_rs->count;
-    } while (i--);
-    */
+    if (Rk->id != 0) {
+	fetch_reg_stats(Rk->row, Rk->col, Rk_rs, globals);
+    }
 
-    /* a bit slower but correct: */
-    fetch_reg_stats(Rk->row, Rk->col, Rk_rs, globals);
-
     return 1;
 }
 
@@ -1487,7 +1483,7 @@
     struct reg_stats *rs_found;
     
     if (rs->id <= 0)
-	G_fatal_error("Invalid region id %d", rs->id);
+	G_fatal_error("fetch_reg_stats(): invalid region id %d", rs->id);
 
     if ((rs_found = rgtree_find(globals->reg_tree, rs)) != NULL) {
 

Modified: grass/trunk/imagery/i.segment/open_files.c
===================================================================
--- grass/trunk/imagery/i.segment/open_files.c	2013-07-21 08:38:51 UTC (rev 57236)
+++ grass/trunk/imagery/i.segment/open_files.c	2013-07-21 08:54:51 UTC (rev 57237)
@@ -64,7 +64,7 @@
     globals->nbands = Ref.nfiles;
 
     for (n = 0; n < Ref.nfiles; n++) {
-	/* returns -1 on error, 2 on empty range, quiting either way. */
+	/* returns -1 on error, 2 on empty range, quitting either way. */
 	if (Rast_read_fp_range(Ref.file[n].name, Ref.file[n].mapset, &fp_range[n]) != 1)
 	    G_fatal_error(_("No min/max found in raster map <%s>"),
 			  Ref.file[n].name);
@@ -114,6 +114,7 @@
 	    }
 	}
     }
+    G_verbose_message(_("Non-NULL cells: %ld"), globals->notnullcells);
     if (globals->notnullcells < 2)
 	G_fatal_error(_("Insufficient number of non-NULL cells in current region"));
 
@@ -544,7 +545,7 @@
 		sizeof(CELL) * 4 * srows * scols);
     }
     else {
-	/* input bands, segment ids, bounds map */
+	/* input bands, segment ids */
 	nseg = (1024. * 1024. * segs_mb) /
 	       (sizeof(DCELL) * globals->nbands * srows * scols + 
 		sizeof(CELL) * 2 * srows * scols);



More information about the grass-commit mailing list