[GRASS-SVN] r68788 - in sandbox/bo/i.segment.gsoc2016/i.segment: . testing

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jun 27 21:23:09 PDT 2016


Author: hao2309
Date: 2016-06-27 21:23:09 -0700 (Mon, 27 Jun 2016)
New Revision: 68788

Modified:
   sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c
   sandbox/bo/i.segment.gsoc2016/i.segment/testing/test_map.ascii
Log:
modifying clustering function

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c	2016-06-27 20:53:19 UTC (rev 68787)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c	2016-06-28 04:23:09 UTC (rev 68788)
@@ -30,9 +30,94 @@
     return exp(-diff2 / (2 * var));
 }
 
+int clustering2(struct globals *globals){
+	int class_val, px_count, first_change;/* the value of class */
+	int row, col, nrows, ncols, stop_row, stop_col,edge_row, edge_col,diag;
+	struct ngbr_stats Rin, Rstop;
+	nrows = globals->row_max;
+	ncols = globals->col_max;
+	CELL temp_id;
+	double cluster_threshold,cluster_threshold2;
+	int **indicator_matrix;
+	/* the indicator matrix to mark if the pixel has been clumped */
+	
+	Rin.mean = G_malloc(globals->datasize);
+	Rstop.mean = G_malloc(globals->datasize);
+	indicator_matrix = G_malloc(nrows *  sizeof(int*));
+	diag=0;
+	
+	for (row = globals->row_min; row < nrows; row++) {
+		indicator_matrix[row] = G_malloc(ncols * sizeof(int));
+	    for (col = globals->col_min; col < ncols; col++) {
+			indicator_matrix[row][col] = 0;
+		}
+	}
+	
+	G_message(_("Pass 1 of 2 for clustering"));
+	
+	px_count=0;
+	class_val=0;
+	stop_row=0;
+	stop_col=0;
+	cluster_threshold = 0.05;
+	cluster_threshold2= pow(cluster_threshold ,2);
+	
+	while (px_count<nrows*ncols){
+		class_val+=1;
+		Segment_get(globals->bands_out, (void *)Rstop.mean, stop_row, stop_col);
+		first_change =0;/* mark the first time non-clump */
+		
+		
+		/* get the first stop row when  */
+	for (row = globals->row_min; row < nrows; row++) {
+	    G_percent(row - globals->row_min,
+	              globals->row_max - globals->row_min, 4);
+	    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);
+			// G_message(_("row: %d, col: %d, temp_id is: %d"),row,col,temp_id);
+			
+			if ((globals->calculate_similarity)(&Rin, &Rstop, globals) <= cluster_threshold2 && indicator_matrix[row][col]==0){
+				indicator_matrix[row][col]+=1;
+				
+				/* if the seg is not in the edg of the image */
+				// if (row!= globals->row_min && col!=globals->col_min && row!=nrows-1 && col!=ncols-1){
+					
+					// /* If this seg is saperate from surrounded segments */
+					// if (indicator_matrix[row+1][col]+indicator_matrix[row][col+1]+indicator_matrix[row-1][col]+indicator_matrix[row][col-1]==0){
+						// class_val+=1;
 
+						// if (diag){
+							// if (indicator_matrix[row-1][col-1]+indicator_matrix[row+1][col-1]+indicator_matrix[row-1][col+1]+indicator_matrix[row+1][col+1]==0){
+								// class_val+=1;
+							// }
+						// }
+					// }
+				// }
+				
+				Segment_put(&globals->rid_seg, &class_val, row, col);
+							
+				px_count+=1;
+			}
+			else if(first_change == 0 && indicator_matrix[row][col]==0){
+				first_change = 1;
+				stop_col = col;
+				stop_row = row;
+			}
+			}
+			
+		}
+	}
+	
+	
+	return TRUE;
+}
+
+
 int clustering(struct globals *globals){
-	int row, col, n, nin;
+	int row, col, n3, nin;
 	int diag;/* if using diag pixel */
 	int class_val;
 	struct ngbr_stats Rin, Rleft, Rabove;
@@ -56,20 +141,26 @@
 	cluster_threshold = 0.05;
 	cluster_threshold2= pow(cluster_threshold ,2);
 	G_message(_("Pass 1 of 2 for clustering"));
-	for (row = globals->row_min+1; row < globals->row_max; row++) {
+	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+1; col < globals->col_max; col++) {
+	    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_out, (void *)Rin.mean, row, col);
-			/* get left pixel value */
-			/* compare with left value and set if same cluster */
+			
+			/* get left pixel value if not the first col*/
+			if(col != globals->col_min){
 			Segment_get(globals->bands_out, (void *)Rleft.mean, row, (col-1));
+			}
 			
+			/* get above pixel value if not the first row*/
+			if(row != globals->row_min){
+			Segment_get(&globals->bands_seg2, (void *)Rabove.mean, row-1, col);
+			}
 			
 			if (col == globals->col_min && row == globals->row_min){
 			class_val+=1;
@@ -91,7 +182,6 @@
 			}
 			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);
@@ -107,18 +197,20 @@
 			if ((globals->calculate_similarity)(&Rin, &Rleft, globals) <= cluster_threshold2){
 				Segment_get(&globals->rid_seg, (void *)&left_new_val, row, col-1);
 				old_val=seg_id = left_new_val;
-			} 
+				// G_message(_("row: %d, col: %d, oldval is: %d, newval is: %d, compare to left"),row,col,old_val,new_val);
+			}
+			
 			if(diag){
-				/* check above right, center, left, in that order */
+				// /* check above right, center, left, in that order */
 			}
 			else{
 				/* 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){
 					
 					Segment_get(&globals->rid_seg, (void *)&above_new_val, row-1, col);
 					temp_clump = &above_new_val;
 					seg_id = *temp_clump;
+					// G_message(_("row: %d, col: %d, oldval is: %d, newval is: %d, compare to above"),row,col,old_val,new_val);
 					if (old_val==0){
 						old_val = *temp_clump;
 					}
@@ -129,19 +221,19 @@
 					* 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.
 					*/
-
+					// G_message(_("row: %d, col: %d, oldval is: %d, newval is: %d, conflict"),row,col,old_val,new_val);
 					/* left of the current row from 1 to col - 1 */
-					for(n = globals->col_min+1; n< col; n++){
-						Segment_get(&globals->rid_seg, (void *)temp_clump, row, n);
+					for(n3 = globals->col_min+1; n3< col; n3++){
+						Segment_get(&globals->rid_seg, (void *)temp_clump, row, n3);
 						if (*temp_clump==old_val){
-							Segment_put(&globals->rid_seg, &new_val, row, n);
+							Segment_put(&globals->rid_seg, &new_val, row, n3);
 						}
 					}
 					// /* right of previous row from col + 1 to ncols */
-					for(n = col+1; n< globals->col_max; n++){
-						Segment_get(&globals->rid_seg, (void *)temp_clump, row-1, n);
+					for(n3 = col+1; n3< globals->col_max; n3++){
+						Segment_get(&globals->rid_seg, (void *)temp_clump, row-1, n3);
 						if (*temp_clump==old_val){
-							Segment_put(&globals->rid_seg, &new_val, row-1, n);
+							Segment_put(&globals->rid_seg, &new_val, row-1, n3);
 						}
 					}
 					}
@@ -152,10 +244,11 @@
 				if (old_val ==0){
 					class_val+=1;
 					seg_id = class_val;
+					// G_message(_("row: %d, col: %d, oldval is: %d, newval is: %d, class++"),row,col,old_val,new_val);
 				}
 			}
 			
-		}
+			}
 		Segment_put(&globals->rid_seg, &seg_id, row, col);
 		}
 	}
@@ -163,7 +256,6 @@
 }
 
 
-
 int mean_shift(struct globals *globals)
 {
     int row, col, t, n1, n2;
@@ -324,14 +416,13 @@
 	G_message(_("Mean shift converged after %d iterations"), t);
     
 	
-	if (clustering(globals) != TRUE)
+	if (clustering2(globals) != TRUE)
 	G_fatal_error(_("Error in clustering segments"));
     /*******************************************************
     *         Part 3 identify connected components         *
     *    Merge small super-pixel to bigger super-pixels    *
     *******************************************************/
 	
-	
     return TRUE;
 }
 

Modified: sandbox/bo/i.segment.gsoc2016/i.segment/testing/test_map.ascii
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/testing/test_map.ascii	2016-06-27 20:53:19 UTC (rev 68787)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/testing/test_map.ascii	2016-06-28 04:23:09 UTC (rev 68788)
@@ -9,8 +9,8 @@
 103 103 103 103 103 103 106 104   
 103 147 147 153 147 156 153 104   
 107 153 153 153 153 153 153 107   
-103 153 147 96 98 153 153 104   
-107 156 153 97 96 147 153 107   
-103 153 153 147 156 153 153 101   
-103 156 153 147 147 153 153 104   
+103 153 98 96 98 153 153 104   
+107 156 153 97 148 147 153 107   
+103 153 153 147 156 96 96 101   
+103 156 153 147 147 153 97 104   
 103 103 107 104 103 106 103 107



More information about the grass-commit mailing list