region_growing.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. /* PURPOSE: Develop the image segments */
  2. /* Currently only region growing is implemented */
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <float.h>
  6. #include <math.h>
  7. #include <time.h>
  8. #include <grass/gis.h>
  9. #include <grass/glocale.h>
  10. #include <grass/raster.h>
  11. #include <grass/segment.h> /* segmentation library */
  12. #include <grass/rbtree.h> /* Red Black Tree library functions */
  13. #include "iseg.h"
  14. #define EPSILON 1.0e-8
  15. #ifdef MAX
  16. #undef MAX
  17. #endif
  18. #define MAX(a,b) ( ((a) > (b)) ? (a) : (b) )
  19. #ifdef MIN
  20. #undef MIN
  21. #endif
  22. #define MIN(a,b) ( ((a) < (b)) ? (a) : (b) )
  23. struct idlist
  24. {
  25. int *ids;
  26. int nids, nalloc;
  27. CELL cellmax;
  28. };
  29. static struct idlist idlist;
  30. /* internal functions */
  31. static int merge_regions(struct ngbr_stats *, struct reg_stats *, /* Ri */
  32. struct ngbr_stats *, struct reg_stats *, /* Rk */
  33. int,
  34. struct globals *);
  35. static int search_neighbors(struct ngbr_stats *, /* Ri */
  36. struct reg_stats *,
  37. struct NB_TREE *, /* Ri's neighbors */
  38. double *, /* highest similarity */
  39. struct ngbr_stats *, /* Ri's best neighbor */
  40. struct reg_stats *,
  41. struct globals *);
  42. static int set_candidate_flag(struct ngbr_stats *, int , struct globals *);
  43. static int find_best_neighbor(struct ngbr_stats *, struct reg_stats *,
  44. struct NB_TREE *, struct ngbr_stats *,
  45. struct reg_stats *, double *, int,
  46. struct globals *);
  47. static int calculate_reg_stats(int, int, struct reg_stats *,
  48. struct globals *);
  49. void init_free_ids(void)
  50. {
  51. idlist.nalloc = 10;
  52. idlist.nids = 0;
  53. idlist.ids = G_malloc(idlist.nalloc * sizeof(int));
  54. idlist.cellmax = ((CELL)1 << (sizeof(CELL) * 8 - 2)) - 1;
  55. idlist.cellmax += ((CELL)1 << (sizeof(CELL) * 8 - 2));
  56. return;
  57. }
  58. void add_free_id(int id)
  59. {
  60. if (id <= 0)
  61. return;
  62. if (idlist.nalloc <= idlist.nids) {
  63. idlist.nalloc = idlist.nids + 10;
  64. idlist.ids = G_realloc(idlist.ids, idlist.nalloc * sizeof(int));
  65. }
  66. idlist.ids[idlist.nids++] = id;
  67. return;
  68. }
  69. int get_free_id(struct globals *globals)
  70. {
  71. if (idlist.nids > 0) {
  72. idlist.nids--;
  73. return idlist.ids[idlist.nids];
  74. }
  75. if (globals->max_rid == idlist.cellmax)
  76. G_fatal_error(_("Too many objects: integer overflow"));
  77. globals->max_rid++;
  78. return globals->max_rid;
  79. }
  80. void free_free_ids(void)
  81. {
  82. if (idlist.nalloc) {
  83. G_free(idlist.ids);
  84. idlist.ids = NULL;
  85. idlist.nalloc = 0;
  86. idlist.nids = 0;
  87. }
  88. return;
  89. }
  90. /* function used by binary tree to compare items */
  91. static int compare_rc(const void *first, const void *second)
  92. {
  93. struct rc *a = (struct rc *)first, *b = (struct rc *)second;
  94. if (a->row < b->row)
  95. return -1;
  96. if (a->row > b->row)
  97. return 1;
  98. /* same row */
  99. if (a->col < b->col)
  100. return -1;
  101. if (a->col > b->col)
  102. return 1;
  103. /* same row and col */
  104. return 0;
  105. }
  106. static int compare_double(double first, double second)
  107. {
  108. if (first < second)
  109. return -1;
  110. return (first > second);
  111. }
  112. static int compare_sim_ngbrs(double simi, double simk, int candi, int candk,
  113. struct ngbr_stats *Ri, struct ngbr_stats *Rk)
  114. {
  115. if (simi < simk)
  116. return -1;
  117. if (simi > simk)
  118. return 1;
  119. if (Rk->count == 0 || Ri->count < Rk->count)
  120. return -1;
  121. if (Ri->count > Rk->count)
  122. return 1;
  123. if (candi && !candk)
  124. return -1;
  125. if (candk && !candi)
  126. return 1;
  127. if (Ri->row < Rk->row)
  128. return -1;
  129. if (Ri->row > Rk->row)
  130. return 1;
  131. if (Ri->col < Rk->col)
  132. return -1;
  133. return (Ri->col > Rk->col);
  134. }
  135. static int dump_Ri(struct ngbr_stats *Ri, struct reg_stats *Ri_rs, double *Ri_sim,
  136. double *Rk_sim, int *Ri_nn, int *Rk_nn, struct globals *globals)
  137. {
  138. int i;
  139. G_debug(0, "Ri, Ri_rs ID: %d, %d", Ri->id, Ri_rs->id);
  140. G_debug(0, "Ri, Ri_rs count: %d, %d", Ri->count, Ri_rs->count);
  141. for (i = 0; i < globals->nbands; i++) {
  142. G_debug(0, "Ri, Ri_rs mean %d: %g, %g", i, Ri->mean[i], Ri_rs->mean[i]);
  143. G_debug(0, "Ri_rs sum %d: %g", i, Ri_rs->sum[i]);
  144. }
  145. G_debug(0, "Ri nn: %d", *Ri_nn);
  146. if (Rk_nn)
  147. G_debug(0, "Rk nn: %d", *Rk_nn);
  148. G_debug(0, "Ri similarity: %g", *Ri_sim);
  149. if (Rk_sim)
  150. G_debug(0, "Rk similarity: %g", *Rk_sim);
  151. return 1;
  152. }
  153. int region_growing(struct globals *globals)
  154. {
  155. int row, col, t;
  156. double threshold, adjthresh, Ri_similarity, Rk_similarity;
  157. double alpha2, divisor; /* threshold parameters */
  158. int n_merges, do_merge; /* number of merges on that iteration */
  159. int pathflag; /* =1 if we didn't find mutually best neighbors, continue with Rk */
  160. int candidates_only;
  161. struct ngbr_stats Ri,
  162. Rk,
  163. Rk_bestn, /* Rk's best neighbor */
  164. *next;
  165. int Ri_nn, Rk_nn; /* number of neighbors for Ri/Rk */
  166. struct NB_TREE *Ri_ngbrs, *Rk_ngbrs;
  167. struct NB_TRAV travngbr;
  168. /* not all regions are in the tree, but we always need reg_stats for Ri and Rk */
  169. struct reg_stats Ri_rs, Rk_rs, Rk_bestn_rs;
  170. double *dp;
  171. struct NB_TREE *tmpnbtree;
  172. CELL cellmax;
  173. struct Cell_head cellhd;
  174. G_verbose_message("Running region growing algorithm");
  175. cellmax = ((CELL)1 << (sizeof(CELL) * 8 - 2)) - 1;
  176. cellmax += ((CELL)1 << (sizeof(CELL) * 8 - 2));
  177. init_free_ids();
  178. /* init neighbor stats */
  179. Ri.mean = G_malloc(globals->datasize);
  180. Rk.mean = G_malloc(globals->datasize);
  181. Rk_bestn.mean = G_malloc(globals->datasize);
  182. Ri_ngbrs = nbtree_create(globals->nbands, globals->datasize);
  183. Rk_ngbrs = nbtree_create(globals->nbands, globals->datasize);
  184. /* init region stats */
  185. Ri_rs.mean = G_malloc(globals->datasize);
  186. Ri_rs.sum = G_malloc(globals->datasize);
  187. Rk_rs.mean = G_malloc(globals->datasize);
  188. Rk_rs.sum = G_malloc(globals->datasize);
  189. Rk_bestn_rs.mean = G_malloc(globals->datasize);
  190. Rk_bestn_rs.sum = G_malloc(globals->datasize);
  191. t = 0;
  192. n_merges = 2;
  193. /* threshold calculation */
  194. alpha2 = globals->alpha * globals->alpha;
  195. threshold = alpha2;
  196. G_debug(1, "Squared threshold: %g", threshold);
  197. Rast_get_cellhd(globals->Ref.file[0].name, globals->Ref.file[0].mapset, &cellhd);
  198. divisor = cellhd.rows + cellhd.cols;
  199. /* TODO: renumber seeds */
  200. while (t < globals->end_t && n_merges > 1) {
  201. G_message(_("Processing pass %d..."), ++t);
  202. n_merges = 0;
  203. globals->candidate_count = 0;
  204. flag_clear_all(globals->candidate_flag);
  205. /* Set candidate flag to true/1 for all non-NULL cells */
  206. for (row = globals->row_min; row < globals->row_max; row++) {
  207. for (col = globals->col_min; col < globals->col_max; col++) {
  208. if (!(FLAG_GET(globals->null_flag, row, col))) {
  209. FLAG_SET(globals->candidate_flag, row, col);
  210. globals->candidate_count++;
  211. }
  212. }
  213. }
  214. G_debug(4, "Starting to process %ld candidate cells",
  215. globals->candidate_count);
  216. /*process candidate cells */
  217. G_percent_reset();
  218. for (row = globals->row_min; row < globals->row_max; row++) {
  219. G_percent(row - globals->row_min,
  220. globals->row_max - globals->row_min, 4);
  221. for (col = globals->col_min; col < globals->col_max; col++) {
  222. if (!(FLAG_GET(globals->candidate_flag, row, col)))
  223. continue;
  224. pathflag = TRUE;
  225. candidates_only = TRUE;
  226. nbtree_clear(Ri_ngbrs);
  227. nbtree_clear(Rk_ngbrs);
  228. G_debug(4, "Next starting cell: row, %d, col, %d",
  229. row, col);
  230. /* First cell in Ri is current row/col */
  231. Ri.row = row;
  232. Ri.col = col;
  233. /* get Ri's segment ID */
  234. Segment_get(&globals->rid_seg, (void *)&Ri.id, Ri.row, Ri.col);
  235. /* find segment neighbors */
  236. /* find Ri's best neighbor, clear candidate flag */
  237. Ri_similarity = 2;
  238. Ri_rs.id = Ri.id;
  239. fetch_reg_stats(Ri.row, Ri.col, &Ri_rs, globals);
  240. memcpy(Ri.mean, Ri_rs.mean, globals->datasize);
  241. Ri.count = Ri_rs.count;
  242. /* Ri is now complete */
  243. G_debug(4, "Ri is now complete");
  244. /* find best neighbor, get region stats for Rk */
  245. Ri_nn = find_best_neighbor(&Ri, &Ri_rs, Ri_ngbrs,
  246. &Rk, &Rk_rs, &Ri_similarity,
  247. 1, globals);
  248. /* Rk is now complete */
  249. G_debug(4, "Rk is now complete");
  250. if (Rk.id < 0) {
  251. /* this can only happen if the segment is surrounded by NULL data */
  252. G_debug(4, "Segment had no valid neighbors");
  253. continue;
  254. }
  255. if (compare_double(Ri_similarity, threshold) >= 0) {
  256. G_debug(4, "Best neighbor is not similar enough");
  257. continue;
  258. }
  259. if (/* !(t & 1) && */ Ri_nn == 1 &&
  260. !(FLAG_GET(globals->candidate_flag, Rk.row, Rk.col)) &&
  261. compare_double(Ri_similarity, threshold) == -1) {
  262. /* this is slow ??? */
  263. int smaller = Rk.count;
  264. if (Ri.count < Rk.count)
  265. smaller = Ri.count;
  266. /* TODO: better */
  267. adjthresh = pow(alpha2, 1. + (double) smaller / divisor);
  268. if (compare_double(Ri_similarity, adjthresh) == -1) {
  269. G_debug(4, "Ri nn == 1");
  270. if (Rk.count < 2)
  271. G_fatal_error("Rk count too low");
  272. if (Rk.count < Ri.count)
  273. G_debug(4, "Rk count lower than Ri count");
  274. merge_regions(&Ri, &Ri_rs, &Rk, &Rk_rs, 1, globals);
  275. n_merges++;
  276. }
  277. pathflag = FALSE;
  278. }
  279. while (pathflag) {
  280. pathflag = FALSE;
  281. if (Rk.count <= globals->nn || Rk.count <= globals->min_segment_size)
  282. candidates_only = FALSE;
  283. /* optional check if Rk is candidate
  284. * to prevent backwards merging */
  285. if (candidates_only &&
  286. !(FLAG_GET(globals->candidate_flag, Rk.row, Rk.col))) {
  287. Ri_similarity = 2;
  288. }
  289. candidates_only = TRUE;
  290. if (compare_double(Ri_similarity, threshold) == -1) {
  291. do_merge = 1;
  292. /* we'll have the neighbor pixel to start with. */
  293. G_debug(4, "Working with Rk");
  294. /* find Rk's best neighbor, do not clear candidate flag */
  295. Rk_similarity = Ri_similarity;
  296. Rk_bestn_rs.count = 0;
  297. /* Rk_rs is already complete */
  298. Rk_nn = find_best_neighbor(&Rk, &Rk_rs, Rk_ngbrs,
  299. &Rk_bestn,
  300. &Rk_bestn_rs,
  301. &Rk_similarity, 0,
  302. globals);
  303. /* not mutually best neighbors */
  304. if (Rk_similarity != Ri_similarity) {
  305. do_merge = 0;
  306. }
  307. /* Ri has only one neighbor, merge */
  308. if (Ri_nn == 1 && Rk_nn > 1)
  309. do_merge = 1;
  310. /* adjust threshold */
  311. if (do_merge) {
  312. int smaller = Rk.count;
  313. if (Ri.count < Rk.count)
  314. smaller = Ri.count;
  315. /* TODO: better */
  316. adjthresh = pow(alpha2, 1. + (double) smaller / divisor);
  317. do_merge = 0;
  318. if (compare_double(Ri_similarity, adjthresh) == -1) {
  319. do_merge = 1;
  320. }
  321. }
  322. if (do_merge) {
  323. G_debug(4, "merge neighbor trees");
  324. Ri_nn -= Ri_ngbrs->count;
  325. Ri_nn += (Rk_nn - Rk_ngbrs->count);
  326. globals->ns.id = Rk.id;
  327. globals->ns.row = Rk.row;
  328. globals->ns.col = Rk.col;
  329. nbtree_remove(Ri_ngbrs, &(globals->ns));
  330. nbtree_init_trav(&travngbr, Rk_ngbrs);
  331. while ((next = nbtree_traverse(&travngbr))) {
  332. if (!nbtree_find(Ri_ngbrs, next) && cmp_ngbr(next, &Ri) != 0)
  333. nbtree_insert(Ri_ngbrs, next);
  334. }
  335. nbtree_clear(Rk_ngbrs);
  336. Ri_nn += Ri_ngbrs->count;
  337. merge_regions(&Ri, &Ri_rs, &Rk, &Rk_rs, 1, globals);
  338. /* Ri is now updated, Rk no longer usable */
  339. /* made a merge, need another iteration */
  340. n_merges++;
  341. Ri_similarity = 2;
  342. Rk_similarity = 2;
  343. /* we have checked the neighbors of Ri, Rk already
  344. * use faster version of finding the best neighbor
  345. */
  346. /* use neighbor tree to find Ri's new best neighbor */
  347. search_neighbors(&Ri, &Ri_rs, Ri_ngbrs, &Ri_similarity,
  348. &Rk, &Rk_rs, globals);
  349. if (Rk.id >= 0 && Ri_nn > 0 &&
  350. compare_double(Ri_similarity, threshold) == -1) {
  351. pathflag = TRUE;
  352. /* candidates_only:
  353. * FALSE: less passes, slower, but less memory
  354. * TRUE: more passes but faster */
  355. candidates_only = TRUE;
  356. }
  357. /* else end of Ri -> Rk chain since we merged Ri and Rk
  358. * go to next row, col */
  359. }
  360. else {
  361. if (compare_double(Rk_similarity, threshold) == -1) {
  362. pathflag = TRUE;
  363. }
  364. /* test this: can it cause an infinite loop ? */
  365. if (!(FLAG_GET(globals->candidate_flag, Rk.row, Rk.col)))
  366. pathflag = FALSE;
  367. if (Rk_nn < 2)
  368. pathflag = FALSE;
  369. if (Rk.id < 0)
  370. pathflag = FALSE;
  371. if (Rk_bestn.id < 0) {
  372. G_debug(4, "Rk's best neighour is negative");
  373. pathflag = FALSE;
  374. }
  375. if (pathflag) {
  376. /* clear candidate flag for Rk */
  377. if (FLAG_GET(globals->candidate_flag, Rk.row, Rk.col)) {
  378. set_candidate_flag(&Rk, FALSE, globals);
  379. }
  380. /* Use Rk as next Ri:
  381. * this is the eCognition technique. */
  382. G_debug(4, "do ecog");
  383. Ri_nn = Rk_nn;
  384. Ri_similarity = Rk_similarity;
  385. dp = Ri.mean;
  386. Ri = Rk;
  387. Rk = Rk_bestn;
  388. Rk_bestn.mean = dp;
  389. Ri_rs.id = Rk_rs.id;
  390. Rk_rs.id = Rk_bestn_rs.id;
  391. Rk_bestn_rs.id = -1;
  392. Ri_rs.count = Rk_rs.count;
  393. Rk_rs.count = Rk_bestn_rs.count;
  394. Rk_bestn_rs.count = 0;
  395. dp = Ri_rs.mean;
  396. Ri_rs.mean = Rk_rs.mean;
  397. Rk_rs.mean = Rk_bestn_rs.mean;
  398. Rk_bestn_rs.mean = dp;
  399. dp = Ri_rs.sum;
  400. Ri_rs.sum = Rk_rs.sum;
  401. Rk_rs.sum = Rk_bestn_rs.sum;
  402. Rk_bestn_rs.sum = dp;
  403. tmpnbtree = Ri_ngbrs;
  404. Ri_ngbrs = Rk_ngbrs;
  405. Rk_ngbrs = tmpnbtree;
  406. nbtree_clear(Rk_ngbrs);
  407. }
  408. }
  409. } /* end if < threshold */
  410. } /* end pathflag */
  411. } /* next col */
  412. } /* next row */
  413. G_percent(1, 1, 1);
  414. /* finished one pass for processing candidate pixels */
  415. G_verbose_message("%d merges", n_merges);
  416. G_debug(4, "Finished pass %d", t);
  417. }
  418. /*end t loop *//*TODO, should there be a max t that it can iterate for? Include t in G_message? */
  419. if (n_merges > 1)
  420. G_message(_("Segmentation processes stopped at %d due to reaching max iteration limit, more merges may be possible"), t);
  421. else
  422. G_message(_("Segmentation converged after %d iterations"), t);
  423. /* assign region IDs to remaining 0 IDs */
  424. G_message(_("Assigning region IDs to remaining single-cell regions..."));
  425. for (row = globals->row_min; row < globals->row_max; row++) {
  426. G_percent(row - globals->row_min,
  427. globals->row_max - globals->row_min, 4);
  428. for (col = globals->col_min; col < globals->col_max; col++) {
  429. if (!(FLAG_GET(globals->null_flag, row, col))) {
  430. /* get segment id */
  431. Segment_get(&globals->rid_seg, (void *) &Ri.id, row, col);
  432. if (Ri.id == 0) {
  433. Ri.id = get_free_id(globals);
  434. Segment_put(&globals->rid_seg, (void *) &Ri.id, row, col);
  435. }
  436. }
  437. }
  438. }
  439. G_percent(1, 1, 1);
  440. free_free_ids();
  441. /* ****************************************************************************************** */
  442. /* final pass, ignore threshold and force a merge for small segments with their best neighbor */
  443. /* ****************************************************************************************** */
  444. if (globals->min_segment_size > 1) {
  445. G_message(_("Merging segments smaller than %d cells..."), globals->min_segment_size);
  446. threshold = globals->alpha * globals->alpha;
  447. flag_clear_all(globals->candidate_flag);
  448. n_merges = 0;
  449. globals->candidate_count = 0;
  450. /* Set candidate flag to true/1 for all non-NULL cells */
  451. for (row = globals->row_min; row < globals->row_max; row++) {
  452. for (col = globals->col_min; col < globals->col_max; col++) {
  453. if (!(FLAG_GET(globals->null_flag, row, col))) {
  454. FLAG_SET(globals->candidate_flag, row, col);
  455. globals->candidate_count++;
  456. }
  457. }
  458. }
  459. G_debug(4, "Starting to process %ld candidate cells",
  460. globals->candidate_count);
  461. /* process candidate cells */
  462. G_percent_reset();
  463. for (row = globals->row_min; row < globals->row_max; row++) {
  464. G_percent(row - globals->row_min,
  465. globals->row_max - globals->row_min, 4);
  466. for (col = globals->col_min; col < globals->col_max; col++) {
  467. int do_merge = 1;
  468. if (!(FLAG_GET(globals->candidate_flag, row, col)))
  469. continue;
  470. Ri.row = row;
  471. Ri.col = col;
  472. /* get segment id */
  473. Segment_get(&globals->rid_seg, (void *) &Ri.id, row, col);
  474. if (Ri.id < 0)
  475. continue;
  476. Ri_rs.id = Ri.id;
  477. /* get segment size */
  478. fetch_reg_stats(Ri.row, Ri.col, &Ri_rs, globals);
  479. memcpy(Ri.mean, Ri_rs.mean, globals->datasize);
  480. Ri.count = Ri_rs.count;
  481. if (Ri.count >= globals->min_segment_size) {
  482. set_candidate_flag(&Ri, FALSE, globals);
  483. do_merge = 0;
  484. }
  485. while (do_merge) {
  486. do_merge = 0;
  487. /* merge all smaller than min size */
  488. if (Ri.count < globals->min_segment_size)
  489. do_merge = 1;
  490. Ri_nn = 0;
  491. Ri_similarity = 2;
  492. Rk.id = -1;
  493. if (do_merge) {
  494. /* find Ri's best neighbor, clear candidate flag */
  495. Ri_nn = find_best_neighbor(&Ri, &Ri_rs, Ri_ngbrs,
  496. &Rk, &Rk_rs,
  497. &Ri_similarity, 1,
  498. globals);
  499. }
  500. do_merge = 0;
  501. if (Rk.id >= 0) {
  502. /* merge Ri with Rk */
  503. /* do not clear candidate flag for Rk */
  504. merge_regions(&Ri, &Ri_rs, &Rk, &Rk_rs, 0, globals);
  505. n_merges++;
  506. if (Ri.count < globals->min_segment_size)
  507. do_merge = 1;
  508. }
  509. }
  510. }
  511. }
  512. G_percent(1, 1, 1);
  513. /* finished one pass for processing candidate pixels */
  514. G_verbose_message("%d merges", n_merges);
  515. }
  516. /* free neighbor stats */
  517. G_free(Ri.mean);
  518. G_free(Rk.mean);
  519. G_free(Rk_bestn.mean);
  520. nbtree_clear(Ri_ngbrs);
  521. nbtree_clear(Rk_ngbrs);
  522. free(Ri_ngbrs);
  523. free(Rk_ngbrs);
  524. /* free region stats */
  525. G_free(Ri_rs.mean);
  526. G_free(Ri_rs.sum);
  527. G_free(Rk_rs.mean);
  528. G_free(Rk_rs.sum);
  529. G_free(Rk_bestn_rs.mean);
  530. G_free(Rk_bestn_rs.sum);
  531. return TRUE;
  532. }
  533. static int find_best_neighbor(struct ngbr_stats *Ri,
  534. struct reg_stats *Ri_rs,
  535. struct NB_TREE *Ri_ngbrs,
  536. struct ngbr_stats *Rk,
  537. struct reg_stats *Rk_rs,
  538. double *sim, int clear_cand,
  539. struct globals *globals)
  540. {
  541. int n, n_ngbrs, no_check, cmp;
  542. struct rc ngbr_rc, next;
  543. struct rclist rilist;
  544. double tempsim;
  545. int neighbors[8][2];
  546. struct RB_TREE *no_check_tree; /* cells already checked */
  547. struct reg_stats *rs_found;
  548. int candk, candtmp;
  549. G_debug(4, "find_best_neighbor()");
  550. if (Ri->id != Ri_rs->id)
  551. G_fatal_error("Ri = %d but Ri_rs = %d", Ri->id, Ri_rs->id);
  552. if (Ri->id < 0)
  553. G_fatal_error("Ri is %d", Ri->id);
  554. /* dynamics of the region growing algorithm
  555. * some regions are growing fast, often surrounded by many small regions
  556. * not all regions are equally growing, some will only grow at a later stage ? */
  557. /* *** initialize data *** */
  558. no_check_tree = rbtree_create(compare_rc, sizeof(struct rc));
  559. ngbr_rc.row = Ri->row;
  560. ngbr_rc.col = Ri->col;
  561. rbtree_insert(no_check_tree, &ngbr_rc);
  562. nbtree_clear(Ri_ngbrs);
  563. n_ngbrs = 0;
  564. /* TODO: add size of largest region to reg_tree, use this as min */
  565. Rk->count = Rk_rs->count = 0;
  566. Rk->id = Rk_rs->id = -1;
  567. candk = 0;
  568. /* go through segment, spreading outwards from head */
  569. rclist_init(&rilist);
  570. /* check neighbors of start cell */
  571. next.row = Ri->row;
  572. next.col = Ri->col;
  573. do {
  574. /* remove from candidates */
  575. if (clear_cand)
  576. FLAG_UNSET(globals->candidate_flag, next.row, next.col);
  577. G_debug(5, "find_pixel_neighbors for row: %d , col %d",
  578. next.row, next.col);
  579. globals->find_neighbors(next.row, next.col, neighbors);
  580. n = globals->nn - 1;
  581. do {
  582. globals->ns.row = ngbr_rc.row = neighbors[n][0];
  583. globals->ns.col = ngbr_rc.col = neighbors[n][1];
  584. no_check = (ngbr_rc.row < globals->row_min ||
  585. ngbr_rc.row >= globals->row_max ||
  586. ngbr_rc.col < globals->col_min ||
  587. ngbr_rc.col >= globals->col_max);
  588. n_ngbrs += no_check;
  589. if (!no_check) {
  590. no_check = ((FLAG_GET(globals->null_flag, ngbr_rc.row,
  591. ngbr_rc.col)) != 0);
  592. n_ngbrs += no_check;
  593. if (!no_check) {
  594. if (!rbtree_find(no_check_tree, &ngbr_rc)) {
  595. /* not yet checked, don't check it again */
  596. rbtree_insert(no_check_tree, &ngbr_rc);
  597. /* get neighbor ID */
  598. Segment_get(&globals->rid_seg,
  599. (void *) &(globals->ns.id),
  600. ngbr_rc.row, ngbr_rc.col);
  601. if (Ri->id > 0 && globals->ns.id == Ri->id) {
  602. /* want to check this neighbor's neighbors */
  603. rclist_add(&rilist, ngbr_rc.row, ngbr_rc.col);
  604. }
  605. else {
  606. /* new neighbor ? */
  607. if (nbtree_find(Ri_ngbrs, &globals->ns) == NULL) {
  608. /* get values for Rk */
  609. globals->rs.id = globals->ns.id;
  610. rs_found = rgtree_find(globals->reg_tree,
  611. &(globals->rs));
  612. if (!rs_found) {
  613. /* region stats are not in search tree */
  614. rs_found = &(globals->rs);
  615. calculate_reg_stats(ngbr_rc.row, ngbr_rc.col,
  616. rs_found, globals);
  617. }
  618. globals->ns.mean = rs_found->mean;
  619. globals->ns.count = rs_found->count;
  620. /* globals->ns is now complete */
  621. tempsim = (globals->calculate_similarity)(Ri, &globals->ns, globals);
  622. candtmp = (FLAG_GET(globals->candidate_flag, ngbr_rc.row, ngbr_rc.col)) != 0;
  623. cmp = compare_sim_ngbrs(tempsim, *sim, candtmp, candk, &globals->ns, Rk);
  624. if (cmp == -1) {
  625. *sim = tempsim;
  626. candk = candtmp;
  627. /* copy temp Rk to Rk */
  628. Rk->row = ngbr_rc.row;
  629. Rk->col = ngbr_rc.col;
  630. Rk->id = rs_found->id;
  631. Rk->count = rs_found->count;
  632. memcpy(Rk->mean, rs_found->mean,
  633. globals->datasize);
  634. Rk_rs->id = Rk->id;
  635. Rk_rs->count = Rk->count;
  636. memcpy(Rk_rs->mean, rs_found->mean,
  637. globals->datasize);
  638. memcpy(Rk_rs->sum, rs_found->sum,
  639. globals->datasize);
  640. }
  641. n_ngbrs++;
  642. nbtree_insert(Ri_ngbrs, &globals->ns);
  643. }
  644. }
  645. }
  646. }
  647. }
  648. } while (n--); /* end do loop - next neighbor */
  649. } while (rclist_drop(&rilist, &next)); /* while there are cells to check */
  650. /* clean up */
  651. rbtree_destroy(no_check_tree);
  652. rclist_destroy(&rilist);
  653. return n_ngbrs;
  654. }
  655. #ifdef _OR_SHAPE_
  656. double calculate_shape(struct reg_stats *rsi, struct reg_stats *rsk,
  657. int nshared, struct globals *globals)
  658. {
  659. /*
  660. In the eCognition literature, we find that the key factor in the
  661. multi-scale segmentation algorithm used by Definiens is the scale
  662. factor f:
  663. f = W.Hcolor + (1 - W).Hshape
  664. Hcolor = sum(b = 1:nbands)(Wb.SigmaB)
  665. Hshape = Ws.Hcompact + (1 - Ws).Hsmooth
  666. Hcompact = PL/sqrt(Npx)
  667. Hsmooth = PL/Pbbox
  668. Where
  669. W is a user-defined weight of importance of object radiometry vs
  670. shape (usually .9 vs .1)
  671. Wb is the weigh given to band B
  672. SigmaB is the std dev of the object for band b
  673. Ws is a user-defined weight giving the importance of compactedness vs smoothness
  674. PL is the perimeter length of the object
  675. Npx the number of pixels within the object
  676. Pbbox the perimeter of the bounding box of the object.
  677. */
  678. /* here we calculate a shape index for the new object to be created
  679. * the radiometric index ranges from 0 to 1, 0 = identical
  680. * with the shape index we want to favour compact and smooth opjects
  681. * thus the shape index should range from 0 to 1,
  682. * 0 = maximum compactness and smoothness */
  683. double smooth, compact;
  684. int pl, pbbox, count;
  685. double bboxdiag;
  686. int pl1, pl2, count1, count2;
  687. int e1, n1, s1, w1, e2, n2, s2, w2, ns_extent, ew_extent;
  688. pl = pl1 + pl2 - nshared;
  689. ns_extent = MAX(n1, n2) - MIN(s1, s2);
  690. ew_extent = MAX(e1, e2) - MIN(w1, w2);
  691. pbbox = 2 * (ns_extent + ew_extent);
  692. /* Smoothness Hsmooth = PL / Pbbox
  693. * the smallest possible value would be
  694. * the diagonal divided by the bbox perimeter
  695. * invert such that the largest possible value would be
  696. * the bbox perimeter divided by the diagonal
  697. */
  698. bboxdiag = sqrt(ns_extent * ns_extent + ew_extent * ew_extent);
  699. smooth = 1. - (double)bboxdiag / pl; /* smaller -> smoother */
  700. count = count1 + count2;
  701. /* compactness Hcompact = PL / sqrt(Npx)
  702. * a circle is the most compact form
  703. * Npx = M_PI * r * r;
  704. * r = sqrt(Npx / M_pi)
  705. * pl_circle = 2 * sqrt(count * M_PI);
  706. * compact = 1 - pl_circle / (pl * sqrt(count);
  707. */
  708. /* compact = 1 - 2 * sqrt(M_PI) / pl; */
  709. /* PL max = Npx */
  710. /* Hcompact max = Npx / sqrt(Npx) = sqrt(Npx)
  711. * Hcompact / Hcompact max = (PL / sqrt(Npx)) / sqrt(Npx)
  712. * = PL / Npx
  713. */
  714. compact = (double)pl / count; /* smaller -> more compact */
  715. return globals->smooth_weight * smooth + (1 - globals->smooth_weight) * compact;
  716. }
  717. #endif
  718. static int search_neighbors(struct ngbr_stats *Ri,
  719. struct reg_stats *Ri_rs,
  720. struct NB_TREE *Ri_ngbrs,
  721. double *sim,
  722. struct ngbr_stats *Rk,
  723. struct reg_stats *Rk_rs,
  724. struct globals *globals)
  725. {
  726. double tempsim, *dp;
  727. struct NB_TRAV travngbr;
  728. struct ngbr_stats *next;
  729. int cmp, candk, candtmp;
  730. G_debug(4, "search_neighbors");
  731. if (Ri->id != Ri_rs->id)
  732. G_fatal_error("Ri = %d but Ri_rs = %d", Ri->id, Ri_rs->id);
  733. if (Ri->id <= 0)
  734. G_fatal_error("Ri is %d", Ri->id);
  735. if (Ri_rs->id <= 0)
  736. G_fatal_error("Ri_rs is %d", Ri_rs->id);
  737. nbtree_init_trav(&travngbr, Ri_ngbrs);
  738. Rk->count = 0;
  739. Rk->id = Rk_rs->id = -1;
  740. candk = 0;
  741. while ((next = nbtree_traverse(&travngbr))) {
  742. tempsim = (globals->calculate_similarity)(Ri, next, globals);
  743. candtmp = (FLAG_GET(globals->candidate_flag, next->row, next->col)) != 0;
  744. cmp = compare_sim_ngbrs(tempsim, *sim, candtmp, candk, next, Rk);
  745. if (cmp == -1) {
  746. *sim = tempsim;
  747. candk = candtmp;
  748. dp = Rk->mean;
  749. *Rk = *next;
  750. Rk->mean = dp;
  751. memcpy(Rk->mean, next->mean, globals->datasize);
  752. }
  753. }
  754. Rk_rs->id = Rk->id;
  755. if (Rk->id >= 0) {
  756. fetch_reg_stats(Rk->row, Rk->col, Rk_rs, globals);
  757. }
  758. return 1;
  759. }
  760. int update_band_vals(int row, int col, struct reg_stats *rs,
  761. struct globals *globals) {
  762. struct rc next, ngbr_rc;
  763. int neighbors[8][2];
  764. int rid, count, n;
  765. /* update band values with sum */
  766. /* rs->id must be set */
  767. G_debug(4, "update_band_vals()");
  768. if (rs->count >= globals->min_reg_size) {
  769. G_fatal_error(_("Region stats should go in tree, %d >= %d"),
  770. rs->count, globals->min_reg_size);
  771. }
  772. Segment_get(&globals->rid_seg, (void *) &rid, row, col);
  773. if (rid != rs->id) {
  774. G_fatal_error(_("Region ids are different"));
  775. }
  776. if (rs->id < 1) {
  777. G_fatal_error(_("Region id %d is invalid"), rs->id);
  778. }
  779. if (rs->count == 1) {
  780. G_warning(_("Region consists of only one cell, nothing to update"));
  781. return rs->count;
  782. }
  783. /* update region stats */
  784. Segment_put(&globals->bands_seg, (void *)rs->sum, row, col);
  785. count = 1;
  786. /* fast version for rs->count == 2 */
  787. if (rs->count == 2) {
  788. globals->find_neighbors(row, col, neighbors);
  789. n = globals->nn - 1;
  790. do {
  791. ngbr_rc.row = neighbors[n][0];
  792. ngbr_rc.col = neighbors[n][1];
  793. if (ngbr_rc.row < globals->row_min || ngbr_rc.row >= globals->row_max ||
  794. ngbr_rc.col < globals->col_min || ngbr_rc.col >= globals->col_max) {
  795. continue;
  796. }
  797. if ((FLAG_GET(globals->null_flag, ngbr_rc.row, ngbr_rc.col)) == 0) {
  798. Segment_get(&globals->rid_seg, (void *) &rid,
  799. ngbr_rc.row, ngbr_rc.col);
  800. if (rid == rs->id) {
  801. /* update region stats */
  802. Segment_put(&globals->bands_seg,
  803. (void *)rs->sum,
  804. ngbr_rc.row, ngbr_rc.col);
  805. count++;
  806. /* only one other neighbor can have the same ID
  807. * deactivate for debugging */
  808. break;
  809. }
  810. }
  811. } while (n--);
  812. if (count > 2)
  813. G_fatal_error(_("Region size is larger than 2: %d"), count);
  814. }
  815. else if (rs->count > 2) {
  816. struct RB_TREE *rc_check_tree; /* cells already checked */
  817. struct rclist rlist;
  818. int no_check;
  819. /* go through region, spreading outwards from head */
  820. rclist_init(&rlist);
  821. rc_check_tree = rbtree_create(compare_rc, sizeof(struct rc));
  822. ngbr_rc.row = row;
  823. ngbr_rc.col = col;
  824. rbtree_insert(rc_check_tree, &ngbr_rc);
  825. next.row = row;
  826. next.col = col;
  827. do {
  828. G_debug(5, "find_pixel_neighbors for row: %d , col %d",
  829. next.row, next.col);
  830. globals->find_neighbors(next.row, next.col, neighbors);
  831. n = globals->nn - 1;
  832. do {
  833. ngbr_rc.row = neighbors[n][0];
  834. ngbr_rc.col = neighbors[n][1];
  835. no_check = (ngbr_rc.row < 0 || ngbr_rc.row >= globals->nrows ||
  836. ngbr_rc.col < 0 || ngbr_rc.col >= globals->ncols);
  837. if (!no_check) {
  838. if ((FLAG_GET(globals->null_flag, ngbr_rc.row, ngbr_rc.col)) == 0) {
  839. /* already checked ? */
  840. if (!rbtree_find(rc_check_tree, &ngbr_rc)) {
  841. /* not yet checked, don't check it again */
  842. rbtree_insert(rc_check_tree, &ngbr_rc);
  843. Segment_get(&globals->rid_seg, (void *) &rid,
  844. ngbr_rc.row, ngbr_rc.col);
  845. if (rid == rs->id) {
  846. /* want to check this neighbor's neighbors */
  847. rclist_add(&rlist, ngbr_rc.row, ngbr_rc.col);
  848. /* update region stats */
  849. Segment_put(&globals->bands_seg,
  850. (void *)rs->sum,
  851. ngbr_rc.row, ngbr_rc.col);
  852. count++;
  853. }
  854. }
  855. }
  856. }
  857. } while (n--);
  858. } while (rclist_drop(&rlist, &next));
  859. /* clean up */
  860. rbtree_destroy(rc_check_tree);
  861. rclist_destroy(&rlist);
  862. }
  863. if (count != rs->count) {
  864. G_fatal_error(_("Region size is %d, should be %d"),
  865. count, rs->count);
  866. }
  867. return count;
  868. }
  869. static int merge_regions(struct ngbr_stats *Ri, struct reg_stats *Ri_rs,
  870. struct ngbr_stats *Rk, struct reg_stats *Rk_rs,
  871. int do_cand, struct globals *globals)
  872. {
  873. int n;
  874. int R_id;
  875. struct rc next, ngbr_rc;
  876. struct rclist rlist;
  877. int neighbors[8][2];
  878. struct reg_stats *new_rs;
  879. G_debug(4, "merge_regions");
  880. /* Ri ID must always be positive */
  881. if (Ri_rs->id < 1 && Ri_rs->count > 1)
  882. G_fatal_error("Ri id is not positive: %d, but count is > 1: %d",
  883. Ri_rs->id, Ri_rs->count);
  884. /* if Rk ID is zero (no seed), Rk count must be 1 */
  885. if (Rk_rs->id < 1 && Rk_rs->count > 1)
  886. G_fatal_error("Rk id is not positive: %d, but count is > 1: %d",
  887. Rk_rs->id, Rk_rs->count);
  888. /* update segment id and clear candidate flag */
  889. /* cases
  890. * Ri, Rk are not in the tree
  891. * Ri, Rk are both in the tree
  892. * Ri is in the tree, Rk is not
  893. * Rk is in the tree, Ri is not
  894. */
  895. /* Ri_rs, Rk_rs must always be set */
  896. /* add Rk */
  897. Ri_rs->count += Rk_rs->count;
  898. n = globals->nbands - 1;
  899. do {
  900. Ri_rs->sum[n] += Rk_rs->sum[n];
  901. Ri_rs->mean[n] = Ri_rs->sum[n] / Ri_rs->count;
  902. } while (n--);
  903. if (Ri->count >= Rk->count) {
  904. if (Ri->id == 0) {
  905. Ri->id = get_free_id(globals);
  906. Ri_rs->id = Ri->id;
  907. Segment_put(&globals->rid_seg, (void *) &Ri->id, Ri->row, Ri->col);
  908. }
  909. if (Rk->count >= globals->min_reg_size) {
  910. if (rgtree_find(globals->reg_tree, Rk_rs) == NULL)
  911. G_fatal_error("merge regions: Rk should be in tree");
  912. /* remove from tree */
  913. rgtree_remove(globals->reg_tree, Rk_rs);
  914. }
  915. add_free_id(Rk->id);
  916. }
  917. else {
  918. if (Ri->count >= globals->min_reg_size) {
  919. if (rgtree_find(globals->reg_tree, Ri_rs) == NULL)
  920. G_fatal_error("merge regions: Ri should be in tree");
  921. /* remove from tree */
  922. rgtree_remove(globals->reg_tree, Ri_rs);
  923. }
  924. add_free_id(Ri->id);
  925. /* magic switch */
  926. Ri_rs->id = Rk->id;
  927. }
  928. if ((new_rs = rgtree_find(globals->reg_tree, Ri_rs)) != NULL) {
  929. /* update stats for tree item */
  930. new_rs->count = Ri_rs->count;
  931. memcpy(new_rs->mean, Ri_rs->mean, globals->datasize);
  932. memcpy(new_rs->sum, Ri_rs->sum, globals->datasize);
  933. }
  934. else if (Ri_rs->count >= globals->min_reg_size) {
  935. /* add to tree */
  936. rgtree_insert(globals->reg_tree, Ri_rs);
  937. }
  938. Ri->count = Ri_rs->count;
  939. memcpy(Ri->mean, Ri_rs->mean, globals->datasize);
  940. if (Rk->id == 0) {
  941. /* the actual merge: change region id */
  942. Segment_put(&globals->rid_seg, (void *) &Ri->id, Rk->row, Rk->col);
  943. if (do_cand) {
  944. if (FLAG_GET(globals->candidate_flag, Rk->row, Rk->col)) {
  945. /* clear candidate flag */
  946. FLAG_UNSET(globals->candidate_flag, Rk->row, Rk->col);
  947. globals->candidate_count--;
  948. }
  949. }
  950. }
  951. else if (Ri->id == Ri_rs->id) {
  952. /* Ri is already updated, including candidate flags
  953. * need to clear candidate flag for Rk and set new id */
  954. /* the actual merge: change region id */
  955. Segment_put(&globals->rid_seg, (void *) &Ri->id, Rk->row, Rk->col);
  956. if (do_cand) {
  957. do_cand = 0;
  958. if (FLAG_GET(globals->candidate_flag, Rk->row, Rk->col)) {
  959. /* clear candidate flag */
  960. FLAG_UNSET(globals->candidate_flag, Rk->row, Rk->col);
  961. globals->candidate_count--;
  962. do_cand = 1;
  963. }
  964. }
  965. rclist_init(&rlist);
  966. if (Rk->count > 1)
  967. rclist_add(&rlist, Rk->row, Rk->col);
  968. while (rclist_drop(&rlist, &next)) {
  969. if (do_cand) {
  970. /* clear candidate flag */
  971. FLAG_UNSET(globals->candidate_flag, next.row, next.col);
  972. globals->candidate_count--;
  973. }
  974. globals->find_neighbors(next.row, next.col, neighbors);
  975. n = globals->nn - 1;
  976. do {
  977. ngbr_rc.row = neighbors[n][0];
  978. ngbr_rc.col = neighbors[n][1];
  979. if (ngbr_rc.row >= globals->row_min &&
  980. ngbr_rc.row < globals->row_max &&
  981. ngbr_rc.col >= globals->col_min &&
  982. ngbr_rc.col < globals->col_max) {
  983. if (!(FLAG_GET(globals->null_flag, ngbr_rc.row, ngbr_rc.col))) {
  984. Segment_get(&globals->rid_seg, (void *) &R_id,
  985. ngbr_rc.row, ngbr_rc.col);
  986. if (Rk->id > 0 && R_id == Rk->id) {
  987. /* the actual merge: change region id */
  988. Segment_put(&globals->rid_seg, (void *) &Ri->id, ngbr_rc.row, ngbr_rc.col);
  989. /* want to check this neighbor's neighbors */
  990. rclist_add(&rlist, ngbr_rc.row, ngbr_rc.col);
  991. }
  992. }
  993. }
  994. } while (n--);
  995. }
  996. rclist_destroy(&rlist);
  997. }
  998. else {
  999. /* Rk was larger than Ri */
  1000. /* clear candidate flag for Rk */
  1001. if (do_cand && FLAG_GET(globals->candidate_flag, Rk->row, Rk->col)) {
  1002. set_candidate_flag(Rk, FALSE, globals);
  1003. }
  1004. /* update region id for Ri */
  1005. /* the actual merge: change region id */
  1006. Segment_put(&globals->rid_seg, (void *) &Rk->id, Ri->row, Ri->col);
  1007. rclist_init(&rlist);
  1008. if (Ri->count > 1)
  1009. rclist_add(&rlist, Ri->row, Ri->col);
  1010. while (rclist_drop(&rlist, &next)) {
  1011. globals->find_neighbors(next.row, next.col, neighbors);
  1012. n = globals->nn - 1;
  1013. do {
  1014. ngbr_rc.row = neighbors[n][0];
  1015. ngbr_rc.col = neighbors[n][1];
  1016. if (ngbr_rc.row >= globals->row_min &&
  1017. ngbr_rc.row < globals->row_max &&
  1018. ngbr_rc.col >= globals->col_min &&
  1019. ngbr_rc.col < globals->col_max) {
  1020. if (!(FLAG_GET(globals->null_flag, ngbr_rc.row, ngbr_rc.col))) {
  1021. Segment_get(&globals->rid_seg, (void *) &R_id, ngbr_rc.row, ngbr_rc.col);
  1022. if (Ri->id > 0 && R_id == Ri->id) {
  1023. /* the actual merge: change region id */
  1024. Segment_put(&globals->rid_seg, (void *) &Rk->id, ngbr_rc.row, ngbr_rc.col);
  1025. /* want to check this neighbor's neighbors */
  1026. rclist_add(&rlist, ngbr_rc.row, ngbr_rc.col);
  1027. }
  1028. }
  1029. }
  1030. } while (n--);
  1031. }
  1032. rclist_destroy(&rlist);
  1033. Ri->id = Ri_rs->id; /* == Rk->id */
  1034. if (Ri->id != Rk->id)
  1035. G_fatal_error("Ri ID should be set to Rk ID");
  1036. }
  1037. /* disable Rk */
  1038. Rk->id = Rk_rs->id = -1;
  1039. Rk->count = Rk_rs->count = 0;
  1040. /* update Ri */
  1041. Ri->id = Ri_rs->id;
  1042. if (Ri_rs->count < globals->min_reg_size) {
  1043. update_band_vals(Ri->row, Ri->col, Ri_rs, globals);
  1044. }
  1045. return TRUE;
  1046. }
  1047. static int set_candidate_flag(struct ngbr_stats *head, int value, struct globals *globals)
  1048. {
  1049. int n, R_id;
  1050. struct rc next, ngbr_rc;
  1051. struct rclist rlist;
  1052. int neighbors[8][2];
  1053. G_debug(4, "set_candidate_flag");
  1054. if ((!(FLAG_GET(globals->candidate_flag, head->row, head->col))) != value) {
  1055. G_warning(_("Candidate flag is already %s"), value ? _("set") : _("unset"));
  1056. return FALSE;
  1057. }
  1058. /* (un)set candidate flag */
  1059. if (value == TRUE) {
  1060. FLAG_SET(globals->candidate_flag, head->row, head->col);
  1061. globals->candidate_count++;
  1062. }
  1063. else {
  1064. FLAG_UNSET(globals->candidate_flag, head->row, head->col);
  1065. globals->candidate_count--;
  1066. }
  1067. if (head->id == 0)
  1068. return TRUE;
  1069. rclist_init(&rlist);
  1070. rclist_add(&rlist, head->row, head->col);
  1071. while (rclist_drop(&rlist, &next)) {
  1072. globals->find_neighbors(next.row, next.col, neighbors);
  1073. n = globals->nn - 1;
  1074. do {
  1075. ngbr_rc.row = neighbors[n][0];
  1076. ngbr_rc.col = neighbors[n][1];
  1077. if (ngbr_rc.row >= globals->row_min &&
  1078. ngbr_rc.row < globals->row_max &&
  1079. ngbr_rc.col >= globals->col_min &&
  1080. ngbr_rc.col < globals->col_max) {
  1081. if (!(FLAG_GET(globals->null_flag, ngbr_rc.row, ngbr_rc.col))) {
  1082. if ((!(FLAG_GET(globals->candidate_flag, ngbr_rc.row, ngbr_rc.col))) == value) {
  1083. Segment_get(&globals->rid_seg, (void *) &R_id, ngbr_rc.row, ngbr_rc.col);
  1084. if (R_id == head->id) {
  1085. /* want to check this neighbor's neighbors */
  1086. rclist_add(&rlist, ngbr_rc.row, ngbr_rc.col);
  1087. /* (un)set candidate flag */
  1088. if (value == TRUE) {
  1089. FLAG_SET(globals->candidate_flag, ngbr_rc.row, ngbr_rc.col);
  1090. globals->candidate_count++;
  1091. }
  1092. else {
  1093. FLAG_UNSET(globals->candidate_flag, ngbr_rc.row, ngbr_rc.col);
  1094. globals->candidate_count--;
  1095. }
  1096. }
  1097. }
  1098. }
  1099. }
  1100. } while (n--);
  1101. }
  1102. rclist_destroy(&rlist);
  1103. return TRUE;
  1104. }
  1105. int fetch_reg_stats(int row, int col, struct reg_stats *rs,
  1106. struct globals *globals)
  1107. {
  1108. struct reg_stats *rs_found;
  1109. if (rs->id < 0)
  1110. G_fatal_error("fetch_reg_stats(): invalid region id %d", rs->id);
  1111. if (rs->id > 0 && (rs_found = rgtree_find(globals->reg_tree, rs)) != NULL) {
  1112. memcpy(rs->mean, rs_found->mean, globals->datasize);
  1113. memcpy(rs->sum, rs_found->sum, globals->datasize);
  1114. rs->count = rs_found->count;
  1115. return 1;
  1116. }
  1117. calculate_reg_stats(row, col, rs, globals);
  1118. return 2;
  1119. }
  1120. static int calculate_reg_stats(int row, int col, struct reg_stats *rs,
  1121. struct globals *globals)
  1122. {
  1123. int ret = 0;
  1124. G_debug(4, "calculate_reg_stats()");
  1125. if (rs->id < 0)
  1126. G_fatal_error("Invalid region id %d", rs->id);
  1127. Segment_get(&globals->bands_seg, (void *)globals->bands_val,
  1128. row, col);
  1129. rs->count = 1;
  1130. memcpy(rs->sum, globals->bands_val, globals->datasize);
  1131. if (rs->id == 0) {
  1132. memcpy(rs->mean, rs->sum, globals->datasize);
  1133. return 1;
  1134. }
  1135. if (globals->min_reg_size < 3)
  1136. ret = 1;
  1137. else if (globals->min_reg_size == 3) {
  1138. int n, rid;
  1139. struct rc ngbr_rc;
  1140. int neighbors[8][2];
  1141. globals->find_neighbors(row, col, neighbors);
  1142. n = globals->nn - 1;
  1143. do {
  1144. ngbr_rc.row = neighbors[n][0];
  1145. ngbr_rc.col = neighbors[n][1];
  1146. if (ngbr_rc.row < globals->row_min || ngbr_rc.row >= globals->row_max ||
  1147. ngbr_rc.col < globals->col_min || ngbr_rc.col >= globals->col_max) {
  1148. continue;
  1149. }
  1150. if ((FLAG_GET(globals->null_flag, ngbr_rc.row, ngbr_rc.col)) == 0) {
  1151. Segment_get(&globals->rid_seg, (void *) &rid,
  1152. ngbr_rc.row, ngbr_rc.col);
  1153. if (rid == rs->id) {
  1154. /* update region stats */
  1155. rs->count++;
  1156. /* only one other neighbor can have the same ID
  1157. * deactivate for debugging */
  1158. break;
  1159. }
  1160. }
  1161. } while (n--);
  1162. if (rs->count > 2)
  1163. G_fatal_error(_("Region size is larger than 2: %d"), rs->count);
  1164. ret = 2;
  1165. }
  1166. else if (globals->min_reg_size > 3) {
  1167. /* rs->id must be set */
  1168. struct RB_TREE *rc_check_tree; /* cells already checked */
  1169. int n, rid;
  1170. struct rc ngbr_rc, next;
  1171. struct rclist rilist;
  1172. int neighbors[8][2];
  1173. int no_check;
  1174. /* go through region, spreading outwards from head */
  1175. rclist_init(&rilist);
  1176. rc_check_tree = rbtree_create(compare_rc, sizeof(struct rc));
  1177. ngbr_rc.row = row;
  1178. ngbr_rc.col = col;
  1179. rbtree_insert(rc_check_tree, &ngbr_rc);
  1180. next.row = row;
  1181. next.col = col;
  1182. do {
  1183. G_debug(5, "find_pixel_neighbors for row: %d , col %d",
  1184. next.row, next.col);
  1185. globals->find_neighbors(next.row, next.col, neighbors);
  1186. n = globals->nn - 1;
  1187. do {
  1188. ngbr_rc.row = neighbors[n][0];
  1189. ngbr_rc.col = neighbors[n][1];
  1190. no_check = (ngbr_rc.row < globals->row_min ||
  1191. ngbr_rc.row >= globals->row_max ||
  1192. ngbr_rc.col < globals->col_min ||
  1193. ngbr_rc.col >= globals->col_max);
  1194. if (!no_check) {
  1195. if ((FLAG_GET(globals->null_flag, ngbr_rc.row, ngbr_rc.col)) == 0) {
  1196. /* already checked ? */
  1197. if (!rbtree_find(rc_check_tree, &ngbr_rc)) {
  1198. /* not yet checked, don't check it again */
  1199. rbtree_insert(rc_check_tree, &ngbr_rc);
  1200. Segment_get(&globals->rid_seg, (void *) &rid,
  1201. ngbr_rc.row, ngbr_rc.col);
  1202. if (rid == rs->id) {
  1203. /* want to check this neighbor's neighbors */
  1204. rclist_add(&rilist, ngbr_rc.row, ngbr_rc.col);
  1205. /* update region stats */
  1206. rs->count++;
  1207. }
  1208. }
  1209. }
  1210. }
  1211. } while (n--);
  1212. } while (rclist_drop(&rilist, &next));
  1213. /* clean up */
  1214. rbtree_destroy(rc_check_tree);
  1215. rclist_destroy(&rilist);
  1216. ret = 3;
  1217. }
  1218. /* band mean */
  1219. if (rs->count == 1)
  1220. memcpy(rs->mean, rs->sum, globals->datasize);
  1221. else {
  1222. int i = globals->nbands - 1;
  1223. do {
  1224. rs->mean[i] = rs->sum[i] / rs->count;
  1225. } while (i--);
  1226. }
  1227. if (rs->count >= globals->min_reg_size)
  1228. G_fatal_error(_("Region of size %d should be in search tree"),
  1229. rs->count);
  1230. return ret;
  1231. }