vil_structuring_element.cxx
Go to the documentation of this file.
1 // This is core/vil/algo/vil_structuring_element.cxx
2 #include <iostream>
4 //:
5 // \file
6 // \brief Structuring element for morphology represented as a list of non-zero pixels
7 // \author Tim Cootes
8 
9 #include <cassert>
10 #ifdef _MSC_VER
11 # include <vcl_msvc_warnings.h>
12 #endif
13 
14 //: Define elements { (p_i[k],p_j[k]) }
15 void vil_structuring_element::set(const std::vector<int>& v_p_i,const std::vector<int>& v_p_j)
16 {
17  assert(v_p_i.size()==v_p_j.size());
18  assert(v_p_i.size()>0);
19  p_i_ = v_p_i;
20  p_j_ = v_p_j;
21 
22  max_i_=min_i_ = v_p_i[0];
23  max_j_=min_j_ = v_p_j[0];
24  for (unsigned int k=1;k<v_p_i.size();++k)
25  {
26  if (v_p_i[k]<min_i_) min_i_=v_p_i[k];
27  else if (v_p_i[k]>max_i_) max_i_=v_p_i[k];
28 
29  if (v_p_j[k]<min_j_) min_j_=v_p_j[k];
30  else if (v_p_j[k]>max_j_) max_j_=v_p_j[k];
31  }
32 }
33 
34 //: Set to disk of radius r
35 // Select pixels in disk s.t. x^x+y^y<r^r
37 {
38  std::vector<int> px,py;
39  double r2 = r*r;
40  int r0 = int(r+1);
41  for (int j=-r0;j<=r0;++j)
42  for (int i=-r0;i<=r0;++i)
43  if (i*i+j*j<r2) { px.push_back(i); py.push_back(j); }
44  set(px,py);
45 }
46 
47 //: Set to line along i (ilo,0)..(ihi,0)
49 {
50  p_i_.resize(1+ihi-ilo);
51  p_j_.resize(1+ihi-ilo);
52  for (int i = ilo;i<=ihi;++i)
53  {
54  p_i_[i-ilo]=i; p_j_[i-ilo]=0;
55  }
56 
57  min_i_ = ilo; max_i_ = ihi;
58  min_j_ = 0; max_j_ = 0;
59 }
60 
61 //: Set to line along j (jlo,0)..(jhi,0)
63 {
64  p_i_.resize(1+jhi-jlo);
65  p_j_.resize(1+jhi-jlo);
66  for (int j = jlo;j<=jhi;++j)
67  {
68  p_i_[j-jlo]=0; p_j_[j-jlo]=j;
69  }
70 
71  min_i_ = 0; max_i_ = 0;
72  min_j_ = jlo; max_j_ = jhi;
73 }
74 
75 //: Write details to stream
76 std::ostream& operator<<(std::ostream& os, const vil_structuring_element& element)
77 {
78  os<<"Bounds ["
79  <<element.min_i()<<','<<element.max_i()<<"]["
80  <<element.min_j()<<','<<element.max_j()<<"] Points: ";
81  for (unsigned int k=0;k<element.p_i().size();++k)
82  os<<'('<<element.p_i()[k]<<','<<element.p_j()[k]<<") ";
83  return os;
84 }
85 
86 //: Generate a list of offsets for use on image with istep,jstep
87 // Gives an efficient way of looping through all the pixels in the structuring element
88 void vil_compute_offsets(std::vector<std::ptrdiff_t>& offset, const vil_structuring_element& element,
89  std::ptrdiff_t istep, std::ptrdiff_t jstep)
90 {
91  unsigned n = element.p_i().size();
92  offset.resize(n);
93  for (unsigned int k=0;k<n;++k)
94  offset[k] = static_cast<std::ptrdiff_t>(element.p_i()[k]*istep + element.p_j()[k]*jstep);
95 }
Structuring element for morphology represented as a list of non-zero pixels.
int max_i() const
Elements in box bounded by [min_i(),max_i()][min_j(),max_j()].
const std::vector< int > & p_j() const
j position of elements (i,j).
std::vector< int > p_i_
i position of elements (i,j).
Structuring element for morphology represented as a list of non-zero pixels.
int max_j() const
Elements in box bounded by [min_i(),max_i()][min_j(),max_j()].
int min_i_
Elements in box bounded by [min_i_,max_i_][min_j_,max_j].
void set_to_disk(double r)
Set to disk of radius r.
int min_j_
Elements in box bounded by [min_i_,max_i_][min_j_,max_j].
int max_j_
Elements in box bounded by [min_i_,max_i_][min_j_,max_j].
void vil_compute_offsets(std::vector< std::ptrdiff_t > &offset, const vil_structuring_element &element, std::ptrdiff_t istep, std::ptrdiff_t jstep)
Generate a list of offsets for use on image with istep,jstep.
std::ostream & operator<<(std::ostream &os, const vil_structuring_element &element)
Write details to stream.
void set(const std::vector< int > &v_p_i, const std::vector< int > &v_p_j)
Define elements { (p_i[k],p_j[k]) }.
void set_to_line_i(int ilo, int ihi)
Set to line along i (ilo,0)..(ihi,0).
int min_j() const
Elements in box bounded by [min_i(),max_i()][min_j(),max_j()].
std::vector< int > p_j_
j position of elements (i,j).
const std::vector< int > & p_i() const
i position of elements (i,j).
int min_i() const
Elements in box bounded by [min_i(),max_i()][min_j(),max_j()].
void set_to_line_j(int jlo, int jhi)
Set to line along j (jlo,0)..(jhi,0).
int max_i_
Elements in box bounded by [min_i_,max_i_][min_j_,max_j].