MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlClusterAlgorithm.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2007, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_CLUSTER_ALGORITHM_H
14#define ML_CLUSTER_ALGORITHM_H
15
17
18#include "mlClusters.h"
19#include "mlClusterInfo.h"
20#include "mlClusterRefVolume.h"
22
23#include <mlModuleIncludes.h>
25#include <mlHost.h>
26
28
31{
33 {
35 similarityToleranceIntensities = 0;
36 similarityToleranceVectors = 0.98;
37 neighborhoodRelation = NBH_3D_26_XYZ;
38 contentImage = nullptr;
39 maskImage = nullptr;
40 useBackgroundValue = true;
41 backgroundValue = 0;
42 useImageValueAsUserData = false;
43 progressCB = nullptr;
44 progressCBUserData = nullptr;
45 }
46
58};
59
60
61template <typename CLUSTERVOXELTYPE, typename DerivedAlgorithm> class ClusterAlgorithm;
62
64template <typename CLUSTERVOXELTYPE, typename DerivedAlgorithm> class ClusterHandler
66{
68
69public:
71 {
72 _algorithm = algorithm;
73 _error = ML_RESULT_OK;
74 _useMask = false;
75 _dataType = -1;
76 }
77
80 {
81 _useMask = useMask;
82 _dataType = type;
83 }
84
86 {
87 OrderedProcessAllPagesHandler::calculateOutputImageProperties(image);
88 image->setDataType(_dataType);
89 if (_useMask) {
91 }
94 imageExt.c = 1;
95 imageExt.t = 1;
96 imageExt.u = 1;
99 pageExt.z = 1;
100 image->setPageExtent(pageExt);
101 _inImageExtent = imageExt;
102 }
103
105 {
106 if ((inputIndex == 0) || (inputIndex == 1 && _useMask)) {
107 return outputSubImageBox;
108 }
109 else {
110 return SubImageBox();
111 }
112 }
113
115 {
116 TSubImage<MLint8> maskImage;
117 if (_useMask) {
118 maskImage = TSubImage<MLint8>(images[1]);
119 }
120 _algorithm->storeNextSlice(images[0]);
121 _error = _algorithm->processSlice(images[0].getOrigin().z, _inImageExtent, maskImage);
122 return _error;
123 }
124
126 {
127 return _error;
128 }
129
130private:
132
133 ImageVector _inImageExtent;
134 MLDataType _dataType;
135 bool _useMask;
136 MLErrorCode _error;
137};
138
139//-----------------------------------------------------------------------------------------------
141//-----------------------------------------------------------------------------------------------
143{
144public:
147
148
151
152
155 bool hasResults() const
156 {
157 return _hasResults;
158 }
159
160
161protected:
163 enum {
164 _NR_OF_SUBIMAGES = 2
165 } NumInImagesEnum;
166
169
172
175
178
181
184
187 {
188 _hasResults = result;
189 }
190
193
195 SubImage _currentSlices[_NR_OF_SUBIMAGES];
196
198
199private:
201 bool _hasResults;
202
205
206
207 template <typename CLUSTERVOXELTYPE, typename DerivedAlgorithm> friend class ClusterHandler;
208};
209
210
211//-----------------------------------------------------------------------------------------------
221//-----------------------------------------------------------------------------------------------
222template <typename CLUSTERVOXELTYPE, typename DerivedAlgorithm> class ClusterAlgorithm : public ClusterAlgorithmBase
223{
224public:
227
232
234 {
235 return static_cast<DerivedAlgorithm*>(this)->calcClustersForSlices();
236 }
237
238protected:
241
249 {
250 // Get predecessor voxel value.
251 CLUSTERVOXELTYPE neighborVoxel = DerivedAlgorithm::getVoxel(neighborVoxelPtr);
252 const bool isInToleranceFlag = static_cast<DerivedAlgorithm*>(this)->isInTolerance(neighborVoxel, currentValue);
253 if (isInToleranceFlag) {
254 currentClusterRef = mergeClusterReference(x, y, z, currentClusterRef);
255 }
256 return currentClusterRef;
257 }
258
259 inline static CLUSTERVOXELTYPE convertBackgroundValue(MLdouble backgroundValue)
260 {
261 return static_cast<CLUSTERVOXELTYPE>(backgroundValue);
262 }
263
267 {
268 // Allocate memory with subimages of the requested voxel type.
269 setInSliceVoxelType(_parameters.contentImage->getDataType());
270
271 ImageVector inputImageExtent = _parameters.contentImage->getImageExtent();
272
274
275 // Initialize data structures for clustering.
276 MLErrorCode errorCode = _clusters->init(inputImageExtent.x, inputImageExtent.y, inputImageExtent.z);
277
278 if (ML_RESULT_OK == errorCode) {
279 if ((_parameters.neighborhoodRelation == NBH_2D_4_XY) || (_parameters.neighborhoodRelation == NBH_2D_8_XY)) {
280 _clusters->setUse2DNeighborhood();
281 }
282
283 const bool useMask = _parameters.maskImage != nullptr;
284
285 if (!Host::getDefaultHost().getUseClassicHost()) {
287 handler.setParameters(useMask, _currentSlices[0].getDataType());
288
289 std::vector<PagedImage*> images;
290 images.push_back(_parameters.contentImage);
291 if (useMask) {
292 images.push_back(_parameters.maskImage);
293 }
294
295 errorCode = Host::getDefaultHost().processAllPagesWithInputImages(
296 images, handler, SubImageBox(), _parameters.progressCB, _parameters.progressCBUserData);
297 if (errorCode != ML_RESULT_OK) {
298 errorCode = handler.getErrorCode();
299 }
300 }
301 else {
302 // Main loop, proceed slice-wise only on success.
303 for (MLint zi = 0; (ML_RESULT_OK == errorCode) && (zi < inputImageExtent.z); zi++) {
304 errorCode = loadNextSlice(zi, inputImageExtent);
305 if (ML_RESULT_OK == errorCode && useMask) {
306 errorCode = loadMaskSlice(inMaskSlice, zi, inputImageExtent);
307 }
308
309 if (ML_RESULT_OK == errorCode) {
310 errorCode = processSlice(zi, inputImageExtent, inMaskSlice);
311 }
312 }
313 }
314
315 if (ML_RESULT_OK == errorCode) {
317 userDataParameters.useImageValueAsUserData = _parameters.useImageValueAsUserData;
318 userDataParameters.voxelSizeInMilliliters = _clusters->getVoxelVolumeInMilliliters();
319 userDataParameters.backgroundValue = _parameters.backgroundValue;
320
321 _clusters->calcClusterSizes(userDataParameters);
322 }
323 }
324
325 setResultFlag(ML_RESULT_OK == errorCode);
326 freeInSliceData();
327
328 return errorCode;
329 }
330
331public:
333 {
334 MLErrorCode errorCode = ML_RESULT_OK;
335
336 const MLint xres = inputImageExtent.x;
337 const MLint yres = inputImageExtent.y;
338
339 const ClusterVoxelType backgroundValue = DerivedAlgorithm::convertBackgroundValue(_parameters.backgroundValue);
340
341 MLTypeData* sliceInternalPtr = reinterpret_cast<MLTypeData*>(_currentSlices[1].getData());
342 MLTypeData* previousSliceInternalPtr = reinterpret_cast<MLTypeData*>(_currentSlices[0].getData());
343 const MLDataType dataType = _currentSlices[1].getDataType();
344 size_t dataTypeSize = MLSizeOf(dataType);
345
346 MLint8* maskPtr = inMaskSlice.getData();
347
348 size_t xoffset = dataTypeSize;
349 size_t yoffset = dataTypeSize * xres;
350
351 const bool useMask = _parameters.maskImage != nullptr;
352 const bool useBackgroundValue = _parameters.useBackgroundValue;
353
354 // iterate over slice voxels
355 for (MLint yi = 0; yi < yres; yi++) {
356 for (MLint xi = 0; xi < xres; xi++) {
359 // advance the internal pointers, we can't advance slicePtr/previousSlicePtr,
360 // because they are accessed below and we don't want to place the advancing into each
361 // continue statement.
364
365 // get current voxel value from untyped image.
366 CLUSTERVOXELTYPE currentValue = DerivedAlgorithm::getVoxel(slicePtr);
367
368 // get corresponding mask value
369 if (useMask) {
371 if (maskValue == 0) {
372 continue;
373 }
374 }
375
376 // if background skip voxel
377 if (useBackgroundValue && (currentValue == backgroundValue)) {
378 continue;
379 }
380
381 // reset cluster reference
383
384 // left and top neighbor are always evaluated
385 if (xi > 0) {
387 updateClusterRefForNeighbor(slicePtr - xoffset, xi - 1, yi, zi, currentValue, currentClusterRef);
388 }
389 if (yi > 0) {
391 updateClusterRefForNeighbor(slicePtr - yoffset, xi, yi - 1, zi, currentValue, currentClusterRef);
392 }
393
394 switch (_parameters.neighborhoodRelation) {
395 case NBH_2D_8_XY:
396 if ((xi > 0) && (yi > 0)) {
397 currentClusterRef = updateClusterRefForNeighbor(slicePtr - xoffset - yoffset, xi - 1, yi - 1, zi,
398 currentValue, currentClusterRef);
399 }
400 if ((xi < xres - 1) && (yi > 0)) {
401 currentClusterRef = updateClusterRefForNeighbor(slicePtr + xoffset - yoffset, xi + 1, yi - 1, zi,
402 currentValue, currentClusterRef);
403 }
404 break;
405 // The missing breaks in the following 3d cases have been left out intentionally to be able to deal with
406 // the different 3d neighborhoods without doubling code lines (so complete 26-3d-neighborhood calculation is
407 // done by case NBH_3D_26_XYZ + NBH_3D_18_XYZ + NBH_3D_6_XYZ)
408 case NBH_3D_26_XYZ:
409 if ((xi > 0) && (yi > 0) && (zi > 0)) {
410 currentClusterRef = updateClusterRefForNeighbor(previousSlicePtr - xoffset - yoffset, xi - 1, yi - 1,
411 zi - 1, currentValue, currentClusterRef);
412 }
413 if ((xi < xres - 1) && (yi > 0) && (zi > 0)) {
414 currentClusterRef = updateClusterRefForNeighbor(previousSlicePtr + xoffset - yoffset, xi + 1, yi - 1,
415 zi - 1, currentValue, currentClusterRef);
416 }
417 if ((xi > 0) && (yi < yres - 1) && (zi > 0)) {
418 currentClusterRef = updateClusterRefForNeighbor(previousSlicePtr - xoffset + yoffset, xi - 1, yi + 1,
419 zi - 1, currentValue, currentClusterRef);
420 }
421 if ((xi < xres - 1) && (yi < yres - 1) && (zi > 0)) {
422 currentClusterRef = updateClusterRefForNeighbor(previousSlicePtr + xoffset + yoffset, xi + 1, yi + 1,
423 zi - 1, currentValue, currentClusterRef);
424 }
425 [[fallthrough]];
426 case NBH_3D_18_XYZ:
427 if ((xi > 0) && (yi > 0)) {
428 currentClusterRef = updateClusterRefForNeighbor(slicePtr - xoffset - yoffset, xi - 1, yi - 1, zi,
429 currentValue, currentClusterRef);
430 }
431 if ((xi < xres - 1) && (yi > 0)) {
432 currentClusterRef = updateClusterRefForNeighbor(slicePtr + xoffset - yoffset, xi + 1, yi - 1, zi,
433 currentValue, currentClusterRef);
434 }
435 if ((zi > 0) && (xi > 0)) {
436 currentClusterRef = updateClusterRefForNeighbor(previousSlicePtr - xoffset, xi - 1, yi, zi - 1,
437 currentValue, currentClusterRef);
438 }
439 if ((zi > 0) && (xi < xres - 1)) {
440 currentClusterRef = updateClusterRefForNeighbor(previousSlicePtr + xoffset, xi + 1, yi, zi - 1,
441 currentValue, currentClusterRef);
442 }
443 if ((zi > 0) && (yi > 0)) {
444 currentClusterRef = updateClusterRefForNeighbor(previousSlicePtr - yoffset, xi, yi - 1, zi - 1,
445 currentValue, currentClusterRef);
446 }
447 if ((zi > 0) && (yi < yres - 1)) {
448 currentClusterRef = updateClusterRefForNeighbor(previousSlicePtr + yoffset, xi, yi + 1, zi - 1,
449 currentValue, currentClusterRef);
450 }
451 [[fallthrough]];
452 case NBH_3D_6_XYZ:
453 if (zi > 0) {
455 updateClusterRefForNeighbor(previousSlicePtr, xi, yi, zi - 1, currentValue, currentClusterRef);
456 }
457 break;
458 case NBH_2D_4_XY:
459 // nothing to do, because 2 neighbors are already evaluated before the switch
460 break;
461 case NBH_COUNT:
462 errorCode = ML_PROGRAMMING_ERROR;
463 break;
464 default:
465 errorCode = ML_PROGRAMMING_ERROR;
466 break;
467 }
468
469 // if no matching neighbor found create new clusterRef
470 if (currentClusterRef == 0) {
471 MLdouble imageValue = 0;
472
473 if (_parameters.useImageValueAsUserData) {
474 imageValue = DerivedAlgorithm::getVoxelAsDouble(slicePtr);
475 }
476
477 currentClusterRef = _clusters->getNewReference(imageValue);
478 }
479 _clusters->setClusterRef(xi, yi, zi, currentClusterRef);
480 }
481 }
482 return errorCode;
483 }
484
485
486private:
488 ClusterAlgorithm& operator=(const ClusterAlgorithm&);
489};
490
492
493#endif
@ CLUSTER_MODE_IdenticalIntensities
Interface class for clustering algorithms.
Clusters * _clusters
Result clusters.
ClusterAlgorithmBase(const ComputeClusterParameters &parameters, Clusters *clusters)
Constructor to be used.
MLErrorCode loadMaskSlice(TSubImage< MLint8 > &slice, MLint sliceNr, const ImageVector &inImgExt) const
Gets current slice of the mask image.
virtual ~ClusterAlgorithmBase()
Default virtual destructor.
void freeInSliceData()
Releases memory of input image slices.
MLErrorCode loadNextSlice(MLint sliceNr, const ImageVector &inImgExt)
Gets next image data.
void setResultFlag(bool result)
Sets internal result flag to result.
MLuint mergeClusterReference(MLint x, MLint y, MLint z, MLuint currClusterRef)
Merges a cluster reference currClusterRef for position (x,y,z).
ComputeClusterParameters _parameters
void storeNextSlice(SubImage &slice)
Sets next image data, expects the data to be managed by memory manager.
bool hasResults() const
Returns whether getCluster() and getClusterOfVoxel() contains cluster data.
void setInSliceVoxelType(MLDataType dt)
Sets voxel type of input slices.
Type specific implementations of the interface class for clustering algorithms.
static CLUSTERVOXELTYPE convertBackgroundValue(MLdouble backgroundValue)
CLUSTERVOXELTYPE ClusterVoxelType
The used type of the voxels, needed by derived classes.
MLuint updateClusterRefForNeighbor(void *neighborVoxelPtr, MLint x, MLint y, MLint z, CLUSTERVOXELTYPE currentValue, MLuint currentClusterRef)
Checks if neighbor at position x,y,z belongs to the same cluster given by currentClusterRef.
MLErrorCode processSlice(MLint zi, const ImageVector &inputImageExtent, TSubImage< MLint8 > &inMaskSlice)
MLErrorCode calcClustersForSlices()
Template applying cluster calculations for one slice.
ClusterAlgorithm(const ComputeClusterParameters &parameters, Clusters *clusters)
Constructor to be used.
ClusterHandler that is used to provide slice-by-slice data to the cluster algorithm.
MLErrorCode processTiles(SubImage *images) override
Reimplement this method to perform the processing, return ML_RESULT_OK if processing should go on; ot...
void calculateOutputImageProperties(PagedImage *image) override
Calculates the (temporary and virtual) output image's properties, which by default has the properties...
SubImageBox calculateInputSubImageBox(int inputIndex, const SubImageBox &outputSubImageBox) override
Called by the host to determine which input image region (tile) of input inputIndex is required to ca...
void setParameters(bool useMask, MLDataType type)
Stores all parameters that are needed to call the algorithm.
MLErrorCode getErrorCode() const
ClusterHandler(ClusterAlgorithm< CLUSTERVOXELTYPE, DerivedAlgorithm > *algorithm)
Structure computes and holds all cluster information. For internal use.
Definition mlClusters.h:29
ImageVector getImageExtent() const
Returns the extent of the (sub)image.
A ProcessAllPages handler that offers ordered tile delivery on any number of input images.
The class PagedImage, representing a fragmented image that manages properties and data of an image lo...
MLEXPORT void setInputSubImageDataType(int inputIndex, MLDataType dataType)
Sets the dataType for the given inputIndex.
MLEXPORT void setPageExtent(const ImageVector &pageExtent)
Sets the extents of the pages to pageExtent.
MLEXPORT void setDataType(MLDataType dataType) override
Sets the type of data to dataType. Overridden to perform access check.
virtual MLEXPORT void setImageExtent(const ImageVector &extent)
Sets the extents of the image to extent. The list of valid pages and its content are cleared.
MLEXPORT void setInputSubImagesAreReadOnly(bool readOnly=true)
Sets the read-only flag for all input images.
This class manages/represents a rectangular 6D image region that is organized linearly in memory.
Definition mlSubImage.h:75
This template class manages/represents a rectangular 6D image region in memory that is organized line...
Definition mlTSubImage.h:99
#define ML_DISALLOW_COPY_AND_ASSIGN(className)
Defines basic macros.
Definition mlMacros.h:21
MLEXPORT size_t MLSizeOf(MLDataType dataType)
Returns the size of the data type dataType in bytes.
MLint32 MLDataType
MLDataType.
Definition mlTypeDefs.h:592
@ MLint8Type
Enumerator for the signed 8-bit ML integer type.
Definition mlTypeDefs.h:616
#define ML_PROGRAMMING_ERROR
A case occurred that should not appear and there are a variety of reasons, typically it is a programm...
Definition mlTypeDefs.h:844
MLint32 MLErrorCode
Type of an ML Error code.
Definition mlTypeDefs.h:772
#define ML_RESULT_OK
No error. Everything seems to be okay.
Definition mlTypeDefs.h:780
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
void MLRequestProgressCB(void *usrData, double progress)
MLuint64 MLuint
An unsigned ML integer type with at least 64 bits used for index calculations on very large images ev...
Definition mlTypeDefs.h:505
double MLdouble
Definition mlTypeDefs.h:217
unsigned char MLTypeData
This is the pointer type used to point to the data of MLType data instances.
char MLint8
Definition mlTypeDefs.h:97
MLint64 MLint
A signed ML integer type with at least 64 bits used for index calculations on very large images even ...
Definition mlTypeDefs.h:489
Cluster user data parameters.
Structure to hold parameters for cluster computation.
MLRequestProgressCB * progressCB