Commit 3f558e48541c0ab3601d2248e1e1940a32c86fad

Authored by Pierre Lassalle
1 parent d9c40966
Exists in master

More efficient GRM

CMakeLists.txt
... ... @@ -27,6 +27,15 @@ ELSE(OTB_FOUND)
27 27 "Cannot build OTB project without OTB. Please set OTB_DIR.")
28 28 ENDIF(OTB_FOUND)
29 29  
  30 +#Activate c++11
  31 +include(CheckCXXCompilerFlag)
  32 +CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
  33 +if(COMPILER_SUPPORTS_CXX11)
  34 + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fpermissive -Wall -Wmaybe-uninitialized")
  35 +else()
  36 + message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
  37 +endif()
  38 +
30 39 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/Code)
31 40  
32 41 add_subdirectory(Code)
... ...
Code/CMakeLists.txt
... ... @@ -16,7 +16,6 @@
16 16  
17 17 #=========================================================================
18 18 add_library(OTBGRM
19   - lsrmContourOperations.cpp
20 19 lsrmNeighborhood.cpp)
21 20  
22 21 target_link_libraries(OTBGRM OTBCommon OTBIO)
... ...
Code/lsrmBaatzSegmenter.txx
... ... @@ -51,7 +51,6 @@ namespace lsrm
51 51 float
52 52 BaatzSegmenter<TImage>::ComputeMergingCost(NodePointerType n1, NodePointerType n2)
53 53 {
54   - const std::size_t bands = n1->m_Means.size();
55 54 const unsigned int a1 = n1->m_Area, a2 = n2->m_Area, a_sum = a1 + a2;
56 55  
57 56 float spect_cost = 0.0f;
... ... @@ -77,7 +76,7 @@ namespace lsrm
77 76 const unsigned int boundary = (GraphOperatorType::FindEdge(n1, n2))->m_Boundary;
78 77 const float p3 = p1 + p2 - 2 * static_cast<float>(boundary);
79 78  
80   - const BoundingBox merged_bbox = ContourOperations::MergeBoundingBoxes(n1->m_Bbox, n2->m_Bbox);
  79 + const BoundingBox merged_bbox = MergeBoundingBoxes(n1->m_Bbox, n2->m_Bbox);
81 80 const float bb1_perimeter = static_cast<float>(2*n1->m_Bbox.m_W + 2*n1->m_Bbox.m_H);
82 81 const float bb2_perimeter = static_cast<float>(2*n2->m_Bbox.m_W + 2*n2->m_Bbox.m_H);
83 82 const float mbb_perimeter = static_cast<float>(2 * merged_bbox.m_W + 2 * merged_bbox.m_H);
... ... @@ -141,11 +140,6 @@ namespace lsrm
141 140 this->m_ImageWidth,
142 141 this->m_ImageHeight,
143 142 this->m_LabelImageFileName);
144   -
145   - if(!this->m_ContourImageFileName.empty())
146   - IOType::WriteContourImage(this->m_Graph,
147   - this->m_InputFileName,
148   - this->m_LabelImageFileName);
149 143 }
150 144 } // end of namespace lsrm
151 145  
... ...
Code/lsrmContourOperations.cpp
... ... @@ -1,689 +0,0 @@
1   -#include "lsrmContourOperations.h"
2   -
3   -namespace lsrm
4   -{
5   - BoundingBox
6   - ContourOperations::MergeBoundingBoxes(const BoundingBox& bb1,
7   - const BoundingBox& bb2)
8   - {
9   - long unsigned int min_ux, min_uy, max_xw, max_yh;
10   - BoundingBox bb;
11   -
12   - min_ux = std::min(bb1.m_UX, bb2.m_UX);
13   - min_uy = std::min(bb1.m_UY, bb2.m_UY);
14   - max_xw = std::max(bb1.m_UX + bb1.m_W, bb2.m_UX + bb2.m_W);
15   - max_yh = std::max(bb1.m_UY + bb1.m_H, bb2.m_UY + bb2.m_H);
16   -
17   - bb.m_UX = min_ux;
18   - bb.m_UY = min_uy;
19   - bb.m_W = max_xw - min_ux;
20   - bb.m_H = max_yh - min_uy;
21   -
22   - return bb;
23   - }
24   -
25   - long unsigned int
26   - ContourOperations::ToBoundingBoxCoordinates(const long unsigned int id,
27   - const BoundingBox& bb,
28   - const unsigned int width)
29   - {
30   - long unsigned int img_x = id % width;
31   - long unsigned int img_y = id / width;
32   - long unsigned int bb_x = img_x - bb.m_UX;
33   - long unsigned int bb_y = img_y - bb.m_UY;
34   - return (bb_y * bb.m_W + bb_x);
35   - }
36   -
37   - typename ContourOperations::PixelList
38   - ContourOperations::GenerateBorderPixels(long unsigned int id,
39   - const Contour& contour,
40   - const unsigned int width)
41   - {
42   - PixelList pixels;
43   - pixels.push_back(id);
44   -
45   - short curr_move, prev_move;
46   -
47   - if(contour.size() > 4)
48   - {
49   - for(ContourConstIterator m = contour.begin()+1; m != contour.end(); m++)
50   - {
51   - curr_move = (*m)[0] + 2*(*m)[1];
52   - prev_move = (*(m-1))[0] + 2*(*(m-1))[1];
53   -
54   - if(curr_move == 0)
55   - {
56   - if(prev_move == 0)
57   - {
58   - id-=width;
59   - pixels.push_back(id);
60   - }
61   - else if(prev_move == 1) // 1
62   - {
63   - id++;
64   - pixels.push_back(id);
65   - id-=width;
66   - pixels.push_back(id);
67   - }
68   - }
69   - else if(curr_move == 1)
70   - {
71   - if(prev_move == 1)
72   - {
73   - id++;
74   - pixels.push_back(id);
75   - }
76   - else if(prev_move == 2)
77   - {
78   - id+=width;
79   - pixels.push_back(id);
80   - id++;
81   - pixels.push_back(id);
82   - }
83   - }
84   - else if(curr_move == 2)
85   - {
86   - if(prev_move == 3)
87   - {
88   - id--;
89   - pixels.push_back(id);
90   - id+=width;
91   - pixels.push_back(id);
92   - }
93   - else if(prev_move == 2)
94   - {
95   - id+=width;
96   - pixels.push_back(id);
97   - }
98   - }
99   - else if(curr_move == 3)
100   - {
101   - if(prev_move==0)
102   - {
103   - id-=width;
104   - pixels.push_back(id);
105   - id--;
106   - pixels.push_back(id);
107   - }
108   - else if(prev_move == 3)
109   - {
110   - id--;
111   - pixels.push_back(id);
112   - }
113   - }
114   - }
115   - // Remove duplicated pixels
116   - std::sort(pixels.begin(), pixels.end());
117   - PixelIterator it = std::unique(pixels.begin(), pixels.end());
118   - pixels.resize(std::distance(pixels.begin(), it));
119   - }
120   - return pixels;
121   - }
122   -
123   - bool
124   - ContourOperations::GetCollisionAtNorth(const long unsigned int idx,
125   - const short * grid,
126   - const unsigned int grid_width)
127   - {
128   - long int tmp_idx = idx - grid_width;
129   -
130   - if(grid[tmp_idx] > 0)
131   - return true;
132   -
133   - while(tmp_idx > 0)
134   - {
135   - if(grid[tmp_idx] > 0)
136   - return true;
137   - tmp_idx -= grid_width;
138   - }
139   - return false;
140   - }
141   -
142   - bool
143   - ContourOperations::GetCollisionAtNorthEast(const long unsigned int idx,
144   - const short * grid,
145   - const unsigned int grid_width)
146   - {
147   - long int tmp_idx = idx - grid_width + 1;
148   -
149   - if(grid[tmp_idx] > 0)
150   - return true;
151   -
152   - while(tmp_idx > 0 && (tmp_idx % grid_width) != 0)
153   - {
154   - if(grid[tmp_idx] > 0)
155   - return true;
156   - tmp_idx = tmp_idx - grid_width + 1;
157   - }
158   - return false;
159   - }
160   -
161   - bool
162   - ContourOperations::GetCollisionAtEast(const long unsigned int idx,
163   - const short * grid,
164   - const unsigned int grid_width)
165   - {
166   - long int tmp_idx = idx + 1;
167   -
168   -
169   - if(grid[tmp_idx] > 0)
170   - return true;
171   -
172   - while((tmp_idx % grid_width) != 0)
173   - {
174   - if(grid[tmp_idx] > 0)
175   - return true;
176   - ++tmp_idx;
177   - }
178   - return false;
179   - }
180   -
181   - bool
182   - ContourOperations::GetCollisionAtSouthEast(const long unsigned int idx,
183   - const short * grid,
184   - const unsigned int grid_width,
185   - const unsigned int grid_height)
186   - {
187   - long int tmp_idx = idx + 1 + grid_width;
188   -
189   - if(grid[tmp_idx] > 0)
190   - return true;
191   -
192   - while(tmp_idx < (grid_width * grid_height) &&
193   - (tmp_idx % grid_width) != 0)
194   - {
195   - if(grid[tmp_idx] > 0)
196   - return true;
197   - tmp_idx = tmp_idx + 1 + grid_width;
198   - }
199   - return false;
200   - }
201   -
202   - bool
203   - ContourOperations::GetCollisionAtSouth(const long unsigned int idx,
204   - const short * grid,
205   - const unsigned int grid_width,
206   - const unsigned int grid_height)
207   - {
208   - long int tmp_idx = idx + grid_width;
209   -
210   - if(grid[tmp_idx] > 0)
211   - return true;
212   -
213   - while(tmp_idx < (grid_width * grid_height))
214   - {
215   - if(grid[tmp_idx] > 0)
216   - return true;
217   - tmp_idx += grid_width;
218   - }
219   - return false;
220   - }
221   -
222   - bool
223   - ContourOperations::GetCollisionAtSouthWest(const long unsigned int idx,
224   - const short * grid,
225   - const unsigned int grid_width,
226   - const unsigned int grid_height)
227   - {
228   - long int tmp_idx = idx + grid_width - 1;
229   -
230   - if(grid[tmp_idx] > 0)
231   - return true;
232   -
233   - while(tmp_idx < (grid_width * grid_height) &&
234   - (tmp_idx % grid_width) != grid_width - 1)
235   - {
236   - if(grid[tmp_idx] > 0)
237   - return true;
238   - tmp_idx = tmp_idx + grid_width - 1;
239   - }
240   - return false;
241   - }
242   -
243   - bool
244   - ContourOperations::GetCollisionAtWest(const long unsigned int idx,
245   - const short * grid,
246   - const unsigned int grid_width)
247   - {
248   - long int tmp_idx = idx - 1;
249   -
250   - if(grid[tmp_idx] > 0)
251   - return true;
252   -
253   - while((tmp_idx % grid_width) != grid_width - 1)
254   - {
255   - if(grid[tmp_idx] > 0)
256   - return true;
257   - tmp_idx -= 1;
258   - }
259   - return false;
260   - }
261   -
262   - bool
263   - ContourOperations::GetCollisionAtNorthWest(const long unsigned int idx,
264   - const short * grid,
265   - const unsigned int grid_width)
266   - {
267   - long int tmp_idx = idx - grid_width - 1;
268   -
269   - if(grid[tmp_idx] > 0)
270   - return true;
271   -
272   - while(tmp_idx > 0 && (tmp_idx % grid_width) != grid_width - 1)
273   - {
274   - if(grid[tmp_idx] > 0)
275   - return true;
276   - tmp_idx = tmp_idx - grid_width - 1;
277   - }
278   - return false;
279   - }
280   -
281   - bool
282   - ContourOperations::IsInternalPixel(const long unsigned int idx,
283   - const short * grid,
284   - const unsigned int grid_width,
285   - const unsigned int grid_height)
286   - {
287   - if(!GetCollisionAtNorth(idx, grid, grid_width))
288   - return false;
289   - if(!GetCollisionAtNorthEast(idx, grid, grid_width))
290   - return false;
291   - if(!GetCollisionAtEast(idx, grid, grid_width))
292   - return false;
293   - if(!GetCollisionAtSouthEast(idx, grid, grid_width, grid_height))
294   - return false;
295   - if(!GetCollisionAtSouth(idx, grid, grid_width, grid_height))
296   - return false;
297   - if(!GetCollisionAtSouthWest(idx, grid, grid_width, grid_height))
298   - return false;
299   - if(!GetCollisionAtWest(idx, grid, grid_width))
300   - return false;
301   - if(!GetCollisionAtNorthWest(idx, grid, grid_width))
302   - return false;
303   -
304   - return true;
305   - }
306   -
307   - void
308   - ContourOperations::UpdateSight(short * sight, const short direction)
309   - {
310   - sight[0] = (direction + 3) % 4; // look at the left
311   - sight[1] = direction; // look in front
312   - sight[2] = (direction + 1) % 4; // look at the right
313   - sight[3] = (direction + 2) % 4; // look behind
314   - }
315   -
316   - void
317   - ContourOperations::EncodeContourForTopPixel(long unsigned int& curr_mat_id,
318   - short& direction,
319   - short& pos,
320   - Contour& curr_contour,
321   - const unsigned int grid_width)
322   - {
323   - if(pos == 1)
324   - {
325   - if(direction==0)
326   - pos = 4;
327   - else if(direction == 1)
328   - {
329   - pos = 1;
330   - curr_contour.push_back(0);
331   - }
332   - }
333   - else if(pos == 2)
334   - {
335   - pos = 4;
336   - curr_contour.push_back(2);
337   - curr_contour.push_back(3);
338   - curr_contour.push_back(0);
339   - }
340   - else if(pos == 3)
341   - {
342   - pos = 4;
343   - curr_contour.push_back(3);
344   - curr_contour.push_back(0);
345   - }
346   - else if(pos == 4)
347   - curr_contour.push_back(0);
348   -
349   -
350   - curr_mat_id -= grid_width;
351   - direction = 0;
352   - }
353   -
354   - void
355   - ContourOperations::EncodeContourForRightPixel(long unsigned int& curr_mat_id,
356   - short& direction,
357   - short& pos,
358   - Contour& curr_contour)
359   - {
360   - if(pos == 1)
361   - curr_contour.push_back(1);
362   - else if(pos == 2)
363   - {
364   - if(direction == 1)
365   - pos = 1;
366   - else if(direction == 2)
367   - curr_contour.push_back(1);
368   - }
369   - else if(pos == 3)
370   - {
371   - pos = 1;
372   - curr_contour.push_back(3);
373   - curr_contour.push_back(0);
374   - curr_contour.push_back(1);
375   - }
376   - else if(pos == 4)
377   - {
378   - pos = 1;
379   - curr_contour.push_back(0);
380   - curr_contour.push_back(1);
381   - }
382   -
383   - curr_mat_id++;
384   - direction = 1;
385   - }
386   -
387   - void
388   - ContourOperations::EncodeContourForBottomPixel(long unsigned int& curr_mat_id,
389   - short& direction,
390   - short& pos,
391   - Contour& curr_contour,
392   - const unsigned int grid_width)
393   - {
394   - if(pos == 1)
395   - {
396   - pos = 2;
397   - curr_contour.push_back(1);
398   - curr_contour.push_back(2);
399   - }
400   - else if(pos == 2)
401   - curr_contour.push_back(2);
402   - else if(pos == 3)
403   - {
404   - if(direction == 2)
405   - pos = 2;
406   - else if(direction == 3)
407   - curr_contour.push_back(2);
408   - }
409   - else if(pos == 4)
410   - {
411   - pos = 2;
412   - curr_contour.push_back(0);
413   - curr_contour.push_back(1);
414   - curr_contour.push_back(2);
415   - }
416   -
417   - curr_mat_id += grid_width;
418   - direction = 2;
419   - }
420   -
421   - void
422   - ContourOperations::EncodeContourForLeftPixel(long unsigned int& curr_mat_id,
423   - short& direction,
424   - short& pos,
425   - Contour& curr_contour)
426   - {
427   - if(pos == 1)
428   - {
429   - pos = 3;
430   - curr_contour.push_back(1);
431   - curr_contour.push_back(2);
432   - curr_contour.push_back(3);
433   - }
434   - else if(pos == 2)
435   - {
436   - pos = 3;
437   - curr_contour.push_back(2);
438   - curr_contour.push_back(3);
439   - }
440   - else if(pos == 3)
441   - curr_contour.push_back(3);
442   - else if(pos == 4)
443   - {
444   - if(direction == 3)
445   - pos = 3;
446   - else if(direction == 0)
447   - {
448   - pos = 4;
449   - curr_contour.push_back(3);
450   - }
451   - }
452   -
453   - curr_mat_id --;
454   - direction = 3;
455   - }
456   -
457   - Contour
458   - ContourOperations::CreateContourFromBorderPixels(const long unsigned int id,
459   - const BoundingBox& bbox,
460   - const short * grid,
461   - const unsigned int width)
462   - {
463   - /* Location on one of the 4 corners of a pixel */
464   - short direction, pos;
465   - /* Field of view */
466   - short sight[4];
467   -
468   - long unsigned int start_mat_id = ToBoundingBoxCoordinates(id, bbox, width);
469   - long unsigned int curr_mat_id = start_mat_id;
470   -
471   - /* New contour */
472   - Contour curr_contour;
473   -
474   - // Build the first move
475   - long int neighbors[4];
476   - FOURNeighborhood(neighbors, curr_mat_id, bbox.m_W, bbox.m_H);
477   -
478   - // Only 2 cases: neighbor at the right or at the bottom
479   - if(grid[neighbors[1]] > 0) // right
480   - {
481   - pos = 1; // top left corner of the pixel
482   - curr_mat_id++; // go to the pixel at the right
483   - direction = 1; // direction is along the right
484   - curr_contour.push_back(1); // Add the move to the right
485   - }
486   - else if(grid[neighbors[2]] > 0) // bottom
487   - {
488   - pos = 2; // top right corner of the pixel
489   - curr_mat_id += bbox.m_W; // go to the pixel at the bottom
490   - direction = 2; // direction is along the bottom
491   - curr_contour.push_back(1); // add move to the right
492   - curr_contour.push_back(2); // add move to the bottom
493   - }
494   -
495   - // Keep going this same reasonning until we reach the start pixel (start_mat_id)
496   - while(curr_mat_id != start_mat_id)
497   - {
498   - UpdateSight(sight, direction);
499   - FOURNeighborhood(neighbors, curr_mat_id, bbox.m_W, bbox.m_H);
500   -
501   - // All the cases are possible: top, right, bottom, left
502   - for(short d=0; d<4; d++)
503   - {
504   - if(neighbors[sight[d]] > -1)
505   - {
506   - if(grid[neighbors[sight[d]]] > 0)
507   - {
508   - if(sight[d] == 0)
509   - {
510   - EncodeContourForTopPixel(curr_mat_id, direction, pos, curr_contour, bbox.m_W);
511   - break;
512   - }
513   - else if(sight[d] == 1)
514   - {
515   - EncodeContourForRightPixel(curr_mat_id, direction, pos, curr_contour);
516   - break;
517   - }
518   - else if(sight[d] == 2)
519   - {
520   - EncodeContourForBottomPixel(curr_mat_id, direction, pos, curr_contour, bbox.m_W);
521   - break;
522   - }
523   - else if(sight[d] == 3)
524   - {
525   - EncodeContourForLeftPixel(curr_mat_id, direction, pos, curr_contour);
526   - break;
527   - }
528   - }
529   - }
530   - }
531   -
532   - // It is possible to reach the start pixel whithout finishing to encode the whole contour.
533   - // 00111
534   - // 11100
535   - // In this case the direction value is always 3, the position value is always 3
536   - // We have to check if there is a neighbor at left of the current direction
537   - if(curr_mat_id == start_mat_id)
538   - {
539   - if(pos == 3 && direction == 3)
540   - {
541   - UpdateSight(sight, direction);
542   - FOURNeighborhood(neighbors, curr_mat_id, bbox.m_W, bbox.m_H);
543   -
544   - if(neighbors[sight[0]] > -1)
545   - {
546   - if(grid[neighbors[sight[0]]] == 1)
547   - {
548   - pos = 3;
549   - direction = 2;
550   - curr_contour.push_back(2);
551   - curr_mat_id += bbox.m_W;
552   - }
553   - }
554   - }
555   - }
556   - }
557   -
558   - // We reach the start pixel but maybe we did not encode the whole contour
559   - if(pos == 3)
560   - {
561   - curr_contour.push_back(3);
562   - curr_contour.push_back(0);
563   - }
564   - else if(pos == 4)
565   - curr_contour.push_back(0);
566   -
567   - return curr_contour;
568   - }
569   -
570   - Contour
571   - ContourOperations::MergeContours(const long unsigned int start_id1,
572   - const long unsigned int start_id2,
573   - const Contour& c1,
574   - const Contour& c2,
575   - const BoundingBox& bb,
576   - const unsigned int width,
577   - const unsigned int height)
578   - {
579   - /* Grid with the dimension of the bounding box initialized to 0 */
580   - short matrix_bbox[bb.m_W * bb.m_H];
581   - memset(matrix_bbox, 0, bb.m_W * bb.m_H * sizeof(short));
582   -
583   - /* Generate the border pixels wrt to c1 and c2 */
584   - PixelList pixels1 = GenerateBorderPixels(start_id1, c1, width);
585   - {
586   - PixelList pixels2 = GenerateBorderPixels(start_id2, c2, width);
587   - pixels1.insert(pixels1.end(), pixels2.begin(), pixels2.end());
588   - }
589   -
590   - /* Each case of the grid where a pixel is located is set to 1 */
591   - for(PixelConstIterator pit = pixels1.begin(); pit != pixels1.end(); ++pit)
592   - matrix_bbox[ToBoundingBoxCoordinates(*pit, bb, width)] = 1;
593   -
594   - /* Remove internal pixels */
595   - long unsigned int bb_id;
596   - bool is_internal;
597   - long int neighbors[8];
598   - for(long unsigned int y = 1; y < bb.m_H - 1; ++y)
599   - {
600   - for(long unsigned int x = 1; x < bb.m_W - 1; ++x)
601   - {
602   - bb_id = y*bb.m_W + x;
603   - is_internal = true;
604   - if(matrix_bbox[bb_id] > 0)
605   - {
606   - EIGHTNeighborhood(neighbors, bb_id, bb.m_W, bb.m_H);
607   - for(short j = 0; j < 8; ++j)
608   - {
609   - if(matrix_bbox[neighbors[j]] < 1)
610   - {
611   - is_internal = false;
612   - break;
613   - }
614   - }
615   -
616   - if(is_internal)
617   - matrix_bbox[bb_id] = 2;
618   - }
619   - }
620   - }
621   -
622   - for(long unsigned int i = 0; i < bb.m_H * bb.m_W; ++i)
623   - {
624   - if(matrix_bbox[i] > 1)
625   - matrix_bbox[i] = 0;
626   - }
627   -
628   - /* Create a new contour */
629   - return CreateContourFromBorderPixels(start_id1, bb, matrix_bbox, width);
630   -
631   - }
632   -
633   - long unsigned int
634   - ContourOperations::ToImageCoordinates(const long unsigned int id,
635   - const BoundingBox& bbox,
636   - const unsigned int width)
637   - {
638   - long unsigned int bbox_y = id / bbox.m_W;
639   - long unsigned int bbox_x = id % bbox.m_W;
640   - long unsigned int img_x = bbox.m_UX + bbox_x;
641   - long unsigned int img_y = bbox.m_UY + bbox_y;
642   - return (img_y * width + img_x);
643   - }
644   -
645   - typename ContourOperations::PixelList
646   - ContourOperations::GeneratePixels(long unsigned int id,
647   - const Contour& contour,
648   - const BoundingBox& bbox,
649   - const unsigned int width)
650   - {
651   - // Generate the bounding box grid
652   - short matrix_bbox[bbox.m_W * bbox.m_H];
653   - memset(matrix_bbox, 0, bbox.m_W * bbox.m_H * sizeof(short));
654   -
655   - PixelList pixels;
656   -
657   - /* Each case of the grid where a pixel is located is set to 1 */
658   - {
659   - // Generate the border pixels
660   - PixelList border_pixels = GenerateBorderPixels(id, contour, width);
661   -
662   - for(PixelConstIterator pit = border_pixels.begin();
663   - pit != border_pixels.end(); ++pit)
664   - matrix_bbox[ToBoundingBoxCoordinates(*pit, bbox, width)] = 1;
665   -
666   - pixels.insert(pixels.end(), border_pixels.begin(), border_pixels.end());
667   - }
668   -
669   - /* For each case of the grid we determine if it is inside or outside
670   - of the region */
671   - long unsigned int bb_id;
672   - for(long unsigned int y = 1; y < bbox.m_H - 1; ++y)
673   - {
674   - for(long unsigned int x = 1; x < bbox.m_W - 1; ++x)
675   - {
676   - bb_id = y*bbox.m_W + x;
677   - if(matrix_bbox[bb_id] < 1)
678   - {
679   - if(IsInternalPixel(bb_id, matrix_bbox, bbox.m_W, bbox.m_H))
680   - {
681   - matrix_bbox[bb_id] = 1;
682   - pixels.push_back(ToImageCoordinates(bb_id, bbox, width));
683   - }
684   - }
685   - }
686   - }
687   - return pixels;
688   - }
689   -} // end of namespace lsrm
Code/lsrmContourOperations.h
... ... @@ -1,305 +0,0 @@
1   -#ifndef __LSRM_CONTOUR_OPERATIONS_H
2   -#define __LSRM_CONTOUR_OPERATIONS_H
3   -#include "lsrmDataStructures.h"
4   -#include "lsrmNeighborhood.h"
5   -#include <cstring>
6   -#include <algorithm>
7   -
8   -namespace lsrm
9   -{
10   - class ContourOperations
11   - {
12   - public:
13   -
14   - /* Some convenient typedefs */
15   - typedef std::vector<long unsigned int> PixelList;
16   - typedef typename PixelList::iterator PixelIterator;
17   - typedef typename PixelList::const_iterator PixelConstIterator;
18   -
19   - /*
20   - * Given two rectangular bounding boxes,
21   - * it returns the bounding box which is
22   - * the union of both bounding boxes.
23   - *
24   - * @params
25   - * const BoundingBox& bb1 : reference to the first bounding box
26   - * const BoundingBox& bb2 : reference to the second bounding box
27   - * @return the union of bb1 and bb2.
28   - */
29   - static BoundingBox MergeBoundingBoxes(const BoundingBox& bb1,
30   - const BoundingBox& bb2);
31   -
32   - /*
33   - * Given the coordinates of the pixel in the current image, the
34   - * bounding box containing it and the width of the image, it returns
35   - * the coordinates of the pixel in the referential of the bounding box.
36   - *
37   - * @params
38   - * const long unsigned int id : coordinates of the pixel in the image
39   - * referential.
40   - * const BoundingBox& bb : bounding box containing the pixel
41   - * const unsigned int width : width of the image
42   - * @return the coordinates of the pixel in the bounding box referential.
43   - */
44   - static long unsigned int ToBoundingBoxCoordinates(const long unsigned int id,
45   - const BoundingBox& bb,
46   - const unsigned int width);
47   -
48   -
49   - /*
50   - * Given the contour, the first pixel coordinates
51   - * and the width of the current image,
52   - * it returns the list of the border pixels.
53   - *
54   - * @params
55   - * const long unsigned int id : coordinates of the first pixel.
56   - * const Contour& contour : reference to the contour.
57   - * const unsigned int width : width of the current image.
58   - */
59   - static PixelList GenerateBorderPixels(long unsigned int id,
60   - const Contour& contour,
61   - const unsigned int width);
62   -
63   - /*
64   - * Return true if it exists a pixel in the grid at the north
65   - * of the current pixel.
66   - *
67   - * @params
68   - * const long unsigned int idx : coordinates of the current pixel
69   - * const short * grid : grid
70   - * const unsigned int grid_width : width of the grid
71   - */
72   - static bool GetCollisionAtNorth(const long unsigned int idx,
73   - const short * grid,
74   - const unsigned int grid_width);
75   -
76   - /*
77   - * Return true if it exists a pixel in the grid at the north East
78   - * of the current pixel.
79   - *
80   - * @params
81   - * const long unsigned int idx : coordinates of the current pixel
82   - * const short * grid : grid
83   - * const unsigned int grid_width : width of the grid
84   - */
85   - static bool GetCollisionAtNorthEast(const long unsigned int idx,
86   - const short * grid,
87   - const unsigned int grid_width);
88   -
89   - /*
90   - * Return true if it exists a pixel in the grid at the East
91   - * of the current pixel.
92   - *
93   - * @params
94   - * const long unsigned int idx : coordinates of the current pixel
95   - * const short * grid : grid
96   - * const unsigned int grid_width : width of the grid
97   - */
98   - static bool GetCollisionAtEast(const long unsigned int idx,
99   - const short * grid,
100   - const unsigned int grid_width);
101   -
102   - /*
103   - * Return true if it exists a pixel in the grid at the South East
104   - * of the current pixel.
105   - *
106   - * @params
107   - * const long unsigned int idx : coordinates of the current pixel
108   - * const short * grid : grid
109   - * const unsigned int grid_width : width of the grid
110   - * const unsigned int grid_height : height of the grid
111   - */
112   - static bool GetCollisionAtSouthEast(const long unsigned int idx,
113   - const short * grid,
114   - const unsigned int grid_width,
115   - const unsigned int grid_height);
116   -
117   - /*
118   - * Return true if it exists a pixel in the grid at the South
119   - * of the current pixel.
120   - *
121   - * @params
122   - * const long unsigned int idx : coordinates of the current pixel
123   - * const short * grid : grid
124   - * const unsigned int grid_width : width of the grid
125   - * const unsigned int grid_height : height of the grid
126   - */
127   - static bool GetCollisionAtSouth(const long unsigned int idx,
128   - const short * grid,
129   - const unsigned int grid_width,
130   - const unsigned int grid_height);
131   -
132   - /*
133   - * Return true if it exists a pixel in the grid at the South West
134   - * of the current pixel.
135   - *
136   - * @params
137   - * const long unsigned int idx : coordinates of the current pixel
138   - * const short * grid : grid
139   - * const unsigned int grid_width : width of the grid
140   - * const unsigned int grid_height : height of the grid
141   - */
142   - static bool GetCollisionAtSouthWest(const long unsigned int idx,
143   - const short * grid,
144   - const unsigned int grid_width,
145   - const unsigned int grid_height);
146   -
147   - /*
148   - * Return true if it exists a pixel in the grid at the West
149   - * of the current pixel.
150   - *
151   - * @params
152   - * const long unsigned int idx : coordinates of the current pixel
153   - * const short * grid : grid
154   - * const unsigned int grid_width : width of the grid
155   - */
156   - static bool GetCollisionAtWest(const long unsigned int idx,
157   - const short * grid,
158   - const unsigned int grid_width);
159   -
160   - /*
161   - * Return true if it exists a pixel in the grid at the North West
162   - * of the current pixel.
163   - *
164   - * @params
165   - * const long unsigned int idx : coordinates of the current pixel
166   - * const short * grid : grid
167   - * const unsigned int grid_width : width of the grid
168   - */
169   - static bool GetCollisionAtNorthWest(const long unsigned int idx,
170   - const short * grid,
171   - const unsigned int grid_width);
172   -
173   - /*
174   - * Return true if the pixel located at the coordinates idx
175   - * is an internal pixel, i.e, in each direction there is a pixel.
176   - *
177   - * @params
178   - * const long unsigned int idx : coordinates of the current pixel
179   - * const short * grid : grid
180   - * const unsigned int grid_width : width of the grid
181   - * const unsigned int grid_height : height of the grid
182   - */
183   - static bool IsInternalPixel(const long unsigned int idx,
184   - const short * grid,
185   - const unsigned int grid_width,
186   - const unsigned int grid_height);
187   -
188   - /*
189   - * Given the direction of the front, it returns the field of view
190   - *
191   - * @params
192   - * short * sight : former field of view.
193   - * const short direction : new front direction.
194   - */
195   - static void UpdateSight(short * sight, const short direction);
196   -
197   -
198   - /*
199   - * Create a contour element when we move toward the top, right, bottom
200   - * or left wrt to the current pixel.
201   - *
202   - * @params
203   - * long unsigned int& curr_mat_id : pixel coordinates
204   - * short& direction : toward the direction we look
205   - * short& pos : location on the pixel
206   - * Contour& curr_contour : new contour to be created
207   - * const unsigned int grid_width : width of the grid
208   - */
209   - static void EncodeContourForTopPixel(long unsigned int& curr_mat_id,
210   - short& direction,
211   - short& pos,
212   - Contour& curr_contour,
213   - const unsigned int grid_width);
214   -
215   - static void EncodeContourForRightPixel(long unsigned int& curr_mat_id,
216   - short& direction,
217   - short& pos,
218   - Contour& curr_contour);
219   -
220   - static void EncodeContourForBottomPixel(long unsigned int& curr_mat_id,
221   - short& direction,
222   - short& pos,
223   - Contour& curr_contour,
224   - const unsigned int grid_width);
225   -
226   - static void EncodeContourForLeftPixel(long unsigned int& curr_mat_id,
227   - short& direction,
228   - short& pos,
229   - Contour& curr_contour);
230   -
231   - /*
232   - * Given the coordinates of the first pixel, the bounding box, the grid
233   - * with the location of the border pixels and the dimension of the referential
234   - * image, it returns a new contour
235   - *
236   - * @params
237   - * const long unsigned int idx : coordinates of the current pixel.
238   - * const BoundingBox& bbox : reference to the bounding box
239   - * const short * grid : grid
240   - * const unsigned int width : width of the current image.
241   - * cont unsigned int height : height of the current image.
242   - */
243   - static Contour CreateContourFromBorderPixels(const long unsigned int id,
244   - const BoundingBox& bbox,
245   - const short * grid,
246   - const unsigned int width);
247   -
248   -
249   - /*
250   - * Given two contours, the coordinates of their first pixels
251   - * and the bounding box wrapping both contours, it returns the
252   - * new contour by merging c1 and c2.
253   - *
254   - * @params
255   - * const long unsigned int start_id1 : coordinates of the first
256   - * pixel of the first contour
257   - * const long unsigned int start_id2 : coordinates of the first
258   - * pixel of the second contour
259   - * const Contour& c1 : reference to the first contour
260   - * const Contour& c2 : reference to the second contour
261   - * const BoundingBox& bb : reference to the bounding box wrapping c1 and c2
262   - * const unsigned int width : width of the current image
263   - * const unsigned int height : height of the current image
264   - */
265   - static Contour MergeContours(const long unsigned int start_id1,
266   - const long unsigned int start_id2,
267   - const Contour& c1,
268   - const Contour& c2,
269   - const BoundingBox& bb,
270   - const unsigned int width,
271   - const unsigned int height);
272   -
273   - /*
274   - * Given the coordinates of the pixel within its bounding box, it
275   - * returns the coordinates of this pixel in the current image.
276   - *
277   - * @params
278   - * const long unsigned int id : coordinates of the pixel within the bounding box
279   - * const BoundingBox& bbox : reference to the bounding box
280   - * const unsigned int width : width of the image
281   - * @return the coordinates of the pixel in the image.
282   - */
283