Medical Imaging Interaction Toolkit  2018.4.99-87d68d9f
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 57 of file mitkGrabCutOpenCVImageFilter.h.

Member Typedef Documentation

◆ ModelPointsList

List holding image indices of the model points.

Definition at line 61 of file mitkGrabCutOpenCVImageFilter.h.

Constructor & Destructor Documentation

◆ GrabCutOpenCVImageFilter()

mitk::GrabCutOpenCVImageFilter::GrabCutOpenCVImageFilter ( )

Definition at line 30 of file mitkGrabCutOpenCVImageFilter.cpp.

◆ ~GrabCutOpenCVImageFilter()

mitk::GrabCutOpenCVImageFilter::~GrabCutOpenCVImageFilter ( )
override

Definition at line 46 of file mitkGrabCutOpenCVImageFilter.cpp.

Member Function Documentation

◆ Clone()

Pointer mitk::GrabCutOpenCVImageFilter::Clone ( ) const

◆ ConvertMaskToModelPointsList()

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

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

Definition at line 348 of file mitkGrabCutOpenCVImageFilter.cpp.

References GetBoundingRectFromMask(), GetMaskFromPointSets(), image, m_BoundingBox, m_InputImage, m_InputImageId, m_ResultImageId, m_ResultMask, m_UseOnlyRegionAroundModelPoints, mask, and RunSegmentation().

Referenced by SetModelPoints().

◆ 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().

Definition at line 269 of file mitkGrabCutOpenCVImageFilter.cpp.

References compare(), m_AdditionalWidth, and MITK_WARN.

Referenced by ConvertMaskToModelPointsList().

◆ 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().

Definition at line 232 of file mitkGrabCutOpenCVImageFilter.cpp.

References m_BackgroundPoints, m_ForegroundPoints, m_InputImage, m_ModelPointsDilationSize, and mask.

Referenced by ConvertMaskToModelPointsList().

◆ 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.

Definition at line 137 of file mitkGrabCutOpenCVImageFilter.cpp.

References m_BoundingBox.

◆ GetResultContours()

std::vector< mitk::GrabCutOpenCVImageFilter::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.

Definition at line 158 of file mitkGrabCutOpenCVImageFilter.cpp.

References GetResultMask().

◆ GetResultContourWithPixel()

mitk::GrabCutOpenCVImageFilter::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

Definition at line 189 of file mitkGrabCutOpenCVImageFilter.cpp.

References compare(), GetResultMask(), mask, and MITK_WARN.

◆ 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

Definition at line 142 of file mitkGrabCutOpenCVImageFilter.cpp.

References m_ResultImageId.

◆ 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

Definition at line 147 of file mitkGrabCutOpenCVImageFilter.cpp.

References m_ResultMask.

Referenced by GetResultContours(), and GetResultContourWithPixel().

◆ 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
OpenCVimage to be segmentated
Returns
false if an empty image was set, true otherwise

Implements mitk::AbstractOpenCVImageFilter.

Definition at line 54 of file mitkGrabCutOpenCVImageFilter.cpp.

References mitk::AbstractOpenCVImageFilter::GetCurrentImageId(), m_ForegroundPoints, m_InputImage, m_InputImageId, and MITK_WARN.

◆ 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

Definition at line 319 of file mitkGrabCutOpenCVImageFilter.cpp.

References compare(), and GMM_COMPONENTS_COUNT.

Referenced by ConvertMaskToModelPointsList().

◆ SetModelPoints() [1/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.

Definition at line 84 of file mitkGrabCutOpenCVImageFilter.cpp.

References m_ForegroundPoints.

◆ SetModelPoints() [2/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.

Definition at line 91 of file mitkGrabCutOpenCVImageFilter.cpp.

References m_BackgroundPoints, and m_ForegroundPoints.

◆ SetModelPoints() [3/4]

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

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

Definition at line 99 of file mitkGrabCutOpenCVImageFilter.cpp.

References ConvertMaskToModelPointsList(), and m_ForegroundPoints.

◆ SetModelPoints() [4/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

Definition at line 106 of file mitkGrabCutOpenCVImageFilter.cpp.

References ConvertMaskToModelPointsList(), m_BackgroundPoints, and m_ForegroundPoints.

◆ 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

Definition at line 114 of file mitkGrabCutOpenCVImageFilter.cpp.

References m_ModelPointsDilationSize, MITK_ERROR, and mitkThrow.

◆ 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.

Definition at line 132 of file mitkGrabCutOpenCVImageFilter.cpp.

References m_UseOnlyRegionAroundModelPoints.

◆ 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

Definition at line 126 of file mitkGrabCutOpenCVImageFilter.cpp.

References m_AdditionalWidth, and m_UseOnlyRegionAroundModelPoints.

Member Data Documentation

◆ m_AdditionalWidth

unsigned int mitk::GrabCutOpenCVImageFilter::m_AdditionalWidth
protected

◆ m_BackgroundPoints

ModelPointsList mitk::GrabCutOpenCVImageFilter::m_BackgroundPoints
protected

Definition at line 243 of file mitkGrabCutOpenCVImageFilter.h.

Referenced by GetMaskFromPointSets(), and SetModelPoints().

◆ m_BoundingBox

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

◆ m_CurrentProcessImageNum

unsigned int mitk::GrabCutOpenCVImageFilter::m_CurrentProcessImageNum
protected

Definition at line 249 of file mitkGrabCutOpenCVImageFilter.h.

◆ m_ForegroundPoints

ModelPointsList mitk::GrabCutOpenCVImageFilter::m_ForegroundPoints
protected

◆ m_GrabCutMask

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

Definition at line 246 of file mitkGrabCutOpenCVImageFilter.h.

◆ m_InputImage

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

◆ m_InputImageId

int mitk::GrabCutOpenCVImageFilter::m_InputImageId
protected

id of the image currently set as m_InputImage

Definition at line 252 of file mitkGrabCutOpenCVImageFilter.h.

Referenced by ConvertMaskToModelPointsList(), and OnFilterImage().

◆ m_ModelPointsDilationSize

int mitk::GrabCutOpenCVImageFilter::m_ModelPointsDilationSize
protected

◆ m_ResultImageId

int mitk::GrabCutOpenCVImageFilter::m_ResultImageId
protected

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

Definition at line 255 of file mitkGrabCutOpenCVImageFilter.h.

Referenced by ConvertMaskToModelPointsList(), and GetResultImageId().

◆ m_ResultMask

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

Definition at line 247 of file mitkGrabCutOpenCVImageFilter.h.

Referenced by ConvertMaskToModelPointsList(), and GetResultMask().

◆ m_UseOnlyRegionAroundModelPoints

bool mitk::GrabCutOpenCVImageFilter::m_UseOnlyRegionAroundModelPoints
protected

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