[GRASS-SVN] r68892 - sandbox/bo/i.segment.gsoc2016/i.segment

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jul 7 19:28:50 PDT 2016


Author: hao2309
Date: 2016-07-07 19:28:49 -0700 (Thu, 07 Jul 2016)
New Revision: 68892

Modified:
   sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c
   sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c
Log:
added check for null val, remove cmp_cells and etc

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c	2016-07-07 17:36:10 UTC (rev 68891)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c	2016-07-08 02:28:49 UTC (rev 68892)
@@ -33,28 +33,7 @@
     return exp(-diff2 / (2 * var));
 }
 
-static int cmp_cells(DCELL **a, int acol, DCELL **b, int bcol,
-                     DCELL *rng, int n, double threshold2)
-{
-    int i;
-    double diff, diff2;
 
-    diff2 = 0;
-    for (i = 0; i < n; i++) {
-	if (Rast_is_d_null_value(&b[i][bcol]))
-	    return 0;
-	diff = a[i][acol] - b[i][bcol];
-	/* normalize with the band's range */
-	if (rng[i])
-	    diff /= rng[i];
-	diff2 += diff * diff;
-    }
-    /* normalize difference to the range [0, 1] */
-    diff2 /= n;
-    
-    return (diff2 <= threshold2);
-}
-
 int clump(struct globals *globals)
 {
     register int row, col,nin,diag;
@@ -62,9 +41,8 @@
     /* input */
     DCELL **prev_in, **cur_in, **temp_in;
     int bcol;
-    DCELL *rng, maxdiff;
     double threshold,thresh2;
-	struct ngbr_stats Rin, Rabove, Rleft;
+	struct ngbr_stats Rin, Rcur, Rabove, Rleft, Rdiag;
     /* output */
     CELL OLD, NEW;
     CELL *temp_clump, *out_cell, *out_cellp;
@@ -84,10 +62,20 @@
     G_message(_("%d-band clumping with threshold %g"), nin, threshold);
 
 	Rin.mean = G_malloc(globals->datasize);
+	Rcur.mean = G_malloc(globals->datasize);
+	Rabove.mean = G_malloc(globals->datasize);
+	Rleft.mean = G_malloc(globals->datasize);
+	Rdiag.mean = G_malloc(globals->datasize);
     nrows = globals->row_max;
     ncols = globals->col_max;
 	
-	diag = 0;
+	/* four pixel neighborhood or diagonal (3x3) pixel neighborhood */
+	if (globals->nn = 4){
+		diag = 0;
+	}
+	else if (globals->nn = 8){
+		diag = 1;
+	}
 	
     thresh2 = threshold * threshold;
 
@@ -101,41 +89,20 @@
     len = (ncols + 2) * sizeof(DCELL);
     prev_in = (DCELL **) G_malloc(sizeof(DCELL *) * nin); 
 	cur_in = (DCELL **) G_malloc(sizeof(DCELL *) * nin);
-    rng = G_malloc(sizeof(DCELL) * nin);
+    
 
-    maxdiff = 0;
-    for (i = 0; i < nin; i++) {
-	DCELL min, max; /* min/max values of each input raster */
+	for (i = 0; i < nin; i++) {
 	
-	max = 0;
-	min =1;
-	
-	for (row = globals->row_min; row < nrows; row++) {
-	    for (col = globals->col_min; col < ncols; col++) {
-			if (!(FLAG_GET(globals->candidate_flag, row, col)))
-				continue;
-			Segment_get(globals->bands_out, (void *)Rin.mean, row, col);
-			if (Rin.mean[i]<min)
-				min = Rin.mean[i];
-			if (Rin.mean[i]>max)
-				max = Rin.mean[i];
-		}
-	}
-	
-	rng[i] = max - min;
-	maxdiff += rng[i] * rng[i];
-
 	prev_in[i] = (DCELL *) G_malloc(len);
-	cur_in[i] = (DCELL *) G_malloc(len);
-
+	cur_in[i] = (DCELL *) G_malloc(len);	
+		
 	/* fake a previous row which is all NULL */
 	Rast_set_d_null_value(prev_in[i], ncols + 2);
 
 	/* set left and right edge to NULL */
 	Rast_set_d_null_value(&cur_in[i][0], 1);
 	Rast_set_d_null_value(&cur_in[i][ncols + 1], 1);
-    }
-    G_debug(1, "maximum possible difference: %g", maxdiff);
+	}
 
     /* allocate CELL buffers two columns larger than current window */
     len = (ncols + 2) * sizeof(CELL);
@@ -161,15 +128,18 @@
      ****************************************************/
 
     G_message(_("Pass 1 of 2..."));
+	
     for (row = 0; row < nrows; row++) {
 	G_percent(row, nrows, 2);
 		for (col = 0; col < ncols; col++){
+			
 			Segment_get(globals->bands_out, (void *)Rin.mean, row, col);
 			for (i = 0; i < nin; i++) {
 				cur_in[i][col+1] = Rin.mean[i];
-			}
+			}  
+			
 		}
-
+	
 	for (col = 1; col <= ncols; col++) {
 	    isnull = 0;
 	    for (i = 0; i < nin; i++) {
@@ -194,7 +164,13 @@
 	    /* try to connect the current cell to an existing clump */
 	    OLD = NEW = 0;
 	    /* same clump as to the left */
-	    if (cmp_cells(cur_in, col, cur_in, col - 1, rng, nin, thresh2)) {
+		
+		/* get the left pixel to compare */
+		for (i = 0; i < nin; i++) {
+				 Rcur.mean[i] = cur_in[i][col];
+				 Rleft.mean[i] = cur_in[i][col-1];
+			}
+	    if (globals->calculate_similarity(&Rcur, &Rleft, globals) <= pow((globals->ms_range_bandwidth),2)){
 		OLD = cur_clump[col] = cur_clump[col - 1];
 	    }
 
@@ -203,7 +179,11 @@
 		temp_clump = prev_clump + col + 1;
 		bcol = col + 1;
 		do {
-		    if (cmp_cells(cur_in, col, prev_in, bcol, rng, nin, thresh2)) {
+			/* get the diag pixel to compare */
+			for (i = 0; i < nin; i++) {
+				 Rdiag.mean[i] = prev_in[i][bcol];
+			}
+		    if (globals->calculate_similarity(&Rcur, &Rdiag, globals) <= pow((globals->ms_range_bandwidth),2)){
 			cur_clump[col] = *temp_clump;
 			if (OLD == 0) {
 			    OLD = *temp_clump;
@@ -255,7 +235,11 @@
 	    }
 	    else {
 		/* check above */
-		if (cmp_cells(cur_in, col, prev_in, col, rng, nin, thresh2)) {
+		for (i = 0; i < nin; i++) {
+				 Rcur.mean[i] = cur_in[i][col];
+				 Rabove.mean[i] = prev_in[i][col];
+			}
+	    if (globals->calculate_similarity(&Rcur, &Rabove, globals) <= pow((globals->ms_range_bandwidth),2)){
 		    temp_clump = prev_clump + col;
 		    cur_clump[col] = *temp_clump;
 		    if (OLD == 0) {
@@ -398,6 +382,8 @@
 			Segment_put(&globals->rid_seg, &out_cell[col], row, col);
 		}
 	}
+	/* update the number of segments */
+	globals->n_regions = cat-1;
 	G_percent(1, 1, 1);
 
     close(cfd);
@@ -518,6 +504,9 @@
 		/* start of the moving window */
 		for (current_window_row = window_row_north;current_window_row<window_row_south;current_window_row++){
 			for (current_window_col = window_col_west;current_window_col<window_col_east;current_window_col++){
+				if (!(FLAG_GET(globals->candidate_flag, current_window_row, current_window_col))){
+					continue;
+				}
 				weight = 1;
 				/* adapt initial spatial and range bandwidths */
 				if (pow((current_window_row - row),2)+pow((current_window_col - col),2) <= pow(globals->ms_spatial_bandwidth,2)){
@@ -551,8 +540,7 @@
 
 		/* if the squared difference between old and new band values 
 		 * is larger than alpha2, then increase n_changes */
-		Segment_get(&globals->bands_seg2, (void *)Rout.mean,row, col);
-		// G_message(_("row: %d, col: %d, Rout.mean of seg2 is: %f"),row,col,Rout.mean[0]);
+
 		diff2 = (globals->calculate_similarity)(&Rin, &Rout, globals);
 		if (diff2 > alpha2)
 		    n_changes++;

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c	2016-07-07 17:36:10 UTC (rev 68891)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c	2016-07-08 02:28:49 UTC (rev 68892)
@@ -87,6 +87,7 @@
 	G_free(outbuf2[n]);
 	}
 	
+	
 	for (n=0;n<globals->nbands; n++){
 	G_asprintf(&shifted_out_name, "%s%s", Ref.file[n].name, globals->ms_suffix);
 	/* set colors */
@@ -101,6 +102,7 @@
 	}
 
 	Rast_free_colors(&colors);
+
 	G_free(shifted_out_name);
 	return TRUE;
  }



More information about the grass-commit mailing list