Medical Imaging Interaction Toolkit  2016.11.0
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) static Pointer New()
 
Pointer Clone () const
 
 GrabCutOpenCVImageFilter ()
 
virtual ~GrabCutOpenCVImageFilter ()
 
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)
 

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

List holding image indices of the model points.

Definition at line 65 of file mitkGrabCutOpenCVImageFilter.h.

Constructor & Destructor Documentation

mitk::GrabCutOpenCVImageFilter::GrabCutOpenCVImageFilter ( )

Definition at line 32 of file mitkGrabCutOpenCVImageFilter.cpp.

mitk::GrabCutOpenCVImageFilter::~GrabCutOpenCVImageFilter ( )
virtual

Definition at line 48 of file mitkGrabCutOpenCVImageFilter.cpp.

Member Function Documentation

Pointer mitk::GrabCutOpenCVImageFilter::Clone ( ) const
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 350 of file mitkGrabCutOpenCVImageFilter.cpp.

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 271 of file mitkGrabCutOpenCVImageFilter.cpp.

References compare(), and MITK_WARN.

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 234 of file mitkGrabCutOpenCVImageFilter.cpp.

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 139 of file mitkGrabCutOpenCVImageFilter.cpp.

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 160 of file mitkGrabCutOpenCVImageFilter.cpp.

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 191 of file mitkGrabCutOpenCVImageFilter.cpp.

References compare(), and MITK_WARN.

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 144 of file mitkGrabCutOpenCVImageFilter.cpp.

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 149 of file mitkGrabCutOpenCVImageFilter.cpp.

mitk::GrabCutOpenCVImageFilter::mitkClassMacro ( GrabCutOpenCVImageFilter  ,
AbstractOpenCVImageFilter   
)
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 56 of file mitkGrabCutOpenCVImageFilter.cpp.

References MITK_WARN.

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 321 of file mitkGrabCutOpenCVImageFilter.cpp.

References compare(), and GMM_COMPONENTS_COUNT.

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 86 of file mitkGrabCutOpenCVImageFilter.cpp.

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 93 of file mitkGrabCutOpenCVImageFilter.cpp.

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 101 of file mitkGrabCutOpenCVImageFilter.cpp.

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 108 of file mitkGrabCutOpenCVImageFilter.cpp.

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 116 of file mitkGrabCutOpenCVImageFilter.cpp.

References MITK_ERROR, and mitkThrow.

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 134 of file mitkGrabCutOpenCVImageFilter.cpp.

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 128 of file mitkGrabCutOpenCVImageFilter.cpp.

Member Data Documentation

unsigned int mitk::GrabCutOpenCVImageFilter::m_AdditionalWidth
protected

Definition at line 241 of file mitkGrabCutOpenCVImageFilter.h.

ModelPointsList mitk::GrabCutOpenCVImageFilter::m_BackgroundPoints
protected

Definition at line 245 of file mitkGrabCutOpenCVImageFilter.h.

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

Definition at line 242 of file mitkGrabCutOpenCVImageFilter.h.

unsigned int mitk::GrabCutOpenCVImageFilter::m_CurrentProcessImageNum
protected

Definition at line 251 of file mitkGrabCutOpenCVImageFilter.h.

ModelPointsList mitk::GrabCutOpenCVImageFilter::m_ForegroundPoints
protected

Definition at line 244 of file mitkGrabCutOpenCVImageFilter.h.

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

Definition at line 248 of file mitkGrabCutOpenCVImageFilter.h.

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

Definition at line 247 of file mitkGrabCutOpenCVImageFilter.h.

int mitk::GrabCutOpenCVImageFilter::m_InputImageId
protected

id of the image currently set as m_InputImage

Definition at line 254 of file mitkGrabCutOpenCVImageFilter.h.

int mitk::GrabCutOpenCVImageFilter::m_ModelPointsDilationSize
protected

Definition at line 238 of file mitkGrabCutOpenCVImageFilter.h.

int mitk::GrabCutOpenCVImageFilter::m_ResultImageId
protected

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

Definition at line 257 of file mitkGrabCutOpenCVImageFilter.h.

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

Definition at line 249 of file mitkGrabCutOpenCVImageFilter.h.

bool mitk::GrabCutOpenCVImageFilter::m_UseOnlyRegionAroundModelPoints
protected

Definition at line 240 of file mitkGrabCutOpenCVImageFilter.h.


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