vigra - vigra/cellimage.hxx Source File

[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

cellimage.hxx |

1 /************************************************************************/
2 /* */
3 /* Copyright 2009-2010 by Ullrich Koethe */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* ullrich.koethe@iwr.uni-heidelberg.de or */
10 /* vigra@informatik.uni-hamburg.de */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35
36 #ifndef VIGRA_CELLIMAGE_HXX
37 #define VIGRA_CELLIMAGE_HXX
38
39 #include <vigra/basicimage.hxx>
40 #include <vigra/pixelneighborhood.hxx>
41 #include <functional>
42
43 namespace vigra {
44
45 namespace cellimage {
46
47 enum CellType { CellTypeRegion = 0,
48 CellTypeLine = 1,
49 CellTypeVertex = 2,
50 CellTypeError = 3,
51 CellTypeVertexOrLine = 4,
52 CellTypeErrorOrLine = 5 };
53
54 // -------------------------------------------------------------------
55 // CellPixel, CellImage
56 // -------------------------------------------------------------------
57 typedef unsigned int CellLabel;
58
59 struct CellPixel
60 {
61 private:
62 CellLabel typeLabel_;
63
64 friend struct CellPixelSerializer;
65
66 public:
67 CellPixel() {}
68 CellPixel(CellType type, CellLabel label = 0)
69 : typeLabel_((label << 2) | type)
70 {}
71
72 inline CellType type() const
73 { return (CellType)(typeLabel_ & 3); }
74 inline void setType(CellType type)
75 { typeLabel_ = (label() << 2) | type; }
76 inline void setType(CellType type, CellLabel label)
77 { typeLabel_ = label << 2 | type; }
78
79 inline CellLabel label() const
80 { return typeLabel_ >> 2; }
81 inline void setLabel(CellLabel label)
82 { typeLabel_ = label << 2 | type(); }
83 inline void setLabel(CellLabel label, CellType type)
84 { typeLabel_ = label << 2 | type; }
85
86 bool operator==(CellPixel const & rhs) const
87 { return typeLabel_ == rhs.typeLabel_; }
88
89 bool operator!=(CellPixel const & rhs) const
90 { return typeLabel_ != rhs.typeLabel_; }
91 };
92
93 typedef BasicImage<CellPixel> CellImage;
94
95 typedef vigra::NeighborhoodCirculator<CellImage::Iterator, EightNeighborCode>
96 CellImageEightCirculator;
97
98 // -------------------------------------------------------------------
99 // CellPixel Serialization
100 // -------------------------------------------------------------------
101 struct CellPixelSerializer
102 {
103 int operator()(CellPixel const &p) const
104 {
105 return p.typeLabel_;
106 }
107
108 CellPixel operator()(int i) const
109 {
110 CellPixel result;
111 result.typeLabel_ = i;
112 return result;
113 }
114 };
115
116 // -------------------------------------------------------------------
117 // CellPixel/CellImage Accessors
118 // -------------------------------------------------------------------
119 template<class VALUE_TYPE = CellType>
120 struct TypeAccessor
121 {
122 typedef VALUE_TYPE value_type;
123 typedef VALUE_TYPE result_type;
124
125 template<class Iterator>
126 value_type operator()(const Iterator &it) const
127 {
128 return it->type();
129 }
130
131 template<class Iterator>
132 void set(value_type type, const Iterator &it) const
133 {
134 it->setType(type);
135 }
136 };
137
138 typedef TypeAccessor<unsigned char> TypeAsByteAccessor;
139
140 typedef TypeAccessor<> CellTypeAccessor;
141
142 struct LabelAccessor
143 {
144 typedef CellLabel value_type;
145
146 template<class Iterator>
147 CellLabel operator()(const Iterator &it) const
148 {
149 return it->label();
150 }
151
152 template<class Iterator>
153 void set(CellLabel label, const Iterator &it) const
154 {
155 it->setLabel(label);
156 }
157 };
158
159 template<CellType type>
160 struct LabelWriter
161 {
162 typedef CellLabel value_type;
163
164 template<class Iterator>
165 void set(CellLabel label, const Iterator &it) const
166 {
167 it->setLabel(label, type);
168 }
169 };
170
171 template<CellType type>
172 struct CellTypeEquals : public std::unary_function<CellType, bool>
173 {
174 bool operator()(CellType t) const
175 {
176 return t == type;
177 }
178
179 template<class Iterator>
180 bool operator()(const Iterator &it) const
181 {
182 return it->type() == type;
183 }
184 };
185
186 struct CellMask : public std::unary_function<vigra::cellimage::CellPixel, bool>
187 {
188 vigra::cellimage::CellPixel maskPixel_;
189
190 CellMask(vigra::cellimage::CellPixel maskPixel)
191 : maskPixel_(maskPixel)
192 {}
193
194 template<class Iterator>
195 bool operator()(const Iterator &it) const
196 {
197 return *it == maskPixel_;
198 }
199 };
200
201 // -------------------------------------------------------------------
202 // RelabelFunctor (unused!)
203 // -------------------------------------------------------------------
204 template<class VALUETYPE>
205 struct RelabelFunctor
206 {
207 typedef VALUETYPE value_type;
208 typedef VALUETYPE argument_type;
209 typedef VALUETYPE result_type;
210
211 RelabelFunctor(VALUETYPE oldValue, VALUETYPE newValue)
212 : oldValue_(oldValue),
213 newValue_(newValue)
214 {}
215
216 VALUETYPE operator()(VALUETYPE value) const
217 {
218 return (value == oldValue_) ? newValue_ : value;
219 }
220
221 VALUETYPE oldValue_, newValue_;
222 };
223
224 // -------------------------------------------------------------------
225 // inspectCell
226 // -------------------------------------------------------------------
227 // thinking about "typename IteratorTraits<EndIterator>::DefaultAccessor":
228 // is not needed since we're not implementing srcCellRange here, but
229 // algorithms.
230 // srcCellRange can not be implemented that easy, because most VIGRA
231 // functions expect an ImageIterator, not a std::iterator
232 template <class EndIterator, class Accessor, class Functor>
233 void inspectCell(EndIterator endIterator, Accessor a, Functor & f)
234 {
235 for(; endIterator.inRange(); ++endIterator)
236 f(a(endIterator));
237 }
238
239 template <class EndIterator, class Functor>
240 void inspectCell(EndIterator endIterator, Functor & f)
241 {
242 for(; endIterator.inRange(); ++endIterator)
243 f(*endIterator);
244 }
245
246 // -------------------------------------------------------------------
247 // transformCell
248 // -------------------------------------------------------------------
249 template <class SrcEndIterator, class SrcAccessor,
250 class DestEndIterator, class DestAccessor, class Functor>
251 void transformCell(SrcEndIterator srcEndIterator, SrcAccessor sa,
252 DestEndIterator destEndIterator, DestAccessor da,
253 Functor const & f)
254 {
255 for(; srcEndIterator.inRange(); ++srcEndIterator, ++destEndIterator)
256 da.set(f(sa(srcEndIterator)), destEndIterator);
257 }
258
259 template <class SrcEndIterator, class DestEndIterator, class Functor>
260 void transformCell(SrcEndIterator srcEndIterator,
261 DestEndIterator destEndIterator,
262 Functor const & f)
263 {
264 for(; srcEndIterator.inRange(); ++srcEndIterator, ++destEndIterator)
265 *destEndIterator = f(*srcEndIterator);
266 }
267
268 } // namespace cellimage
269
270 } // namespace vigra
271
272 #endif // VIGRA_CELLIMAGE_HXX

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.10.0 (Mon Nov 18 2013)