Point Cloud Library (PCL)  1.9.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 using namespace std;
56 
57 namespace pcl
58 {
59 namespace cuda
60 {
61 namespace detail
62 {
63 
64 //
65 // Declarations
66 //
67 
68 class DjSets
69 {
70 public:
71  DjSets(int n);
72  int find(int elem);
73  int merge(int set1, int set2);
74 
75  void init (int n);
76 
77  std::vector<int> parent;
78  std::vector<int> rank;
79  std::vector<int> size;
80 private:
81  DjSets(const DjSets&);
82  void operator =(const DjSets&);
83 };
84 
85 
86 template <typename T>
87 struct GraphEdge
88 {
89  GraphEdge() {}
90  GraphEdge(int to, int next, const T& val) : to(to), next(next), val(val) {}
91  int to;
92  int next;
93  T val;
94 };
95 
96 
97 template <typename T>
98 class Graph
99 {
100 public:
102 
103  Graph(int numv, int nume_max);
104 
105  void addEdge(int from, int to, const T& val=T());
106 
107  std::vector<int> start;
108  std::vector<Edge> edges;
109 
110  int numv;
111  int nume_max;
112  int nume;
113 private:
114  Graph(const Graph&);
115  void operator =(const Graph&);
116 };
117 
118 
120 {
122  SegmLinkVal(int dr, int dsp) : dr(dr), dsp(dsp) {}
123  bool operator <(const SegmLinkVal& other) const
124  {
125  return dr + dsp < other.dr + other.dsp;
126  }
127  int dr;
128  int dsp;
129 };
130 
131 
132 struct SegmLink
133 {
134  SegmLink() {}
135  SegmLink(int from, int to, const SegmLinkVal& val)
136  : from(from), to(to), val(val) {}
137  bool operator <(const SegmLink& other) const
138  {
139  return val < other.val;
140  }
141  int from;
142  int to;
144 };
145 
146 //
147 // Implementation
148 //
149 
150 DjSets::DjSets(int n)
151 {
152  init (n);
153 }
154 
155 
156 inline int DjSets::find(int elem)
157 {
158  int set = elem;
159  while (set != parent[set])
160  set = parent[set];
161  while (elem != parent[elem])
162  {
163  int next = parent[elem];
164  parent[elem] = set;
165  elem = next;
166  }
167  return set;
168 }
169 
170 inline void DjSets::init(int n)
171 {
172  parent.resize(n);
173  rank.resize(n, 0);
174  size.resize(n, 1);
175  for (int i = 0; i < n; ++i)
176  parent[i] = i;
177 }
178 
179 inline int DjSets::merge(int set1, int set2)
180 {
181  if (rank[set1] < rank[set2])
182  {
183  parent[set1] = set2;
184  size[set2] += size[set1];
185  return set2;
186  }
187  if (rank[set2] < rank[set1])
188  {
189  parent[set2] = set1;
190  size[set1] += size[set2];
191  return set1;
192  }
193  parent[set1] = set2;
194  rank[set2]++;
195  size[set2] += size[set1];
196  return set2;
197 }
198 
199 
200 template <typename T>
201 Graph<T>::Graph(int numv, int nume_max) : start(numv, -1), edges(nume_max)
202 {
203  this->numv = numv;
204  this->nume_max = nume_max;
205  nume = 0;
206 }
207 
208 
209 template <typename T>
210 inline void Graph<T>::addEdge(int from, int to, const T& val)
211 {
212  edges[nume] = Edge(to, start[from], val);
213  start[from] = nume;
214  nume++;
215 }
216 
217 
218 inline int pix(int y, int x, int ncols)
219 {
220  return y * ncols + x;
221 }
222 
223 
224 inline int sqr(int x)
225 {
226  return x * x;
227 }
228 
229 
230 inline int dist2(const cv::Vec4b& lhs, const cv::Vec4b& rhs)
231 {
232  return sqr(lhs[0] - rhs[0]) + sqr(lhs[1] - rhs[1]) + sqr(lhs[2] - rhs[2]);
233 }
234 
235 
236 inline int dist2(const cv::Vec2s& lhs, const cv::Vec2s& rhs)
237 {
238  return sqr(lhs[0] - rhs[0]) + sqr(lhs[1] - rhs[1]);
239 }
240 
241 } // namespace
242 
243 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) );
244 
245 } // namespace
246 } // namespace
int pix(int y, int x, int ncols)
std::vector< int > rank
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
std::vector< Edge > edges
int dist2(const cv::Vec4b &lhs, const cv::Vec4b &rhs)
std::vector< int > parent
std::vector< int > start
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))
GraphEdge(int to, int next, const T &val)
std::vector< int > size
void addEdge(int from, int to, const T &val=T())
#define PCL_EXPORTS
Definition: pcl_macros.h:241