30 #ifndef _D_BASE_MEASURE_OPERATIONS_HPP
31 #define _D_BASE_MEASURE_OPERATIONS_HPP
33 #include "Core/include/private/DImage.hpp"
34 #include "Base/include/private/DBlob.hpp"
59 typedef typename Image<T>::lineType lineType;
60 typedef _retType retType;
63 virtual void initialize(
const Image<T> & )
68 virtual void processSequence(lineType ,
size_t )
72 virtual void finalize(
const Image<T> & )
76 virtual RES_T processImage(
const Image<T> &imIn,
bool onlyNonZero =
false)
79 ASSERT(CHECK_ALLOCATED(&imIn), RES_ERR_BAD_ALLOCATION);
85 processSequence(pixels, pixCount);
90 for (
size_t i = 0; i < pixCount; i++) {
91 if (pixels[i] != T(0)) {
92 if (curSize == 0 && curStart == 0)
95 }
else if (curSize > 0) {
96 processSequence(pixels + curStart, curSize);
102 processSequence(pixels + curStart, curSize);
111 virtual RES_T processImage(
const Image<T> &imIn,
115 ASSERT(CHECK_ALLOCATED(&imIn), RES_ERR_BAD_ALLOCATION);
120 processSequence(pixels, pixCount);
126 virtual retType operator()(
const Image<T> &imIn,
bool onlyNonZero =
false)
128 processImage(imIn, onlyNonZero);
132 virtual retType processImage(
const Image<T> &imIn,
const Blob &blob)
136 ASSERT(CHECK_ALLOCATED(&imIn), RES_ERR_BAD_ALLOCATION, retVal);
139 Blob::sequences_const_iterator it = blob.sequences.begin();
140 Blob::sequences_const_iterator it_end = blob.sequences.end();
141 for (; it != it_end; it++)
142 processSequence(pixels + (*it).offset, (*it).size);
147 virtual retType operator()(
const Image<T> &imIn,
const Blob &blob)
149 processImage(imIn, blob);
156 virtual retType operator()(
const Image<T> &imIn,
const Blob &blob,
157 SMIL_UNUSED
bool flag)
159 processImage(imIn, blob);
175 template <
class T,
class _retType>
177 typedef typename Image<T>::lineType lineType;
178 typedef _retType retType;
179 virtual void processSequence(lineType ,
size_t ,
180 size_t ,
size_t ,
size_t )
183 virtual RES_T processImage(
const Image<T> &imIn,
bool onlyNonZero =
false)
185 this->initialize(imIn);
186 ASSERT(CHECK_ALLOCATED(&imIn), RES_ERR_BAD_ALLOCATION);
188 typename Image<T>::volType slices = imIn.
getSlices();
189 typename Image<T>::sliceType lines;
190 typename Image<T>::lineType pixels;
195 for (
size_t z = 0; z < dims[2]; z++) {
197 for (
size_t y = 0; y < dims[1]; y++)
198 processSequence(lines[y], dims[0], 0, y, z);
201 for (
size_t z = 0; z < dims[2]; z++) {
203 for (
size_t y = 0; y < dims[1]; y++) {
208 for (
size_t x = 0; x < dims[0]; x++) {
209 if (pixels[x] != 0) {
212 }
else if (curSize > 0) {
213 processSequence(pixels + curStart, curSize, curStart, y, z);
218 processSequence(pixels + curStart, curSize, curStart, y, z);
222 this->finalize(imIn);
225 virtual retType processImage(
const Image<T> &imIn,
const Blob &blob)
227 this->initialize(imIn);
229 ASSERT(CHECK_ALLOCATED(&imIn), RES_ERR_BAD_ALLOCATION, this->retVal);
232 Blob::sequences_const_iterator it = blob.sequences.begin();
233 Blob::sequences_const_iterator it_end = blob.sequences.end();
235 for (; it != it_end; it++) {
237 this->processSequence(pixels + (*it).offset, (*it).size, x, y, z);
239 this->finalize(imIn);
244 virtual void processSequence(lineType ,
size_t )
260 template <
class T,
class labelT,
class funcT>
261 map<labelT, typename funcT::retType>
262 processBlobMeasure(
const Image<T> &imIn,
const map<labelT, Blob> &blobs)
264 typedef typename funcT::retType retType;
265 map<labelT, typename funcT::retType> res;
267 ASSERT(CHECK_ALLOCATED(&imIn), RES_ERR_BAD_ALLOCATION, res);
269 size_t blobNbr = blobs.size();
270 std::vector<labelT> _keys;
271 std::vector<retType> _results(blobNbr);
273 for (
typename map<labelT, Blob>::const_iterator it = blobs.begin();
274 it != blobs.end(); it++)
275 _keys.push_back(it->first);
277 labelT *keys = _keys.data();
278 retType *results = _results.data();
283 int nthreads = Core::getInstance()->getNumberOfThreads();
284 #pragma omp parallel private(i) num_threads(nthreads)
290 for (i = 0; i < blobNbr; i++) {
292 const Blob &blob = blobs.at(keys[i]);
293 results[i] = func(imIn, blob);
296 for (i = 0; i < blobNbr; i++)
297 res[keys[i]] = results[i];
304 template <
class T,
class labelT,
class funcT>
305 map<labelT, typename funcT::retType>
306 processBlobMeasure(
const Image<T> &imIn,
bool onlyNonZero =
true)
308 map<labelT, Blob> blobs =
computeBlobs(imIn, onlyNonZero);
309 return processBlobMeasure<T, labelT, funcT>(imIn, blobs);
void getCoordsFromOffset(size_t off, size_t &x, size_t &y, size_t &z) const
Get x,y(,z) coordinates for a given offset.
Definition: DBaseImage.h:291
void getSize(size_t *w, size_t *h, size_t *d) const
Get image size.
Definition: DBaseImage.h:118
size_t getPixelCount() const
Get the number of pixels.
Definition: DBaseImage.h:160
Main Image class.
Definition: DImage.hpp:57
volType getSlices() const
Get an array containing the start offset of each slice.
Definition: DImage.hpp:117
lineType getPixels() const
Get the pixels as a 1D array.
Definition: DImage.hpp:105
map< T, Blob > computeBlobs(const Image< T > &imIn, bool onlyNonZero=true)
Create a map of blobs from a labeled image.
Definition: DBlob.hpp:98
List of offset and size of line contiguous pixels.
Definition: DBlob.hpp:82
Definition: DBaseMeasureOperations.hpp:55
Definition: DBaseMeasureOperations.hpp:176