13#ifndef ML_CLUSTER_ALGORITHM_H
14#define ML_CLUSTER_ALGORITHM_H
61template <
typename CLUSTERVOXELTYPE,
typename DerivedAlgorithm>
class ClusterAlgorithm;
64template <
typename CLUSTERVOXELTYPE,
typename DerivedAlgorithm>
72 _algorithm = algorithm;
102 _inImageExtent = imageExt;
107 if ((inputIndex == 0) || (inputIndex == 1 && _useMask))
109 return outputSubImageBox;
124 _algorithm->storeNextSlice(images[0]);
125 _error = _algorithm->processSlice(images[0].getOrigin().z, _inImageExtent, maskImage);
204 template <
typename CLUSTERVOXELTYPE,
typename DerivedAlgorithm>
220template <
typename CLUSTERVOXELTYPE,
typename DerivedAlgorithm>
249 CLUSTERVOXELTYPE currentValue,
253 CLUSTERVOXELTYPE neighborVoxel = DerivedAlgorithm::getVoxel(neighborVoxelPtr);
254 const bool isInToleranceFlag =
static_cast<DerivedAlgorithm*
>(
this)->isInTolerance(neighborVoxel, currentValue);
255 if (isInToleranceFlag)
259 return currentClusterRef;
264 return static_cast<CLUSTERVOXELTYPE
>(backgroundValue);
288 const bool useMask =
_parameters.maskImage !=
nullptr;
295 std::vector<PagedImage*> images;
316 errorCode =
loadMaskSlice(inMaskSlice, zi, inputImageExtent);
321 errorCode =
processSlice(zi, inputImageExtent, inMaskSlice);
333 _clusters->calcClusterSizes(userDataParameters);
349 const MLint xres = inputImageExtent.
x;
350 const MLint yres = inputImageExtent.
y;
357 size_t dataTypeSize =
MLSizeOf(dataType);
361 size_t xoffset = dataTypeSize;
362 size_t yoffset = dataTypeSize * xres;
364 const bool useMask =
_parameters.maskImage !=
nullptr;
365 const bool useBackgroundValue =
_parameters.useBackgroundValue;
368 for (
MLint yi = 0; yi < yres; yi++)
370 for (
MLint xi = 0; xi < xres; xi++)
373 MLTypeData* previousSlicePtr = previousSliceInternalPtr;
377 sliceInternalPtr += dataTypeSize;
378 previousSliceInternalPtr += dataTypeSize;
381 CLUSTERVOXELTYPE currentValue = DerivedAlgorithm::getVoxel(slicePtr);
386 MLint8 maskValue = *maskPtr++;
394 if (useBackgroundValue && (currentValue == backgroundValue))
400 MLuint currentClusterRef = 0;
415 if ((xi > 0) && (yi > 0))
417 currentClusterRef =
updateClusterRefForNeighbor(slicePtr - xoffset - yoffset, xi - 1, yi - 1, zi, currentValue, currentClusterRef);
419 if ((xi<xres - 1) && (yi>0))
421 currentClusterRef =
updateClusterRefForNeighbor(slicePtr + xoffset - yoffset, xi + 1, yi - 1, zi, currentValue, currentClusterRef);
428 if ((xi > 0) && (yi > 0) && (zi > 0))
430 currentClusterRef =
updateClusterRefForNeighbor(previousSlicePtr - xoffset - yoffset, xi - 1, yi - 1, zi - 1, currentValue, currentClusterRef);
432 if ((xi<xres - 1) && (yi>0) && (zi > 0))
434 currentClusterRef =
updateClusterRefForNeighbor(previousSlicePtr + xoffset - yoffset, xi + 1, yi - 1, zi - 1, currentValue, currentClusterRef);
436 if ((xi > 0) && (yi<yres - 1) && (zi>0))
438 currentClusterRef =
updateClusterRefForNeighbor(previousSlicePtr - xoffset + yoffset, xi - 1, yi + 1, zi - 1, currentValue, currentClusterRef);
440 if ((xi<xres - 1) && (yi<yres - 1) && (zi>0))
442 currentClusterRef =
updateClusterRefForNeighbor(previousSlicePtr + xoffset + yoffset, xi + 1, yi + 1, zi - 1, currentValue, currentClusterRef);
446 if ((xi>0) && (yi > 0))
448 currentClusterRef =
updateClusterRefForNeighbor(slicePtr - xoffset - yoffset, xi - 1, yi - 1, zi, currentValue, currentClusterRef);
450 if ((xi<xres - 1) && (yi>0))
452 currentClusterRef =
updateClusterRefForNeighbor(slicePtr + xoffset - yoffset, xi + 1, yi - 1, zi, currentValue, currentClusterRef);
454 if ((zi > 0) && (xi > 0))
458 if ((zi > 0) && (xi<xres - 1))
462 if ((zi>0) && (yi > 0))
466 if ((zi > 0) && (yi<yres - 1))
489 if (currentClusterRef == 0)
495 imageValue = DerivedAlgorithm::getVoxelAsDouble(slicePtr);
498 currentClusterRef =
_clusters->getNewReference(imageValue);
500 _clusters->setClusterRef(xi, yi, zi, currentClusterRef);
@ CLUSTER_MODE_IdenticalIntensities
Clusters * _clusters
Result clusters.
ClusterAlgorithmBase(const ComputeClusterParameters ¶meters, Clusters *clusters)
Constructor to be used.
MLErrorCode loadMaskSlice(TSubImage< MLint8 > &slice, MLint sliceNr, const ImageVector &inImgExt) const
Gets current slice of the mask image.
friend class ClusterHandler
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.
void setInSliceVoxelType(MLDataType dt)
Sets voxel type of input slices.
SubImage _currentSlices[_NR_OF_SUBIMAGES]
do not load the whole image into memory, only keep two slices at once
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)
MLErrorCode runClustering()
MLErrorCode processSlice(MLint zi, const ImageVector &inputImageExtent, TSubImage< MLint8 > &inMaskSlice)
MLErrorCode calcClustersForSlices()
ClusterAlgorithm(const ComputeClusterParameters ¶meters, Clusters *clusters)
Constructor to be used.
MLErrorCode processTiles(SubImage *images) override
void calculateOutputImageProperties(PagedImage *image) override
SubImageBox calculateInputSubImageBox(int inputIndex, const SubImageBox &outputSubImageBox) override
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.
static Host & getDefaultHost()
Returns a reference to the Host singleton.
ImageVector getImageExtent() const
Returns the extent of the (sub)image.
void calculateOutputImageProperties(PagedImage *image) override
OrderedProcessAllPagesHandler()
MLEXPORT void setInputSubImageDataType(int inputIndex, MLDataType dataType)
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)
const DATATYPE * getData() const
Returns the memory address of the image region. Overloads methods from SubImage.
ComponentType c
Color component of the vector.
ComponentType t
Time component of the vector.
ComponentType u
Unit/Modality/User component of the vector.
ComponentType z
Z component of the vector.
ComponentType x
X component of the vector.
ComponentType y
Y component of the vector.
#define ML_DISALLOW_COPY_AND_ASSIGN(className)
Defines basic macros.
MLEXPORT size_t MLSizeOf(MLDataType dataType)
@ MLint8Type
Enumerator for the signed 8-bit ML integer type.
#define ML_PROGRAMMING_ERROR
MLint32 MLErrorCode
Type of an ML Error code.
#define ML_RESULT_OK
No error. Everything seems to be okay.
void MLRequestProgressCB(void *usrData, double progress)
unsigned char MLTypeData
This is the pointer type used to point to the data of MLType data instances.
TSubImageBox< MLint > SubImageBox
Defines the standard SubImageBox type used in the ML. Its size varies with the size of the MLint type...
TImageVector< MLint > ImageVector
Defines the standard ImageVector type that is used by the ML for indexing and coordinates.
Cluster user data parameters.
MLdouble voxelSizeInMilliliters
bool useImageValueAsUserData
Structure to hold parameters for cluster computation.
CLUSTER_MODE_TYPE clusterMode
ComputeClusterParameters()
MLdouble similarityToleranceIntensities
void * progressCBUserData
bool useImageValueAsUserData
MLRequestProgressCB * progressCB
MLdouble similarityToleranceVectors
PagedImage * contentImage
NBH_TYPE neighborhoodRelation