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

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Jun 19 22:52:47 PDT 2016


Author: hao2309
Date: 2016-06-19 22:52:46 -0700 (Sun, 19 Jun 2016)
New Revision: 68714

Modified:
   sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c
   sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c
Log:
added cluster function

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c	2016-06-20 04:48:35 UTC (rev 68713)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c	2016-06-20 05:52:46 UTC (rev 68714)
@@ -31,18 +31,141 @@
 }
 
 
+int clustering(struct globals *globals){
+	int row, col, n;
+	int seg_id, class_val;
+	struct ngbr_stats Rin, Rleft, Rabove;
+	CELL left_new_val, above_new_val, Rtemp;
+	double min_val, max_val, old_val, new_val; 
+	double cluster_threshold, cluster_threshold2;
+	/*******************************************************
+    *             Part 2 cluster similar values            *
+    *         Refer clump.c to cluster super-pixels        *
+    *******************************************************/
+	
+	Rleft.mean = G_malloc(globals->datasize);
+	Rin.mean = G_malloc(globals->datasize);
+	Rabove.mean = G_malloc(globals->datasize);
+	
+	G_message(_("Pass 1 of 2 for clustering"));
+	G_percent_reset();
+	class_val=0;
+	cluster_threshold = 0.05;
+	cluster_threshold2= pow((cluster_threshold * globals->max_diff),2);
+	for (row = globals->row_min; row < globals->row_max; row++) {
+	    G_percent(row - globals->row_min,
+	              globals->row_max - globals->row_min, 4);
+	    for (col = globals->col_min; col < globals->col_max; col++) {
+		if (!(FLAG_GET(globals->candidate_flag, row, col)))
+		    continue;
+		old_val=new_val=0;
+		/* get the current pixel value */
+		Segment_get(&globals->bands_seg2, (void *)Rin.mean, row, col);
+		/* get left pixel value */
+		if (col == globals->col_min && row == globals->row_min){
+			class_val+=1;
+			seg_id=class_val;
+		}
+		else if (row == globals->row_min){
+			/* compare with left value and set if same cluster */
+			Segment_get(&globals->bands_seg2, (void *)Rleft.mean, row, col-1);
+			if ((globals->calculate_similarity)(&Rin, &Rleft, globals) <= cluster_threshold2){
+				G_debug(1, "row: %d, col: %d, left_similarity is: %f",row,col,(globals->calculate_similarity)(&Rin, &Rleft, globals));
+				Segment_get(&globals->rid_seg, (void *)&left_new_val, row, col-1);
+				seg_id = left_new_val;
+			}
+			/* end compare left */
+			else{
+				class_val+=1;
+				seg_id=class_val;
+			}
+		}
+		else if(col == globals->col_min){
+			/* compare with above value and set if same cluster */
+			Segment_get(&globals->bands_seg2, (void *)Rabove.mean, row-1, col);
+			if ((globals->calculate_similarity)(&Rin, &Rabove, globals) <= cluster_threshold2){
+				G_debug(1, "row: %d, col: %d, above_similarity is: %f",row,col,(globals->calculate_similarity)(&Rin, &Rabove, globals));
+				Segment_get(&globals->rid_seg, (void *)&above_new_val, row-1, col);
+				seg_id = above_new_val;
+			}
+			/* end compare above */
+			else{
+				class_val+=1;
+				seg_id=class_val;
+			}
+		}
+		else{
+			/* compare with left value then above value */
+			Segment_get(&globals->bands_seg2, (void *)Rleft.mean, row, col-1);
+			Segment_get(&globals->bands_seg2, (void *)Rabove.mean, row-1, col);
+			if ((globals->calculate_similarity)(&Rin, &Rleft, globals) <= cluster_threshold2){
+				G_debug(1, "row: %d, col: %d, left_similarity is: %f",row,col,(globals->calculate_similarity)(&Rin, &Rleft, globals));
+				Segment_get(&globals->rid_seg, (void *)&left_new_val, row, col-1);
+				old_val =seg_id = left_new_val;
+			}
+			if((globals->calculate_similarity)(&Rin, &Rabove, globals) <= cluster_threshold2){
+				G_debug(1, "row: %d, col: %d, above_similarity is: %f",row,col,(globals->calculate_similarity)(&Rin, &Rabove, globals));
+				Segment_get(&globals->rid_seg, (void *)&above_new_val, row-1, col);
+				seg_id = above_new_val;
+				if (old_val == 0){
+					old_val = above_new_val;
+				}
+				else{
+					new_val =  above_new_val;
+					if (old_val != new_val){
+					/* conflict! preserve NEW clump ID and change OLD clump ID.
+					* Must go back to the left in the current row and to the right
+					* in the previous row to change all the clump values as well.
+					*/
+					for(n = globals->col_min+1; n< col; n++){
+						Segment_get(&globals->rid_seg, (void *)&Rtemp, row, n);
+						if (Rtemp==old_val){
+							Segment_put(&globals->rid_seg, &new_val, row, n);
+						}
+					}
+					// /* right of previous row from col + 1 to ncols */
+					for(n = col+1; n< globals->col_max; n++){
+						Segment_get(&globals->rid_seg, (void *)&Rtemp, row-1, n);
+						if (Rtemp==old_val){
+							Segment_put(&globals->rid_seg, &new_val, row-1, n);
+						}
+					}
+				}
+				}
+			}
+			if (new_val ==0 && old_val ==0){
+				class_val+=1;
+				seg_id = class_val;
+			}
+			Segment_put(&globals->rid_seg, &seg_id, row, col);
+		}
+		
+		
+		G_debug(1, "row: %d, col: %d, Rin is: %f",row,col,Rin.mean[0]);
+		
+		G_debug(1, "row: %d, col: %d, seg_id is: %d",row,col,seg_id);
+		// G_debug(1, "row: %d, col: %d, cluster_threshold * globals->max_diff is: %f",row,col,pow((cluster_threshold * globals->max_diff),2));
+		/* compare with above value and set if same cluster */
+		Segment_put(&globals->rid_seg, &seg_id, row, col);
+		}
+	}
+
+	
+	return TRUE;
+}
+
+
+
 int mean_shift(struct globals *globals)
 {
-    int row, col, t;
+    int row, col, t, n1, n2;
     int n_changes, window_size;
 	int window_row_north, window_row_south, window_col_west,window_col_east, current_window_row, current_window_col;
-    double alpha2, cluster_threshold,cluster_threshold2;
+    double alpha2;
     struct ngbr_stats Rin, Rout, Rwin;
-	struct ngbr_stats Rleft, Rabove;
     double weight, sum_of_weights, diff2;
-	int seg_id, class_val;
-	CELL left_new_val, above_new_val;
-	double min_val, max_val; 
+	SEGMENT *bands_tmp;
+	
 
 	/* G_fatal_error(_("Mean shift is not yet implemented"));
     return FALSE; */
@@ -50,8 +173,6 @@
     Rin.mean = G_malloc(globals->datasize);
 	Rwin.mean = G_malloc(globals->datasize);
     Rout.mean = G_malloc(globals->datasize);
-	Rleft.mean = G_malloc(globals->datasize);
-	Rabove.mean = G_malloc(globals->datasize);
 
     /* TODO: need another segment structure holding output
      * initially, input and output are original band values */
@@ -78,7 +199,7 @@
 	G_message(_("Processing pass %d..."), ++t);
 	
 	/* for mean shift, output of the previous iteration becomes input of the next iteration */
-	SEGMENT *bands_tmp;
+
 	bands_tmp = globals->bands_in;
 	globals->bands_in = globals->bands_out;
 	globals->bands_out = bands_tmp;
@@ -164,7 +285,7 @@
 			}/* end of moving window col */
 		}/* end of moving window row */
 		
-		int n1 = globals->nbands - 1;
+		n1 = globals->nbands - 1;
 		do{
 			Rout.mean[n1] /=sum_of_weights;
 		} while(n1--);
@@ -182,7 +303,7 @@
 		    n_changes++;
 		
 		/* reset Rout to zero to prepare next iteration */
-		int n2 = globals->nbands - 1;
+		n2 = globals->nbands - 1;
 		do{
 			Rout.mean[n2] =0;
 		} while(n2--);
@@ -196,85 +317,9 @@
     else
 	G_message(_("Mean shift converged after %d iterations"), t);
     
-	/*******************************************************
-    *             Part 2 cluster similar values            *
-    *         Refer clump.c to cluster super-pixels        *
-    *******************************************************/
-	G_message(_("Pass 1 of 2 for clustering"));
-	G_percent_reset();
-	class_val=0;
-	cluster_threshold = 0.1;
-	cluster_threshold2= pow((cluster_threshold * globals->max_diff),2);
-	for (row = globals->row_min; row < globals->row_max; row++) {
-	    G_percent(row - globals->row_min,
-	              globals->row_max - globals->row_min, 4);
-	    for (col = globals->col_min; col < globals->col_max; col++) {
-		if (!(FLAG_GET(globals->candidate_flag, row, col)))
-		    continue;
-		
-		/* get the current pixel value */
-		Segment_get(&globals->bands_seg2, (void *)Rin.mean, row, col);
-		/* get left pixel value */
-		if (col == globals->col_min && row == globals->row_min){
-			seg_id=class_val;
-		}
-		else if (row == globals->row_min){
-			/* compare with left value and set if same cluster */
-			Segment_get(&globals->bands_seg2, (void *)Rleft.mean, row, col-1);
-			if ((globals->calculate_similarity)(&Rin, &Rleft, globals) <= cluster_threshold2){
-				G_debug(1, "row: %d, col: %d, left_similarity is: %f",row,col,(globals->calculate_similarity)(&Rin, &Rleft, globals));
-				Segment_get(&globals->rid_seg, (void *)&left_new_val, row, col-1);
-				seg_id = left_new_val;
-			}
-			/* end compare left */
-			else{
-				class_val+=1;
-				seg_id=class_val;
-			}
-		}
-		else if(col == globals->col_min){
-			/* compare with above value and set if same cluster */
-			Segment_get(&globals->bands_seg2, (void *)Rabove.mean, row-1, col);
-			if ((globals->calculate_similarity)(&Rin, &Rabove, globals) <= cluster_threshold2){
-				G_debug(1, "row: %d, col: %d, above_similarity is: %f",row,col,(globals->calculate_similarity)(&Rin, &Rabove, globals));
-				Segment_get(&globals->rid_seg, (void *)&above_new_val, row-1, col);
-				seg_id = above_new_val;
-			}
-			/* end compare above */
-			else{
-				class_val+=1;
-				seg_id=class_val;
-			}
-		}
-		else{
-			/* compare with left value then above value */
-			Segment_get(&globals->bands_seg2, (void *)Rleft.mean, row, col-1);
-			Segment_get(&globals->bands_seg2, (void *)Rabove.mean, row-1, col);
-			if ((globals->calculate_similarity)(&Rin, &Rleft, globals) <= cluster_threshold2 && (globals->calculate_similarity)(&Rin, &Rleft, globals) < (globals->calculate_similarity)(&Rin, &Rabove, globals)){
-				G_debug(1, "row: %d, col: %d, left_similarity is: %f",row,col,(globals->calculate_similarity)(&Rin, &Rleft, globals));
-				Segment_get(&globals->rid_seg, (void *)&left_new_val, row, col-1);
-				seg_id = left_new_val;
-			}
-			else if((globals->calculate_similarity)(&Rin, &Rabove, globals) <= cluster_threshold2){
-				G_debug(1, "row: %d, col: %d, above_similarity is: %f",row,col,(globals->calculate_similarity)(&Rin, &Rabove, globals));
-				Segment_get(&globals->rid_seg, (void *)&above_new_val, row-1, col);
-				seg_id = above_new_val;
-			}
-			else{
-				class_val+=1;
-				seg_id=class_val;
-			}
-		}
-		
-		G_debug(1, "row: %d, col: %d, Rin is: %f",row,col,Rin.mean[0]);
-		
-		G_debug(1, "row: %d, col: %d, seg_id is: %d",row,col,seg_id);
-		// G_debug(1, "row: %d, col: %d, cluster_threshold * globals->max_diff is: %f",row,col,pow((cluster_threshold * globals->max_diff),2));
-		/* compare with above value and set if same cluster */
-		Segment_put(&globals->rid_seg, &seg_id, row, col);
-		}
-	}
-
+	
+	if (clustering(globals) != TRUE)
+	G_fatal_error(_("Error in clustering segments"));
     /*******************************************************
     *         Part 3 identify connected components         *
     *    Merge small super-pixel to bigger super-pixels    *
@@ -284,5 +329,3 @@
     return TRUE;
 }
 
-
-

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c	2016-06-20 04:48:35 UTC (rev 68713)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/write_output.c	2016-06-20 05:52:46 UTC (rev 68714)
@@ -13,6 +13,62 @@
  * to try and keep the code more clear.  Need to see if this raster makes stats processing easier?  Or IFDEF it out?
  */
 
+ int write_ms_seg2(struct globals *globals)
+ {
+	
+	int *seg2_fd, row, col, n;
+	DCELL **outbuf2, *seg2;
+	struct ngbr_stats;
+	
+	seg2_fd = G_malloc(globals->nbands * sizeof(int));
+	outbuf2 = (DCELL **) G_malloc(globals->nbands * sizeof(DCELL *));
+	seg2 = G_malloc(sizeof(DCELL) * globals->nbands);
+	
+	for (n=0;n<globals->nbands; n++){
+	outbuf2[n] = Rast_allocate_d_buf();
+	}
+	
+    G_debug(1, "preparing shifted value output raster");
+    /* open output raster map */
+	for (n=0;n<globals->nbands; n++){
+    seg2_fd[n] = Rast_open_new(globals->ms_bands_out, DCELL_TYPE);
+	}
+    G_debug(1, "start data transfer from seg2 file to raster");
+
+    G_message(_("Writing out seg2 ..."));
+    for (row = 0; row < globals->nrows; row++) {
+
+	G_percent(row, globals->nrows, 9);
+	
+	for (n=0;n<globals->nbands; n++){
+	Rast_set_d_null_value(outbuf2[n], globals->ncols);
+	}
+
+	for (col = 0; col < globals->ncols; col++){
+	    if (!(FLAG_GET(globals->null_flag, row, col))) {
+
+		Segment_get(&globals->bands_seg2, (void *)seg2, row, col);
+			for (n=0;n<globals->nbands; n++){
+				outbuf2[n][col] = seg2[n];
+			}
+	    }
+	}
+
+	for (n=0;n<globals->nbands; n++){
+	Rast_put_row(seg2_fd[n], outbuf2[n], DCELL_TYPE);
+	}
+    }
+    G_percent(1, 1, 1);
+    
+    /* close and save segment id file */
+	for (n=0;n<globals->nbands; n++){
+    Rast_close(seg2_fd[n]);
+	G_free(outbuf2[n]);
+	}
+	return TRUE;
+ }
+ 
+ 
 int write_output(struct globals *globals)
 {
     int out_fd, row, col, maxid;
@@ -59,40 +115,12 @@
 
 	/* write out the shifted mean values(seg2) */
 	if (globals->method == ORM_MS){
-	int seg2_fd;
-	DCELL *outbuf2, seg2;
-	outbuf2 = Rast_allocate_d_buf();
-	
-    G_debug(1, "preparing shifted value output raster");
-    /* open output raster map */
-    seg2_fd = Rast_open_new(globals->ms_bands_out, DCELL_TYPE);
-
-    G_debug(1, "start data transfer from seg2 file to raster");
-
-    G_message(_("Writing out seg2 ..."));
-    maxid = 0;
-    for (row = 0; row < globals->nrows; row++) {
-
-	G_percent(row, globals->nrows, 9);
-
-	Rast_set_d_null_value(outbuf2, globals->ncols);
-	for (col = 0; col < globals->ncols; col++) {
-
-	    if (!(FLAG_GET(globals->null_flag, row, col))) {
-		Segment_get(&globals->bands_seg2, (void *) &seg2, row, col);
-			
-		    outbuf2[col] = seg2;
-
-	    }
+		if(write_ms_seg2(globals) != TRUE)
+		{
+			G_message(_("Unable to write out shifted bands value for seg2"));
+		}
+		
 	}
-	Rast_put_row(seg2_fd, outbuf2, DCELL_TYPE);
-    }
-    G_percent(1, 1, 1);
-    
-    /* close and save segment id file */
-    Rast_close(seg2_fd);
-    G_free(outbuf2);
-	}
 	
     /* set colors */
     Rast_init_colors(&colors);



More information about the grass-commit mailing list