Medical Imaging Interaction Toolkit  2023.12.00
Medical Imaging Interaction Toolkit
mitk::GrabCutOpenCVImageFilter Class Reference

Makes the OpenCV GrabCut filter available as OpenCVImageFilter. More...

#include <mitkGrabCutOpenCVImageFilter.h>

Inheritance diagram for mitk::GrabCutOpenCVImageFilter:
Collaboration diagram for mitk::GrabCutOpenCVImageFilter:

Public Types

typedef std::vector< itk::Index< 2 > > ModelPointsList
 List holding image indices of the model points. More...
 

Public Member Functions

 mitkClassMacro (GrabCutOpenCVImageFilter, AbstractOpenCVImageFilter)
 
Pointer Clone () const
 
 GrabCutOpenCVImageFilter ()
 
 ~GrabCutOpenCVImageFilter () override
 
bool OnFilterImage (cv::Mat &image) override
 Implementation of the virtual image filtering method. The input image is copied to a member attribute, but the actual filtering is not done in this method. Instead a worker thread is waken up every time this method is called, if foreground model points were set before. More...
 
void SetModelPoints (ModelPointsList foregroundPoints)
 Sets a list of image indices as foreground model points. More...
 
void SetModelPoints (ModelPointsList foregroundPoints, ModelPointsList backgroundPoints)
 Sets a list of image indices as foreground and another list as background model points. More...
 
void SetModelPoints (cv::Mat foregroundMask)
 Sets a mask where every non-zero pixel is treated as a foreground model pixel. More...
 
void SetModelPoints (cv::Mat foregroundMask, cv::Mat backgroundMask)
 Sets masks specifying foreground and background points. More...
 
void SetModelPointsDilationSize (int modelPointsDilationSize)
 Set a size of which each model point is dilated before image filtering. The more color information of the foreground object the GrabCut filter gets the better the result will be. Therefore the foreground pixels can be dilated before filtering. The caller is responsible for setting a dilation size so that no foreground model pixels will be indeed part of the background after dilation. More...
 
void SetUseOnlyRegionAroundModelPoints (unsigned int additionalBorder)
 Use only the region around the foreground model points for the segmentation. More...
 
void SetUseFullImage ()
 The full image is used as input for the segmentation. This method sets the behaviour back to the default behaviour in case mitk::GrabCutOpenCVImageFilter::SetUseOnlyRegionAroundModelPoints() was called before. More...
 
cv::Rect GetRegionAroundModelPoints ()
 Getter for the rectangle used for the area of segmentation. See mitk::GrabCutOpenCVImageFilter::SetUseOnlyRegionAroundModelPoints(). This method is mainly for debugging purposes and may be removed in the future. More...
 
int GetResultImageId ()
 Getter for an ascending id of the current result image. The id will be increased for every segmentation that is produced by the worker thread. It can be used to determine if a new segmentation was produced since the last time a segmentation was got from this filter. More...
 
cv::Mat GetResultMask ()
 Getter for the result mask of the current segmentation. The result of this method is not necessarily consistent with the result of mitk::GrabCutOpenCVImageFilter::GetResultContours() if they are called afterwards. The segmentation may have changed in the meantime. One should decide if he needs a mask or a contour or convert one into the other on his own. More...
 
std::vector< ModelPointsListGetResultContours ()
 Getter for the contours of the current segmentation. More...
 
ModelPointsList GetResultContourWithPixel (itk::Index< 2 > pixelIndex)
 Getter for one specific contour of the current segmentation. More...
 
- Public Member Functions inherited from mitk::AbstractOpenCVImageFilter
 mitkClassMacroItkParent (AbstractOpenCVImageFilter, itk::Object)
 
virtual bool FilterImage (cv::Mat &image, int id=INVALID_IMAGE_ID)
 

Static Public Member Functions

static Pointer New ()
 

Protected Member Functions

cv::Mat GetMaskFromPointSets ()
 Creates an image mask for GrabCut algorithm by using the foreground and background point sets. Background and foreground points will be dilated by the size set by mitk::GrabCutOpenCVImageFilter::SetModelPointsDilationSize(). More...
 
cv::Rect GetBoundingRectFromMask (cv::Mat mask)
 Creates a bounding box around all pixels which aren't propably background. The bounding box is widened as specified by mitk::GrabCutOpenCVImageFilter::SetUseOnlyRegionAroundModelPoints(). More...
 
cv::Mat RunSegmentation (cv::Mat input, cv::Mat mask)
 Performs a GrabCut segmentation of the given input image. More...
 
ModelPointsList ConvertMaskToModelPointsList (cv::Mat mask)
 Creates a list of points from every non-zero pixel of the given mask. More...
 
- Protected Member Functions inherited from mitk::AbstractOpenCVImageFilter
 AbstractOpenCVImageFilter ()
 
int GetCurrentImageId ()
 

Protected Attributes

int m_ModelPointsDilationSize
 
bool m_UseOnlyRegionAroundModelPoints
 
unsigned int m_AdditionalWidth
 
cv::Rect m_BoundingBox
 
ModelPointsList m_ForegroundPoints
 
ModelPointsList m_BackgroundPoints
 
cv::Mat m_InputImage
 
cv::Mat m_GrabCutMask
 
cv::Mat m_ResultMask
 
unsigned int m_CurrentProcessImageNum
 
int m_InputImageId
 id of the image currently set as m_InputImage More...
 
int m_ResultImageId
 id of the image which segmentation result is currently present in m_ResultMask More...
 
- Protected Attributes inherited from mitk::AbstractOpenCVImageFilter
int m_CurrentImageId
 

Additional Inherited Members

- Static Public Attributes inherited from mitk::AbstractOpenCVImageFilter
static int INVALID_IMAGE_ID
 

Detailed Description

Makes the OpenCV GrabCut filter available as OpenCVImageFilter.

Image filtering is done asynchronly by using a worker thread as GrabCut segmentation can take up to some seconds. Calling the mitk::GrabCutOpenCVImageFilter::OnFilterImage() method sets just the input image and wakes up the worker thread. It is not guaranteed that every image gets segmented. If multiple new images where set before a segmentation was finished, only the last new image gets segmented afterwards.

At least foreground model points have to be set by mitk::GrabCutOpenCVImageFilter::SetModelPoints() before a segmentation can be performed. The worder thread will not be waken up before any model points were set.

When a new segmentation is ready, mitk::GrabCutOpenCVImageFilter::GetCurrentImageId() returns a new image id. The segmentation can be got then by calling mitk::GrabCutOpenCVImageFilter::GetResultMask(), mitk::GrabCutOpenCVImageFilter::GetResultContours() or mitk::GrabCutOpenCVImageFilter::GetResultContourWithPixel().

Definition at line 61 of file mitkGrabCutOpenCVImageFilter.h.

Member Typedef Documentation

◆ ModelPointsList

List holding image indices of the model points.

Definition at line 65 of file mitkGrabCutOpenCVImageFilter.h.

Constructor & Destructor Documentation

◆ GrabCutOpenCVImageFilter()

mitk::GrabCutOpenCVImageFilter::GrabCutOpenCVImageFilter ( )

◆ ~GrabCutOpenCVImageFilter()

mitk::GrabCutOpenCVImageFilter::~GrabCutOpenCVImageFilter ( )
override

Member Function Documentation

◆ Clone()

Pointer mitk::GrabCutOpenCVImageFilter::Clone ( ) const

◆ ConvertMaskToModelPointsList()

ModelPointsList mitk::GrabCutOpenCVImageFilter::ConvertMaskToModelPointsList ( cv::Mat  mask)
protected

Creates a list of points from every non-zero pixel of the given mask.

◆ GetBoundingRectFromMask()

cv::Rect mitk::GrabCutOpenCVImageFilter::GetBoundingRectFromMask ( cv::Mat  mask)
protected

Creates a bounding box around all pixels which aren't propably background. The bounding box is widened as specified by mitk::GrabCutOpenCVImageFilter::SetUseOnlyRegionAroundModelPoints().

◆ GetMaskFromPointSets()

cv::Mat mitk::GrabCutOpenCVImageFilter::GetMaskFromPointSets ( )
protected

Creates an image mask for GrabCut algorithm by using the foreground and background point sets. Background and foreground points will be dilated by the size set by mitk::GrabCutOpenCVImageFilter::SetModelPointsDilationSize().

◆ GetRegionAroundModelPoints()

cv::Rect mitk::GrabCutOpenCVImageFilter::GetRegionAroundModelPoints ( )

Getter for the rectangle used for the area of segmentation. See mitk::GrabCutOpenCVImageFilter::SetUseOnlyRegionAroundModelPoints(). This method is mainly for debugging purposes and may be removed in the future.

◆ GetResultContours()

std::vector<ModelPointsList> mitk::GrabCutOpenCVImageFilter::GetResultContours ( )

Getter for the contours of the current segmentation.

A segmentation can consist of multiple regions therefore a list of contours is returned. If one needs only one specific region he can call mitk::GrabCutOpenCVImageFilter::GetResultContourWithPixel().

This result of this method is not necessarily consistent with the result of mitk::GrabCutOpenCVImageFilter::GetResultContours() if they are called afterwards. The segmentation may have changed in the meantime. One should decide if he needs a mask or a contour or convert one into the other on his own.

Returns
List containing lists of pixel indices for every contour.

◆ GetResultContourWithPixel()

ModelPointsList mitk::GrabCutOpenCVImageFilter::GetResultContourWithPixel ( itk::Index< 2 >  pixelIndex)

Getter for one specific contour of the current segmentation.

Can be used if only one (of possible multiple contours) is needed. A pixel index must be given to select from the contours. This could be one of the foreground model pixels for example. If other criteria are needed to distinguish the contours mitk::GrabCutOpenCVImageFilter::GetResultContours() can be used instead and therefore contour selection can be done by hand then.

This result of this method is not necessarily consistent with the result of mitk::GrabCutOpenCVImageFilter::GetResultContours() if they are called afterwards. The segmentation may have changed in the meantime. One should decide if he needs a mask or a contour or convert one into the other on his own.

Parameters
pixelIndexindex of a pixel which lies inside the contour
Returns
list of pixel indices for the selected contour

◆ GetResultImageId()

int mitk::GrabCutOpenCVImageFilter::GetResultImageId ( )

Getter for an ascending id of the current result image. The id will be increased for every segmentation that is produced by the worker thread. It can be used to determine if a new segmentation was produced since the last time a segmentation was got from this filter.

int lastResultImageId = grabCutFilter->GetResultImageId(); // do something if ( lastResultImageId != grabCutFilter->GetResultImageId() ) // get new segmentation

◆ GetResultMask()

cv::Mat mitk::GrabCutOpenCVImageFilter::GetResultMask ( )

Getter for the result mask of the current segmentation. The result of this method is not necessarily consistent with the result of mitk::GrabCutOpenCVImageFilter::GetResultContours() if they are called afterwards. The segmentation may have changed in the meantime. One should decide if he needs a mask or a contour or convert one into the other on his own.

Returns
image of the size of the input image where all pixels segmented as foreground are non-zero

◆ mitkClassMacro()

mitk::GrabCutOpenCVImageFilter::mitkClassMacro ( GrabCutOpenCVImageFilter  ,
AbstractOpenCVImageFilter   
)

◆ New()

static Pointer mitk::GrabCutOpenCVImageFilter::New ( )
static

◆ OnFilterImage()

bool mitk::GrabCutOpenCVImageFilter::OnFilterImage ( cv::Mat &  image)
overridevirtual

Implementation of the virtual image filtering method. The input image is copied to a member attribute, but the actual filtering is not done in this method. Instead a worker thread is waken up every time this method is called, if foreground model points were set before.

Parameters
imageOpenCV image to be segmentated
Returns
false if an empty image was set, true otherwise

Implements mitk::AbstractOpenCVImageFilter.

◆ RunSegmentation()

cv::Mat mitk::GrabCutOpenCVImageFilter::RunSegmentation ( cv::Mat  input,
cv::Mat  mask 
)
protected

Performs a GrabCut segmentation of the given input image.

Parameters
inputimage on which the segmentation will be performed
maskforeground and background pixels used as basis for segmentation
Returns
mask with every pixel of the segmented foreground object set non-zero

◆ SetModelPoints() [1/4]

void mitk::GrabCutOpenCVImageFilter::SetModelPoints ( cv::Mat  foregroundMask)

Sets a mask where every non-zero pixel is treated as a foreground model pixel.

◆ SetModelPoints() [2/4]

void mitk::GrabCutOpenCVImageFilter::SetModelPoints ( cv::Mat  foregroundMask,
cv::Mat  backgroundMask 
)

Sets masks specifying foreground and background points.

Parameters
foregroundMaskevery non-zero pixel is treated as a foreground model pixel
backgroundMaskevery non-zero pixel is treated as a background model pixel

◆ SetModelPoints() [3/4]

void mitk::GrabCutOpenCVImageFilter::SetModelPoints ( ModelPointsList  foregroundPoints)

Sets a list of image indices as foreground model points.

Parameters
foregroundPointsList of image indices which definitely belong to the foreground.

◆ SetModelPoints() [4/4]

void mitk::GrabCutOpenCVImageFilter::SetModelPoints ( ModelPointsList  foregroundPoints,
ModelPointsList  backgroundPoints 
)

Sets a list of image indices as foreground and another list as background model points.

Parameters
foregroundPointsList of image indices which definitely belong to the foreground.
backgroundPointsList of image indices which definitely belong to the background.

◆ SetModelPointsDilationSize()

void mitk::GrabCutOpenCVImageFilter::SetModelPointsDilationSize ( int  modelPointsDilationSize)

Set a size of which each model point is dilated before image filtering. The more color information of the foreground object the GrabCut filter gets the better the result will be. Therefore the foreground pixels can be dilated before filtering. The caller is responsible for setting a dilation size so that no foreground model pixels will be indeed part of the background after dilation.

Dilation is done to the background model pixles as well, if there are any set for the filter.

Parameters
modelPointsDilationSizehow many pixels are added in every direction, 0 sets back to no dilation

◆ SetUseFullImage()

void mitk::GrabCutOpenCVImageFilter::SetUseFullImage ( )

The full image is used as input for the segmentation. This method sets the behaviour back to the default behaviour in case mitk::GrabCutOpenCVImageFilter::SetUseOnlyRegionAroundModelPoints() was called before.

◆ SetUseOnlyRegionAroundModelPoints()

void mitk::GrabCutOpenCVImageFilter::SetUseOnlyRegionAroundModelPoints ( unsigned int  additionalBorder)

Use only the region around the foreground model points for the segmentation.

This is mainly for reasons of segmentation speed and has the drawback that the foreground model points (plus the given additional border) have to cover the whole foreground object.

The segmentation filter can be set back to using the whole image by calling mitk::GrabCutOpenCVImageFilter::SetUseFullImage().

Parameters
additionalBordersize of the border around the foreground points which will be used for segmentation, too

Member Data Documentation

◆ m_AdditionalWidth

unsigned int mitk::GrabCutOpenCVImageFilter::m_AdditionalWidth
protected

Definition at line 243 of file mitkGrabCutOpenCVImageFilter.h.

◆ m_BackgroundPoints

ModelPointsList mitk::GrabCutOpenCVImageFilter::m_BackgroundPoints
protected

Definition at line 247 of file mitkGrabCutOpenCVImageFilter.h.

◆ m_BoundingBox

cv::Rect mitk::GrabCutOpenCVImageFilter::m_BoundingBox
protected

Definition at line 244 of file mitkGrabCutOpenCVImageFilter.h.

◆ m_CurrentProcessImageNum

unsigned int mitk::GrabCutOpenCVImageFilter::m_CurrentProcessImageNum
protected

Definition at line 253 of file mitkGrabCutOpenCVImageFilter.h.

◆ m_ForegroundPoints

ModelPointsList mitk::GrabCutOpenCVImageFilter::m_ForegroundPoints
protected

Definition at line 246 of file mitkGrabCutOpenCVImageFilter.h.

◆ m_GrabCutMask

cv::Mat mitk::GrabCutOpenCVImageFilter::m_GrabCutMask
protected

Definition at line 250 of file mitkGrabCutOpenCVImageFilter.h.

◆ m_InputImage

cv::Mat mitk::GrabCutOpenCVImageFilter::m_InputImage
protected

Definition at line 249 of file mitkGrabCutOpenCVImageFilter.h.

◆ m_InputImageId

int mitk::GrabCutOpenCVImageFilter::m_InputImageId
protected

id of the image currently set as m_InputImage

Definition at line 256 of file mitkGrabCutOpenCVImageFilter.h.

◆ m_ModelPointsDilationSize

int mitk::GrabCutOpenCVImageFilter::m_ModelPointsDilationSize
protected

Definition at line 240 of file mitkGrabCutOpenCVImageFilter.h.

◆ m_ResultImageId

int mitk::GrabCutOpenCVImageFilter::m_ResultImageId
protected

id of the image which segmentation result is currently present in m_ResultMask

Definition at line 259 of file mitkGrabCutOpenCVImageFilter.h.

◆ m_ResultMask

cv::Mat mitk::GrabCutOpenCVImageFilter::m_ResultMask
protected

Definition at line 251 of file mitkGrabCutOpenCVImageFilter.h.

◆ m_UseOnlyRegionAroundModelPoints

bool mitk::GrabCutOpenCVImageFilter::m_UseOnlyRegionAroundModelPoints
protected

Definition at line 242 of file mitkGrabCutOpenCVImageFilter.h.


The documentation for this class was generated from the following file: