[GRASS-SVN] r68952 - sandbox/bo/i.segment.gsoc2016/i.segment
svn_grass at osgeo.org
svn_grass at osgeo.org
Wed Jul 13 13:47:27 PDT 2016
Author: hao2309
Date: 2016-07-13 13:47:26 -0700 (Wed, 13 Jul 2016)
New Revision: 68952
Modified:
sandbox/bo/i.segment.gsoc2016/i.segment/iseg.h
sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c
sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c
sandbox/bo/i.segment.gsoc2016/i.segment/region_growing.c
Log:
added merge smaller region function
Modified: sandbox/bo/i.segment.gsoc2016/i.segment/iseg.h
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/iseg.h 2016-07-12 19:29:37 UTC (rev 68951)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/iseg.h 2016-07-13 20:47:26 UTC (rev 68952)
@@ -150,7 +150,7 @@
int rclist_drop(struct rclist *, struct rc *);
void rclist_destroy(struct rclist *);
-
/* write_output.c */
int write_output(struct globals *);
int close_files(struct globals *);
+
Modified: sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c 2016-07-12 19:29:37 UTC (rev 68951)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/mean_shift.c 2016-07-13 20:47:26 UTC (rev 68952)
@@ -16,6 +16,18 @@
#define INCR 1024
+
+/* extern functions */
+extern int merge_regions(struct ngbr_stats *, struct reg_stats *, /* Ri */
+ struct ngbr_stats *, struct reg_stats *, /* Rk */
+ int,
+ struct globals *);
+extern int set_candidate_flag(struct ngbr_stats *, int , struct globals *);
+extern int find_best_neighbor(struct ngbr_stats *, struct reg_stats *,
+ struct NB_TREE *, struct ngbr_stats *,
+ struct reg_stats *, double *, int,
+ struct globals *);
+
/* standard gauss funtion:
* a * exp(-(x - m)^2 / (2 * stddev^2)
* a is not needed because the sum of weights is calculated for each
@@ -36,11 +48,11 @@
int clump(struct globals *globals)
{
- register int row, col,nin,diag;
+ register int row, col,nin;
register int i, n;
/* input */
DCELL **prev_in, **cur_in, **temp_in;
- int bcol;
+ int bcol, diag;
double threshold,thresh2;
struct ngbr_stats Rin, Rcur, Rabove, Rleft, Rdiag;
/* output */
@@ -388,6 +400,137 @@
return TRUE;
}
+
+int merge_small(struct globals *globals){
+ register int row, col;
+ register int i, n;
+ double alpha2, Ri_similarity;
+ int n_merges;
+ struct ngbr_stats Ri,
+ Rk,
+ Rk_bestn, /* Rk's best neighbor */
+ *next;
+ struct NB_TREE *Ri_ngbrs, *Rk_ngbrs;
+ struct reg_stats Ri_rs, Rk_rs, Rk_bestn_rs;
+ int Ri_nn, Rk_nn; /* number of neighbors for Ri/Rk */
+
+
+ /* init neighbor stats */
+ Ri.mean = G_malloc(globals->datasize);
+ Rk.mean = G_malloc(globals->datasize);
+ Rk_bestn.mean = G_malloc(globals->datasize);
+
+ Ri_ngbrs = nbtree_create(globals->nbands, globals->datasize);
+ Rk_ngbrs = nbtree_create(globals->nbands, globals->datasize);
+
+ /* init region stats */
+ Ri_rs.mean = G_malloc(globals->datasize);
+ Ri_rs.sum = G_malloc(globals->datasize);
+ Rk_rs.mean = G_malloc(globals->datasize);
+ Rk_rs.sum = G_malloc(globals->datasize);
+ Rk_bestn_rs.mean = G_malloc(globals->datasize);
+ Rk_bestn_rs.sum = G_malloc(globals->datasize);
+ /* ****************************************************************************************** */
+ /* final pass, ignore threshold and force a merge for small segments with their best neighbor */
+ /* ****************************************************************************************** */
+
+ if (globals->min_segment_size > 1) {
+ G_message(_("Merging segments smaller than %d cells..."), globals->min_segment_size);
+
+ alpha2 = globals->alpha * globals->alpha;
+
+ flag_clear_all(globals->candidate_flag);
+
+ n_merges = 0;
+ globals->candidate_count = 0;
+
+ /* Set candidate flag to true/1 for all non-NULL cells */
+ for (row = globals->row_min; row < globals->row_max; row++) {
+ for (col = globals->col_min; col < globals->col_max; col++) {
+ if (!(FLAG_GET(globals->null_flag, row, col))) {
+ FLAG_SET(globals->candidate_flag, row, col);
+
+ globals->candidate_count++;
+ }
+ }
+ }
+
+ G_debug(4, "Starting to process %ld candidate cells",
+ globals->candidate_count);
+
+ /* process candidate cells */
+ G_percent_reset();
+ 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++) {
+ int do_merge = 1;
+
+ if (!(FLAG_GET(globals->candidate_flag, row, col)))
+ continue;
+
+ Ri.row = row;
+ Ri.col = col;
+
+ /* get segment id */
+ Segment_get(&globals->rid_seg, (void *) &Ri.id, row, col);
+
+ if (Ri.id < 0)
+ continue;
+
+ Ri_rs.id = Ri.id;
+
+ /* get segment size */
+
+ fetch_reg_stats(Ri.row, Ri.col, &Ri_rs, globals);
+ memcpy(Ri.mean, Ri_rs.mean, globals->datasize);
+ Ri.count = Ri_rs.count;
+
+ if (Ri.count >= globals->min_segment_size) {
+ set_candidate_flag(&Ri, FALSE, globals);
+ do_merge = 0;
+ }
+
+ while (do_merge) {
+ do_merge = 0;
+
+ /* merge all smaller than min size */
+ if (Ri.count < globals->min_segment_size)
+ do_merge = 1;
+
+ Ri_nn = 0;
+ Ri_similarity = 2;
+
+ Rk.id = 0;
+
+ if (do_merge) {
+
+ /* find Ri's best neighbor, clear candidate flag */
+ Ri_nn = find_best_neighbor(&Ri, &Ri_rs, Ri_ngbrs,
+ &Rk, &Rk_rs,
+ &Ri_similarity, 1,
+ globals);
+ }
+ do_merge = 0;
+
+ if (Rk.id != 0) {
+ /* merge Ri with Rk */
+ /* do not clear candidate flag for Rk */
+ merge_regions(&Ri, &Ri_rs, &Rk, &Rk_rs, 0, globals);
+ n_merges++;
+
+ if (Ri.count < globals->min_segment_size)
+ do_merge = 1;
+ }
+ }
+ }
+ }
+ G_percent(1, 1, 1);
+ }
+
+ return TRUE;
+}
+
int mean_shift(struct globals *globals)
{
int row, col, t, n1, n2;
@@ -540,8 +683,6 @@
if (diff2 > alpha2)
n_changes++;
-
- // G_message(_("row: %d, col: %d, diff2 is: %f"),row,col,diff2);
}/* end col iteration */
}/* end row iteration */
}/* end while iteration */
@@ -550,14 +691,16 @@
else
G_message(_("Mean shift converged after %d iterations"), t);
-
+ /* part 2 clustering the similar pixel to segment ID */
if (clump(globals) != TRUE)
G_fatal_error(_("Error in clustering segments"));
- /*******************************************************
- * Part 3 identify connected components *
- * Merge small super-pixel to bigger super-pixels *
- *******************************************************/
+
+ /* part 3 Merge small super-pixel to bigger super-pixels */
+
+ if (merge_small(globals) != TRUE)
+ G_fatal_error(_("Error in Merging smaller segments"));
+
return TRUE;
}
Modified: sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c 2016-07-12 19:29:37 UTC (rev 68951)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/parse_args.c 2016-07-13 20:47:26 UTC (rev 68952)
@@ -58,7 +58,7 @@
ms_suffix->required = NO;
ms_suffix->answer = "_ms";
ms_suffix->description = _("If also output the intermediam shifted-value band");
- ms_spatial_bandwidth->guisection = _("MS_Settings");
+ ms_suffix->guisection = _("MS_Settings");
ms_range_bandwidth = G_define_option();
ms_range_bandwidth->key = "ms_range_bandwidth";
@@ -89,7 +89,6 @@
min_segment_size->label = _("Minimum number of cells in a segment");
min_segment_size->description =
_("The final step will merge small segments with their best neighbor");
- min_segment_size->guisection = _("RG_Settings");
#ifdef _OR_SHAPE_
radio_weight = G_define_option();
Modified: sandbox/bo/i.segment.gsoc2016/i.segment/region_growing.c
===================================================================
--- sandbox/bo/i.segment.gsoc2016/i.segment/region_growing.c 2016-07-12 19:29:37 UTC (rev 68951)
+++ sandbox/bo/i.segment.gsoc2016/i.segment/region_growing.c 2016-07-13 20:47:26 UTC (rev 68952)
@@ -26,7 +26,7 @@
#define MIN(a,b) ( ((a) < (b)) ? (a) : (b) )
/* internal functions */
-static int merge_regions(struct ngbr_stats *, struct reg_stats *, /* Ri */
+int merge_regions(struct ngbr_stats *, struct reg_stats *, /* Ri */
struct ngbr_stats *, struct reg_stats *, /* Rk */
int,
struct globals *);
@@ -37,8 +37,8 @@
struct ngbr_stats *, /* Ri's best neighbor */
struct reg_stats *,
struct globals *);
-static int set_candidate_flag(struct ngbr_stats *, int , struct globals *);
-static int find_best_neighbor(struct ngbr_stats *, struct reg_stats *,
+int set_candidate_flag(struct ngbr_stats *, int , struct globals *);
+int find_best_neighbor(struct ngbr_stats *, struct reg_stats *,
struct NB_TREE *, struct ngbr_stats *,
struct reg_stats *, double *, int,
struct globals *);
@@ -570,7 +570,7 @@
}
-static int find_best_neighbor(struct ngbr_stats *Ri,
+int find_best_neighbor(struct ngbr_stats *Ri,
struct reg_stats *Ri_rs,
struct NB_TREE *Ri_ngbrs,
struct ngbr_stats *Rk,
@@ -1019,7 +1019,7 @@
return count;
}
-static int merge_regions(struct ngbr_stats *Ri, struct reg_stats *Ri_rs,
+int merge_regions(struct ngbr_stats *Ri, struct reg_stats *Ri_rs,
struct ngbr_stats *Rk, struct reg_stats *Rk_rs,
int do_cand, struct globals *globals)
{
@@ -1224,7 +1224,7 @@
return TRUE;
}
-static int set_candidate_flag(struct ngbr_stats *head, int value, struct globals *globals)
+int set_candidate_flag(struct ngbr_stats *head, int value, struct globals *globals)
{
int n, R_id;
struct rc next, ngbr_rc;
More information about the grass-commit
mailing list