segment-graph.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright (C) 2006 Pedro Felzenszwalb
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #ifndef SEGMENT_GRAPH
  16. #define SEGMENT_GRAPH
  17. #include <algorithm>
  18. #include <cmath>
  19. #include "disjoint-set.h"
  20. // threshold function
  21. #define THRESHOLD(size, c) (c/size)
  22. typedef struct {
  23. float w;
  24. int a, b;
  25. } edge;
  26. bool operator<(const edge &a, const edge &b) {
  27. return a.w < b.w;
  28. }
  29. /*
  30. * Segment a graph
  31. *
  32. * Returns a disjoint-set forest representing the segmentation.
  33. *
  34. * num_vertices: number of vertices in graph.
  35. * num_edges: number of edges in graph
  36. * edges: array of edges.
  37. * c: constant for treshold function.
  38. */
  39. universe *segment_graph(int num_vertices, int num_edges, edge *edges,
  40. float c) {
  41. // sort edges by weight
  42. // note: if weights occure more then once, this might lead to non-deterministic (non-reproducable) behaviour, since
  43. // "Equivalent elements are not guaranteed to keep their original relative order"
  44. // std::sort(edges, edges + num_edges);
  45. /* for (int i = 0; i < 5; i++) {
  46. int a = edges[i].a;
  47. int b = edges[i].b;
  48. mexPrintf("edges before %d: %d <-> %d with weight %f\n",i, a, b, edges[i].w);
  49. } */
  50. // adaptation: use stable_sort instead, which will keep the relative position of equal elements :)
  51. std::stable_sort(edges, edges + num_edges);
  52. // make a disjoint-set forest
  53. universe *u = new universe(num_vertices);
  54. // init thresholds
  55. float *threshold = new float[num_vertices];
  56. for (int i = 0; i < num_vertices; i++)
  57. threshold[i] = THRESHOLD(1,c);
  58. //that's the same as with find, and also non-reproducable
  59. // for (int i = 0; i < 5; i++) {
  60. // int a = edges[i].a;
  61. // int b = edges[i].b;
  62. // mexPrintf("edges after %d: %d <-> %d with weight %f\n",i, a, b, edges[i].w);
  63. // }
  64. // for (int i = 0; i < 5; i++) {
  65. // int a = u->find(edges[i].a);
  66. // int b = u->find(edges[i].b);
  67. // mexPrintf("components %d: %d <-> %d\n",i, a, b);
  68. // }
  69. // this is already non-reproducable
  70. // for each edge, in non-decreasing weight order...
  71. for (int i = 0; i < num_edges; i++) {
  72. edge *pedge = &edges[i];
  73. // components conected by this edge
  74. int a = u->find(pedge->a);
  75. int b = u->find(pedge->b);
  76. if (a != b) {
  77. if ((pedge->w <= threshold[a]) &&
  78. (pedge->w <= threshold[b])) {
  79. u->join(a, b);
  80. a = u->find(a);
  81. threshold[a] = pedge->w + THRESHOLD(u->size(a), c);
  82. }
  83. }
  84. }
  85. // free up
  86. delete threshold;
  87. return u;
  88. }
  89. #endif