Point Cloud Library (PCL)  1.14.1-dev
mssegmentation.h
1 // this file is extracted from OpenCV/modules/gpu/src/mssegmentation.cpp
2 // to get access to the intermediate results of meanShiftSegmentation()
3 // minor changes to compile correctly with pcl::cuda and namespace reorganization
4 
5 /*M///////////////////////////////////////////////////////////////////////////////////////
6 //
7 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
8 //
9 // By downloading, copying, installing or using the software you agree to this license.
10 // If you do not agree to this license, do not download, install,
11 // copy or use the software.
12 //
13 //
14 // License Agreement
15 // For Open Source Computer Vision Library
16 //
17 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
18 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
19 // Third party copyrights are property of their respective owners.
20 //
21 // Redistribution and use in source and binary forms, with or without modification,
22 // are permitted provided that the following conditions are met:
23 //
24 // * Redistribution's of source code must retain the above copyright notice,
25 // this list of conditions and the following disclaimer.
26 //
27 // * Redistribution's in binary form must reproduce the above copyright notice,
28 // this list of conditions and the following disclaimer in the documentation
29 // and/or other materials provided with the distribution.
30 //
31 // * The name of the copyright holders may not be used to endorse or promote products
32 // derived from this software without specific prior written permission.
33 //
34 // This software is provided by the copyright holders and contributors "as is" and
35 // any express or implied warranties, including, but not limited to, the implied
36 // warranties of merchantability and fitness for a particular purpose are disclaimed.
37 // In no event shall the Intel Corporation or contributors be liable for any direct,
38 // indirect, incidental, special, exemplary, or consequential damages
39 // (including, but not limited to, procurement of substitute goods or services;
40 // loss of use, data, or profits; or business interruption) however caused
41 // and on any theory of liability, whether in contract, strict liability,
42 // or tort (including negligence or otherwise) arising in any way out of
43 // the use of this software, even if advised of the possibility of such damage.
44 //
45 //M*/
46 
47 #pragma once
48 
49 #include <pcl/pcl_exports.h>
50 
51 #include <vector>
52 #include <opencv2/core/core.hpp>
53 #include <opencv2/gpu/gpu.hpp>
54 
55 namespace pcl
56 {
57 namespace cuda
58 {
59 namespace detail
60 {
61 
62 //
63 // Declarations
64 //
65 
66 class DjSets
67 {
68 public:
69  DjSets(int n);
70  int find(int elem);
71  int merge(int set1, int set2);
72 
73  void init (int n);
74 
75  std::vector<int> parent;
76  std::vector<int> rank;
77  std::vector<int> size;
78 private:
79  DjSets(const DjSets&);
80  void operator =(const DjSets&);
81 };
82 
83 
84 template <typename T>
85 struct GraphEdge
86 {
87  GraphEdge() {}
88  GraphEdge(int to, int next, const T& val) : to(to), next(next), val(val) {}
89  int to;
90  int next;
91  T val;
92 };
93 
94 
95 template <typename T>
96 class Graph
97 {
98 public:
99  using Edge = GraphEdge<T>;
100 
101  Graph(int numv, int nume_max);
102 
103  void addEdge(int from, int to, const T& val=T());
104 
105  std::vector<int> start;
106  std::vector<Edge> edges;
107 
108  int numv;
109  int nume_max;
110  int nume;
111 private:
112  Graph(const Graph&);
113  void operator =(const Graph&);
114 };
115 
116 
118 {
120  SegmLinkVal(int dr, int dsp) : dr(dr), dsp(dsp) {}
121  bool operator <(const SegmLinkVal& other) const
122  {
123  return dr + dsp < other.dr + other.dsp;
124  }
125  int dr;
126  int dsp;
127 };
128 
129 
130 struct SegmLink
131 {
132  SegmLink() {}
133  SegmLink(int from, int to, const SegmLinkVal& val)
134  : from(from), to(to), val(val) {}
135  bool operator <(const SegmLink& other) const
136  {
137  return val < other.val;
138  }
139  int from;
140  int to;
142 };
143 
144 //
145 // Implementation
146 //
147 
149 {
150  init (n);
151 }
152 
153 
154 inline int DjSets::find(int elem)
155 {
156  int set = elem;
157  while (set != parent[set])
158  set = parent[set];
159  while (elem != parent[elem])
160  {
161  int next = parent[elem];
162  parent[elem] = set;
163  elem = next;
164  }
165  return set;
166 }
167 
168 inline void DjSets::init(int n)
169 {
170  parent.resize(n);
171  rank.resize(n, 0);
172  size.resize(n, 1);
173  for (int i = 0; i < n; ++i)
174  parent[i] = i;
175 }
176 
177 inline int DjSets::merge(int set1, int set2)
178 {
179  if (rank[set1] < rank[set2])
180  {
181  parent[set1] = set2;
182  size[set2] += size[set1];
183  return set2;
184  }
185  if (rank[set2] < rank[set1])
186  {
187  parent[set2] = set1;
188  size[set1] += size[set2];
189  return set1;
190  }
191  parent[set1] = set2;
192  rank[set2]++;
193  size[set2] += size[set1];
194  return set2;
195 }
196 
197 
198 template <typename T>
199 Graph<T>::Graph(int numv, int nume_max) : start(numv, -1), edges(nume_max)
200 {
201  this->numv = numv;
202  this->nume_max = nume_max;
203  nume = 0;
204 }
205 
206 
207 template <typename T>
208 inline void Graph<T>::addEdge(int from, int to, const T& val)
209 {
210  edges[nume] = Edge(to, start[from], val);
211  start[from] = nume;
212  nume++;
213 }
214 
215 
216 inline int pix(int y, int x, int ncols)
217 {
218  return y * ncols + x;
219 }
220 
221 
222 inline int sqr(int x)
223 {
224  return x * x;
225 }
226 
227 
228 inline int dist2(const cv::Vec4b& lhs, const cv::Vec4b& rhs)
229 {
230  return sqr(lhs[0] - rhs[0]) + sqr(lhs[1] - rhs[1]) + sqr(lhs[2] - rhs[2]);
231 }
232 
233 
234 inline int dist2(const cv::Vec2s& lhs, const cv::Vec2s& rhs)
235 {
236  return sqr(lhs[0] - rhs[0]) + sqr(lhs[1] - rhs[1]);
237 }
238 
239 } // namespace
240 
241 PCL_EXPORTS void meanShiftSegmentation(const cv::gpu::GpuMat& src, cv::Mat& dst, int sp, int sr, int minsize, detail::DjSets &comps, cv::TermCriteria criteria = cv::TermCriteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS, 5, 1) );
242 
243 } // namespace
244 } // namespace
std::vector< int > rank
std::vector< int > parent
std::vector< int > size
int merge(int set1, int set2)
Graph(int numv, int nume_max)
void addEdge(int from, int to, const T &val=T())
std::vector< int > start
std::vector< Edge > edges
int pix(int y, int x, int ncols)
int dist2(const cv::Vec4b &lhs, const cv::Vec4b &rhs)
PCL_EXPORTS void meanShiftSegmentation(const cv::gpu::GpuMat &src, cv::Mat &dst, int sp, int sr, int minsize, detail::DjSets &comps, cv::TermCriteria criteria=cv::TermCriteria(cv::TermCriteria::MAX_ITER+cv::TermCriteria::EPS, 5, 1))
#define PCL_EXPORTS
Definition: pcl_macros.h:323
GraphEdge(int to, int next, const T &val)