cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
oniPlaneSegmentation.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5  $Id: $
6 
7  Author(s): Balazs Vagvolgyi
8  Created on: 2011
9 
10  (C) Copyright 2006-2011 Johns Hopkins University (JHU), All Rights
11  Reserved.
12 
13 --- begin cisst license - do not edit ---
14 
15 This software is provided "as is" under an open source license, with
16 no warranty. The complete license can be found in license.txt and
17 http://www.cisst.org/cisst/license.txt.
18 
19 --- end cisst license ---
20 */
21 
22 #ifndef _oniPlaneSegmentation_H
23 #define _oniPlaneSegmentation_H
24 
25 #include <cisstStereoVision.h>
26 
27 
29 {
30  unsigned char r;
31  unsigned char g;
32  unsigned char b;
33  unsigned short col;
34  unsigned short row;
35  float x;
36  float y;
37  float z;
38 };
39 
40 
41 class oniPlane
42 {
43 public:
44  oniPlane();
45  virtual ~oniPlane();
46 
47  void Allocate(unsigned int width, unsigned int height);
48  void Release();
49  void CopyOf(const oniPlane & other);
50  void RandomizeColor();
51 
52  int ID; // unique identifier
53  int Label; // label in PlaneLabels array
54  bool Used; // flag that specifies if plane is used or not
55  int Frame; // ID of video frame where plane was detected most recently
56  int First; // ID of video frame where plane was first detected
57  vctInt2 CoW; // center of weight
58  unsigned int Area; // number of pixels covered
59  svlRect Rect; // bounding rectangle
60  svlSampleImageMono8* Mask; // pixel mask
61  svlSampleImageMono16* Dist; // distance image (pixel value indicates distance from plane)
62  svlSampleImageMono32* Hist; // UV histogram
63  vctFloat4 Plane; // 3D plane equation
64  int GradX; // average gradient along X
65  int GradY; // average gradient along Y
66  unsigned int Peak; // gradient peak ID
68 };
69 
70 
72 {
73 public:
76 
77  void SetPlaneID(unsigned int planeid);
78  void SetPlaneDistanceThreshold(double threshold);
79  void SetColorMatchWeight(double weight);
80  void SetGradientHistogramThreshold(unsigned char threshold);
81  void SetPeakRadius(unsigned int radius);
82  void SetMinObjectArea(unsigned int minarea);
83  void SetGradientRadius(unsigned int radius);
84  void SetMergeThreshold(float threshold);
85  void SetMinPlaneArea(unsigned int minarea);
86  unsigned int GetPlaneID() const;
87  double GetPlaneDistanceThreshold() const;
88  double GetColorMatchWeight() const;
89  unsigned char GetGradientHistogramThreshold() const;
90  unsigned int GetPeakRadius() const;
91  unsigned int GetMinObjectArea() const;
92  unsigned int GetGradientRadius() const;
93  float GetMergeThreshold() const;
94  unsigned int GetMinPlaneArea() const;
95 
96  void GetPlaneIDs(vctDynamicVector<unsigned int> & id_vector) const;
97  void GetPlaneIDMap(svlSampleImageMono32 & id_map) const;
98  const oniPlane& GetPlane(unsigned int plane_id) const;
99  int GetPlaneCopy(oniPlane & plane, unsigned int plane_id) const;
100 
101  bool Process(svlSampleImageRGB* rgb,
102  svlSampleImage3DMap* pointcloud,
103  svlSampleImageRGB* visualized,
104  svlSampleImageMono16* planedistance,
105  svlSampleBlobs* planeobjects);
106 
107  bool GetUVHistogram(svlSampleImageRGB* uvhistogramimage);
108 
109  bool GetObjectVector(std::vector< std::vector<oniRGBCRXYZ> > & objects,
110  svlSampleImageRGB* rgb,
111  svlSampleImage3DMap* pointcloud,
112  svlSampleBlobs* planeobjects);
113 
114 private:
115  bool Initialize(svlSampleImageRGB* rgb,
116  svlSampleImage3DMap* pointcloud,
117  svlSampleImageRGB* visualized,
118  svlSampleImageMono16* planedistance,
119  svlSampleBlobs* planeobjects);
120 
121  void ComputeDepthGradientHistogram(vctDynamicMatrixRef<float> pointcloud,
123 
124  void NormalizeGradientHistogram(vctDynamicMatrixRef<unsigned int> histogram,
125  vctDynamicMatrixRef<unsigned char> norm_histogram);
126 
127  void ThresholdHistogram(svlSampleImageMono8* image);
128 
129  unsigned int FindGradientPeaks(vctDynamicMatrixRef<unsigned int> histogram,
131  svlSampleBlobs* segments,
132  const int radius);
133 
134  void LabelImage(svlSampleBlobs* segments, svlSampleImageMono8* gradlabels);
135 
136  unsigned int FindLargestSegments(svlSampleImageMono32* labels,
138  svlSampleBlobs* segments);
139 
140  void FitPlane(unsigned int planeid, svlSampleImage3DMap* points);
141 
142  unsigned int ConsolidatePlanes();
143 
144  void CalculateAverageGradientForPlane(unsigned int planeid, int & gradx, int & grady);
145 
146  void LabelObjects(unsigned int planeid);
147 
148  void CalculateColorHistogram(unsigned int planeid, svlSampleImageRGB* yuvimage);
149 
150  void FilterLabels(unsigned int planeid, svlSampleImageRGB* yuvimage);
151 
152  void ComputePlaneStats(int frameid);
153 
154  void ConsolidatePlaneLabels();
155 
156  void TrackPlanes();
157 
158  void DrawPlanes(svlSampleImageRGB* visualized);
159 
160  void VisualizePlaneObjects(svlSampleImageRGB* image,
161  svlSampleImageRGB* visualized,
162  svlSampleImageMono8* planelabels,
163  svlSampleImageMono32* objectlabels,
164  svlSampleBlobs* blobs);
165 
166  void CreateColorHistogramImage(svlSampleImageMono32* uvhistogram,
167  svlSampleImageRGB* uvhistogramimage);
168 
169  inline unsigned int sqrt_uint32(unsigned int value);
170 
171 private:
172  bool Initialized;
173 
174  unsigned int FrameCounter; // Zero based frame index
175  unsigned int PlaneIDCounter; // Zero based plane ID
176  unsigned int PlaneID; // ID of plane to be visualized
177  unsigned int PlaneDistanceThreshold;
178  unsigned int ColorMatchWeight;
179  unsigned char GradientHistogramThreshold;
180  unsigned int MinObjectArea;
181  unsigned int GradientRadius;
182  unsigned int PeakRadius;
183  unsigned int MaxPlaneMatchError;
184  float MergeThreshold;
185  unsigned int MaxPlaneCount;
186  unsigned int MinPlaneArea;
187 
188  svlSampleBlobs* Blobs;
189  svlSampleBlobs* GradientPeaks;
190  svlSampleImageMono8* GradientLabels;
191  svlSampleImageMono8* HistogramImage;
192  svlSampleImageMono8* TempHistogramImage;
193  svlSampleImageMono8* TempMask;
194  svlSampleImageMono32* HistogramLabels;
195  svlSampleImageMono32* TempHistogram;
196  svlSampleImageMono32* TempHistogram2;
197  svlSampleImageMono32* BlobLabels;
198  svlSampleImageMono32* PlaneLabels;
199  svlSampleImageMono32* PlaneGradientErrors;
200  svlSampleImageMono32* PlaneObjectLabels;
201  svlSampleImageRGB* YUVImage;
202  svlSampleImageRGB* UVHistogramImage;
203 
204  vctDynamicMatrix<short> GradX, GradY;
205  vctDynamicVector<vctFloat3> PlaneFitPoints;
206  vctDynamicVector<float> PlaneFitWeights;
207 
208  svlImageProcessing::Internals BlobDetectorInternals;
209  svlImageProcessing::Internals HistogramBlobInternals;
210  svlImageProcessing::Internals PlaneBlobInternals;
211 
212  unsigned int PeakCount;
213 
214  // Plane cache used as work buffer
215  vctDynamicVector<oniPlane> PlaneCache;
216  unsigned int PlaneCacheSize;
217 
218  // Plane history
219  vctDynamicVector<oniPlane> PlaneHistory;
220  vctDynamicVector<int> VisiblePlanePositions;
221  unsigned int NumberOfVisiblePlanes;
222  int PlaneHistoryPosition;
223 
224  oniPlane InvalidPlane;
225 };
226 
227 #endif // _oniPlaneSegmentation_H
228 
svlSampleImageMono32 * Hist
Definition: oniPlaneSegmentation.h:62
unsigned int GetMinPlaneArea() const
unsigned char g
Definition: oniPlaneSegmentation.h:31
float z
Definition: oniPlaneSegmentation.h:37
bool Process(svlSampleImageRGB *rgb, svlSampleImage3DMap *pointcloud, svlSampleImageRGB *visualized, svlSampleImageMono16 *planedistance, svlSampleBlobs *planeobjects)
bool GetUVHistogram(svlSampleImageRGB *uvhistogramimage)
unsigned int GetPeakRadius() const
unsigned int GetPlaneID() const
Definition: svlSampleBlobs.h:31
unsigned char GetGradientHistogramThreshold() const
double GetColorMatchWeight() const
vctFloat4 Plane
Definition: oniPlaneSegmentation.h:63
void SetMinPlaneArea(unsigned int minarea)
bool GetObjectVector(std::vector< std::vector< oniRGBCRXYZ > > &objects, svlSampleImageRGB *rgb, svlSampleImage3DMap *pointcloud, svlSampleBlobs *planeobjects)
vctInt2 CoW
Definition: oniPlaneSegmentation.h:57
int First
Definition: oniPlaneSegmentation.h:56
void SetPlaneID(unsigned int planeid)
unsigned char b
Definition: oniPlaneSegmentation.h:32
void Release()
Definition: oniPlaneSegmentation.h:28
int ID
Definition: oniPlaneSegmentation.h:52
void SetGradientHistogramThreshold(unsigned char threshold)
virtual ~oniPlane()
svlRect Rect
Definition: oniPlaneSegmentation.h:59
svlRGB Color
Definition: oniPlaneSegmentation.h:67
void GetPlaneIDs(vctDynamicVector< unsigned int > &id_vector) const
void SetColorMatchWeight(double weight)
void SetGradientRadius(unsigned int radius)
Definition: svlSampleImageCustom.h:37
bool Used
Definition: oniPlaneSegmentation.h:54
Definition: svlImageProcessing.h:38
int Label
Definition: oniPlaneSegmentation.h:53
int GetPlaneCopy(oniPlane &plane, unsigned int plane_id) const
void SetPlaneDistanceThreshold(double threshold)
void CopyOf(const oniPlane &other)
unsigned short col
Definition: oniPlaneSegmentation.h:33
svlSampleImageMono8 * Mask
Definition: oniPlaneSegmentation.h:60
float x
Definition: oniPlaneSegmentation.h:35
unsigned int Peak
Definition: oniPlaneSegmentation.h:66
void GetPlaneIDMap(svlSampleImageMono32 &id_map) const
unsigned int GetGradientRadius() const
void SetMergeThreshold(float threshold)
float GetMergeThreshold() const
void SetPeakRadius(unsigned int radius)
Definition: svlTypes.h:215
int GradX
Definition: oniPlaneSegmentation.h:64
Definition: oniPlaneSegmentation.h:41
unsigned char r
Definition: oniPlaneSegmentation.h:30
unsigned int Area
Definition: oniPlaneSegmentation.h:58
unsigned short row
Definition: oniPlaneSegmentation.h:34
unsigned int GetMinObjectArea() const
double GetPlaneDistanceThreshold() const
Definition: svlTypes.h:118
Definition: oniPlaneSegmentation.h:71
svlSampleImageMono16 * Dist
Definition: oniPlaneSegmentation.h:61
void Allocate(unsigned int width, unsigned int height)
void RandomizeColor()
const oniPlane & GetPlane(unsigned int plane_id) const
float y
Definition: oniPlaneSegmentation.h:36
void SetMinObjectArea(unsigned int minarea)
int Frame
Definition: oniPlaneSegmentation.h:55
int GradY
Definition: oniPlaneSegmentation.h:65