1 #ifndef vil_structuring_element_h_
2 #define vil_structuring_element_h_
3 //:
4 // \file
5 // \brief Structuring element for morphology represented as a list of non-zero pixels
6 // \author Tim Cootes
7 
8 #include <vector>
9 #include <iosfwd>
10 #include <cstddef>
11 #ifdef _MSC_VER
12 #  include <vcl_msvc_warnings.h>
13 #endif
14 
15 //: Structuring element for morphology represented as a list of non-zero pixels
16 // Elements in box bounded by [min_i(),max_i()][min_j(),max_j()]
17 // Non-zero pixels are given by (p_i[k],p_j[k])
18 class vil_structuring_element
19 {
20   //: i position of elements (i,j)
21   std::vector<int> p_i_;
22   //: j position of elements (i,j)
23   std::vector<int> p_j_;
24   //: Elements in box bounded by [min_i_,max_i_][min_j_,max_j]
25   int min_i_{0};
26   //: Elements in box bounded by [min_i_,max_i_][min_j_,max_j]
27   int max_i_{-1};
28   //: Elements in box bounded by [min_i_,max_i_][min_j_,max_j]
29   int min_j_{0};
30   //: Elements in box bounded by [min_i_,max_i_][min_j_,max_j]
31   int max_j_{-1};
32 
33 public:
34   vil_structuring_element() = default;
35 
36   //: Define elements { (p_i[k],p_j[k]) }
vil_structuring_element(const std::vector<int> & v_p_i,const std::vector<int> & v_p_j)37   vil_structuring_element(const std::vector<int>& v_p_i,const std::vector<int>& v_p_j)
38   { set(v_p_i,v_p_j); }
39 
40   //: Define elements { (p_i[k],p_j[k]) }
41   void set(const std::vector<int>& v_p_i,const std::vector<int>& v_p_j);
42 
43   //: Set to disk of radius r
44   //  Select pixels in disk s.t. x^x+y^y<r^r
45   void set_to_disk(double r);
46 
47   //: Set to line along i (ilo,0)..(ihi,0)
48   void set_to_line_i(int ilo, int ihi);
49 
50   //: Set to line along j (jlo,0)..(jhi,0)
51   void set_to_line_j(int jlo, int jhi);
52 
53   //: i position of elements (i,j)
p_i()54   const std::vector<int>& p_i() const { return p_i_; }
55   //: j position of elements (i,j)
p_j()56   const std::vector<int>& p_j() const { return p_j_; }
57 
58   //: Elements in box bounded by [min_i(),max_i()][min_j(),max_j()]
min_i()59   int min_i() const { return min_i_; }
60   //: Elements in box bounded by [min_i(),max_i()][min_j(),max_j()]
max_i()61   int max_i() const { return max_i_; }
62   //: Elements in box bounded by [min_i(),max_i()][min_j(),max_j()]
min_j()63   int min_j() const { return min_j_; }
64   //: Elements in box bounded by [min_i(),max_i()][min_j(),max_j()]
max_j()65   int max_j() const { return max_j_; }
66 };
67 
68 //: Write details to stream
69 std::ostream& operator<<(std::ostream&, const vil_structuring_element& element);
70 
71 //: Generate a list of offsets for use on image with istep,jstep
72 //  On exit offset[k] = element.p_i()[k]*istep +  element.p_j()[k]*jstep
73 //  Gives an efficient way of looping through all the pixels in the structuring element
74 void vil_compute_offsets(std::vector<std::ptrdiff_t>& offset,
75                          const vil_structuring_element& element,
76                          std::ptrdiff_t istep, std::ptrdiff_t jstep);
77 
78 #endif // vil_structuring_element_h_
79