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

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Jun 8 09:26:07 PDT 2016


Author: hao2309
Date: 2016-06-08 09:26:07 -0700 (Wed, 08 Jun 2016)
New Revision: 68638

Modified:
   sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c
Log:
move stand-alone code to sub folder and get start on mean-shift.c

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c	2016-06-08 14:26:50 UTC (rev 68637)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c	2016-06-08 16:26:07 UTC (rev 68638)
@@ -39,7 +39,8 @@
 
 	    if (!(FLAG_GET(globals->null_flag, row, col))) {
 		Segment_get(&globals->rid_seg, (void *) &rid, row, col);
-
+		
+		
 		if (rid > 0) {
 		    outbuf[col] = rid;
 		    if (maxid < rid)
@@ -64,158 +65,158 @@
     Rast_command_history(&hist);
     Rast_write_history(globals->out_name, &hist);
 
-    /* write goodness of fit */
-    if (globals->out_band) {
-	int mean_fd;
-	FCELL *meanbuf;
-	double thresh, maxdev, sim, mingood;
-	struct ngbr_stats Ri, Rk;
-	struct Ref Ref;		/* group reference list */
-	DCELL **inbuf;		/* buffers to store lines from each of the imagery group rasters */
-	int n, *in_fd;
-	struct FPRange *fp_range;	/* min/max values of each input raster */
-	DCELL *min, *max;
+    // /* write goodness of fit */
+    // if (globals->out_band) {
+	// int mean_fd;
+	// FCELL *meanbuf;
+	// double thresh, maxdev, sim, mingood;
+	// struct ngbr_stats Ri, Rk;
+	// struct Ref Ref;		/* group reference list */
+	// DCELL **inbuf;		/* buffers to store lines from each of the imagery group rasters */
+	// int n, *in_fd;
+	// struct FPRange *fp_range;	/* min/max values of each input raster */
+	// DCELL *min, *max;
 
-	mean_fd = Rast_open_new(globals->out_band, FCELL_TYPE);
-	meanbuf = Rast_allocate_f_buf();
+	// mean_fd = Rast_open_new(globals->out_band, FCELL_TYPE);
+	// meanbuf = Rast_allocate_f_buf();
 
-	/* goodness of fit for each cell: 1 = good fit, 0 = bad fit */
-	/* similarity of each cell to region mean
-	 * max possible difference: globals->threshold
-	 * if similarity < globals->alpha * globals->alpha * globals->threshold
-	 * 1
-	 * else 
-	 * (similarity - globals->alpha * globals->alpha * globals->threshold) /
-	 * (globals->threshold * (1 - globals->alpha * globals->alpha) */
+	// /* goodness of fit for each cell: 1 = good fit, 0 = bad fit */
+	// /* similarity of each cell to region mean
+	 // * max possible difference: globals->threshold
+	 // * if similarity < globals->alpha * globals->alpha * globals->threshold
+	 // * 1
+	 // * else 
+	 // * (similarity - globals->alpha * globals->alpha * globals->threshold) /
+	 // * (globals->threshold * (1 - globals->alpha * globals->alpha) */
 
-	thresh = globals->alpha * globals->alpha * globals->max_diff;
-	maxdev = globals->max_diff * (1 - globals->alpha * globals->alpha);
-	mingood = 1;
+	// thresh = globals->alpha * globals->alpha * globals->max_diff;
+	// maxdev = globals->max_diff * (1 - globals->alpha * globals->alpha);
+	// mingood = 1;
 
-	/* open input bands */
-	if (!I_get_group_ref(globals->image_group, &Ref))
-	    G_fatal_error(_("Group <%s> not found in the current mapset"),
-			  globals->image_group);
-	if (Ref.nfiles <= 0)
-	    G_fatal_error(_("Group <%s> contains no raster maps"),
-			  globals->image_group);
+	// /* open input bands */
+	// if (!I_get_group_ref(globals->image_group, &Ref))
+	    // G_fatal_error(_("Group <%s> not found in the current mapset"),
+			  // globals->image_group);
+	// if (Ref.nfiles <= 0)
+	    // G_fatal_error(_("Group <%s> contains no raster maps"),
+			  // globals->image_group);
 
-	in_fd = G_malloc(Ref.nfiles * sizeof(int));
-	inbuf = (DCELL **) G_malloc(Ref.nfiles * sizeof(DCELL *));
-	fp_range = G_malloc(Ref.nfiles * sizeof(struct FPRange));
-	min = G_malloc(Ref.nfiles * sizeof(DCELL));
-	max = G_malloc(Ref.nfiles * sizeof(DCELL));
+	// in_fd = G_malloc(Ref.nfiles * sizeof(int));
+	// inbuf = (DCELL **) G_malloc(Ref.nfiles * sizeof(DCELL *));
+	// fp_range = G_malloc(Ref.nfiles * sizeof(struct FPRange));
+	// min = G_malloc(Ref.nfiles * sizeof(DCELL));
+	// max = G_malloc(Ref.nfiles * sizeof(DCELL));
 
-	G_debug(1, "Opening input rasters...");
-	for (n = 0; n < Ref.nfiles; n++) {
-	    inbuf[n] = Rast_allocate_d_buf();
-	    in_fd[n] = Rast_open_old(Ref.file[n].name, Ref.file[n].mapset);
+	// G_debug(1, "Opening input rasters...");
+	// for (n = 0; n < Ref.nfiles; n++) {
+	    // inbuf[n] = Rast_allocate_d_buf();
+	    // in_fd[n] = Rast_open_old(Ref.file[n].name, Ref.file[n].mapset);
 
-	    /* 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);
-	    Rast_get_fp_range_min_max(&(fp_range[n]), &min[n], &max[n]);
+	    // /* 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);
+	    // Rast_get_fp_range_min_max(&(fp_range[n]), &min[n], &max[n]);
 
-	    G_debug(1, "Range for layer %d: min = %f, max = %f",
-			n, min[n], max[n]);
-	}
+	    // G_debug(1, "Range for layer %d: min = %f, max = %f",
+			// n, min[n], max[n]);
+	// }
 
-	G_message(_("Writing out goodness of fit"));
-	for (row = 0; row < globals->nrows; row++) {
+	// G_message(_("Writing out goodness of fit"));
+	// for (row = 0; row < globals->nrows; row++) {
 
-	    G_percent(row, globals->nrows, 9);
+	    // G_percent(row, globals->nrows, 9);
 
-	    Rast_set_f_null_value(meanbuf, globals->ncols);
+	    // Rast_set_f_null_value(meanbuf, globals->ncols);
 
-	    for (n = 0; n < Ref.nfiles; n++) {
-		Rast_get_d_row(in_fd[n], inbuf[n], row);
-	    }
+	    // for (n = 0; n < Ref.nfiles; n++) {
+		// Rast_get_d_row(in_fd[n], inbuf[n], row);
+	    // }
 
-	    for (col = 0; col < globals->ncols; col++) {
+	    // for (col = 0; col < globals->ncols; col++) {
 
-		if (!(FLAG_GET(globals->null_flag, row, col))) {
+		// if (!(FLAG_GET(globals->null_flag, row, col))) {
 		    
-		    Segment_get(&globals->rid_seg, (void *) &rid, row, col);
+		    // Segment_get(&globals->rid_seg, (void *) &rid, row, col);
 
-		    if (rid > 0) {
+		    // if (rid > 0) {
 			
-			Ri.row = Rk.row = row;
-			Ri.col = Rk.col = col;
+			// Ri.row = Rk.row = row;
+			// Ri.col = Rk.col = col;
 
-			/* get values for Ri = this region */
-			globals->rs.id = rid;
-			fetch_reg_stats(row, col, &globals->rs, globals);
-			Ri.mean = globals->rs.mean;
-			Ri.count = globals->rs.count; 
+			// /* get values for Ri = this region */
+			// globals->rs.id = rid;
+			// fetch_reg_stats(row, col, &globals->rs, globals);
+			// Ri.mean = globals->rs.mean;
+			// Ri.count = globals->rs.count; 
 
-			sim = 0.;
-			/* region consists of more than one cell */
-			if (Ri.count > 1) {
+			// sim = 0.;
+			// /* region consists of more than one cell */
+			// if (Ri.count > 1) {
 
-			    /* get values for Rk = this cell */
-			    for (n = 0; n < Ref.nfiles; n++) {
-				if (globals->weighted == FALSE)
-				    /* scaled version */
-				    globals->second_val[n] = (inbuf[n][col] - min[n]) / (max[n] - min[n]);
-				else
-				    globals->second_val[n] = inbuf[n][col];
-			    }
+			    // /* get values for Rk = this cell */
+			    // for (n = 0; n < Ref.nfiles; n++) {
+				// if (globals->weighted == FALSE)
+				    // /* scaled version */
+				    // globals->second_val[n] = (inbuf[n][col] - min[n]) / (max[n] - min[n]);
+				// else
+				    // globals->second_val[n] = inbuf[n][col];
+			    // }
 
-			    Rk.mean = globals->second_val;
+			    // Rk.mean = globals->second_val;
 
-			    /* calculate similarity */
-			    sim = (*globals->calculate_similarity) (&Ri, &Rk, globals);
-			}
+			    // /* calculate similarity */
+			    // sim = (*globals->calculate_similarity) (&Ri, &Rk, globals);
+			// }
 			
-			if (0) {
-			    if (sim < thresh)
-				meanbuf[col] = 1;
-			    else {
-				sim = 1. - (sim - thresh) / maxdev;
-				meanbuf[col] = sim;
-				if (mingood > sim)
-				    mingood = sim;
-			    }
-			}
-			else {
-			    sim = 1 - sim;
-			    meanbuf[col] = sim;
-			    if (mingood > sim)
-				mingood = sim;
-			}
-		    }
-		}
-	    }
-	    Rast_put_row(mean_fd, meanbuf, FCELL_TYPE);
-	}
+			// if (0) {
+			    // if (sim < thresh)
+				// meanbuf[col] = 1;
+			    // else {
+				// sim = 1. - (sim - thresh) / maxdev;
+				// meanbuf[col] = sim;
+				// if (mingood > sim)
+				    // mingood = sim;
+			    // }
+			// }
+			// else {
+			    // sim = 1 - sim;
+			    // meanbuf[col] = sim;
+			    // if (mingood > sim)
+				// mingood = sim;
+			// }
+		    // }
+		// }
+	    // }
+	    // Rast_put_row(mean_fd, meanbuf, FCELL_TYPE);
+	// }
 
-	Rast_close(mean_fd);
+	// Rast_close(mean_fd);
 
-	Rast_init_colors(&colors);
-	Rast_make_grey_scale_fp_colors(&colors, mingood, 1);
-	Rast_write_colors(globals->out_band, G_mapset(), &colors);
+	// Rast_init_colors(&colors);
+	// Rast_make_grey_scale_fp_colors(&colors, mingood, 1);
+	// Rast_write_colors(globals->out_band, G_mapset(), &colors);
 
-	Rast_short_history(globals->out_band, "raster", &hist);
-	Rast_command_history(&hist);
-	Rast_write_history(globals->out_band, &hist);
+	// Rast_short_history(globals->out_band, "raster", &hist);
+	// Rast_command_history(&hist);
+	// Rast_write_history(globals->out_band, &hist);
 
-	G_free(meanbuf);
+	// G_free(meanbuf);
 
-	G_debug(1, "Closing input rasters...");
-	for (n = 0; n < Ref.nfiles; n++) {
-	    Rast_close(in_fd[n]);
-	    G_free(inbuf[n]);
-	}
-	G_free(inbuf);
-	G_free(in_fd);
-	G_free(fp_range);
-	G_free(min);
-	G_free(max);
-    }
+	// G_debug(1, "Closing input rasters...");
+	// for (n = 0; n < Ref.nfiles; n++) {
+	    // Rast_close(in_fd[n]);
+	    // G_free(inbuf[n]);
+	// }
+	// G_free(inbuf);
+	// G_free(in_fd);
+	// G_free(fp_range);
+	// G_free(min);
+	// G_free(max);
+    // }
 
-    /* free memory */
-    Rast_free_colors(&colors);
+    // /* free memory */
+    // Rast_free_colors(&colors);
 
     return TRUE;
 }



More information about the grass-commit mailing list