MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkGrabCutOpenCVImageFilterTest.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
14#include <mitkTestingMacros.h>
15
16#include "itkIndex.h"
17#include <itksys/SystemTools.hxx>
18
19#include <opencv2/imgproc.hpp>
20#include <opencv2/imgproc/imgproc_c.h>
21#include <opencv2/imgcodecs.hpp>
22
24
25static void GrabCutTestLoadedImage(std::string imagePath, std::string maskPath, std::string resultMaskPath)
26{
27 // load test images
28 cv::Mat image = cv::imread(imagePath.c_str());
29 cv::Mat maskImage = cv::imread(maskPath.c_str());
30 cv::Mat resultMaskImage = cv::imread(resultMaskPath.c_str());
31
32 // make sure that the loaded mask is a gray scale image
33 cv::Mat maskImageGray;
34 cv::cvtColor(maskImage, maskImageGray, CV_RGB2GRAY, 1);
35
36 // make sure that the loaded reference image is a gray scale image
37 cv::Mat resultMaskImageGray;
38 cv::cvtColor(resultMaskImage, resultMaskImageGray, CV_RGB2GRAY, 1);
39
40 // extract foreground points from loaded mask image
41 cv::Mat foregroundMask, foregroundPoints;
42 cv::compare(maskImageGray, 250, foregroundMask, cv::CMP_GE);
43 cv::findNonZero(foregroundMask, foregroundPoints);
44
45 // push extracted forground points into a vector of itk indices
46 std::vector<itk::Index<2> > foregroundPointsVector;
47 for ( size_t n = 0; n < foregroundPoints.total(); ++n)
48 {
49 itk::Index<2> index;
50 index.SetElement(0, foregroundPoints.at<cv::Point>(n).x);
51 index.SetElement(1, foregroundPoints.at<cv::Point>(n).y);
52 foregroundPointsVector.push_back(index);
53 }
54
55 mitk::GrabCutOpenCVImageFilter::Pointer grabCutFilter = mitk::GrabCutOpenCVImageFilter::New();
56
57 int currentImageId = 0;
58
59 // test filtering with image set but no model points set
60 {
61 MITK_TEST_CONDITION(grabCutFilter->FilterImage(image), "Filtering should return true for sucess.")
62 cv::Mat resultMask = grabCutFilter->GetResultMask();
63 MITK_TEST_CONDITION(resultMask.empty(), "Result mask must be empty when no foreground points are set.")
64 }
65
66 // test filtering with very little model points set
67 {
68 std::vector<itk::Index<2> > littleForegroundPointsSet(foregroundPointsVector.begin(), foregroundPointsVector.begin()+3);
69 grabCutFilter->SetModelPoints(littleForegroundPointsSet);
70 grabCutFilter->FilterImage(image, ++currentImageId);
71
72 cv::Mat resultMask;
73 // wait up to ten seconds for the segmentation to finish
74 for (unsigned int n = 0; n < 100; ++n)
75 {
76 if ( grabCutFilter->GetResultImageId() == currentImageId )
77 {
78 resultMask = grabCutFilter->GetResultMask();
79 break;
80 }
81 itksys::SystemTools::Delay(100);
82 }
83
84 MITK_TEST_CONDITION(!resultMask.empty(),
85 "Result mask must not be empty when little ("
86 << littleForegroundPointsSet.size() <<") foreground points are set.");
87 }
88
89 // test filtering with image and model points set
90 {
91 grabCutFilter->SetModelPoints(foregroundPointsVector);
92 MITK_TEST_CONDITION(grabCutFilter->FilterImage(image, ++currentImageId), "Filtering should return true for sucess.")
93
94 cv::Mat resultMask;
95 // wait up to ten seconds for the segmentation to finish
96 for (unsigned int n = 0; n < 100; ++n)
97 {
98 if ( grabCutFilter->GetResultImageId() == currentImageId )
99 {
100 resultMask = grabCutFilter->GetResultMask();
101 break;
102 }
103 itksys::SystemTools::Delay(100);
104 }
105
106 MITK_TEST_CONDITION( ! resultMask.empty() && cv::countNonZero(resultMask != resultMaskImageGray) == 0,
107 "Filtered image should match reference image.")
108
109 // adding new image should still work
110 MITK_TEST_CONDITION(grabCutFilter->FilterImage(image), "Adding new image should still work.")
111 }
112
113
114 // test filtering with using only region around model points
115 // (but with really big additional width so that whole image should be used again)
116 {
117 grabCutFilter->SetUseOnlyRegionAroundModelPoints(image.cols);
118 grabCutFilter->FilterImage(image, ++currentImageId);
119
120 cv::Mat resultMask;
121 // wait up to ten seconds for the segmentation to finish
122 for (unsigned int n = 0; n < 100; ++n)
123 {
124 if (grabCutFilter->GetResultImageId() == currentImageId)
125 {
126 resultMask = grabCutFilter->GetResultMask();
127 break;
128 }
129
130 itksys::SystemTools::Delay(100);
131 }
132
133 MITK_TEST_CONDITION( ! resultMask.empty() && cv::countNonZero(resultMask != resultMaskImageGray) == 0,
134 "Filtered image with really big region used should match reference image again.")
135 }
136
137
138 // test filtering with using only region around model points
139 {
140 grabCutFilter->SetUseOnlyRegionAroundModelPoints(0);
141 grabCutFilter->FilterImage(image, ++currentImageId);
142
143 cv::Mat resultMask;
144 // wait up to ten seconds for the segmentation to finish
145 for (unsigned int n = 0; n < 100; ++n)
146 {
147 if (grabCutFilter->GetResultImageId() == currentImageId)
148 {
149 resultMask = grabCutFilter->GetResultMask();
150 break;
151 }
152
153 itksys::SystemTools::Delay(100);
154 }
155
156 cv::Mat nonPropablyBackgroundMask, modelPoints;
157 cv::compare(maskImageGray, 250, nonPropablyBackgroundMask, cv::CMP_GE);
158 cv::findNonZero(nonPropablyBackgroundMask, modelPoints);
159 cv::Rect boundingRect = cv::boundingRect(modelPoints);
160
161 cv::Mat compareMask(resultMask.rows, resultMask.cols, resultMask.type(), 0.0);
162 resultMaskImageGray(boundingRect).copyTo(compareMask(boundingRect));
163
164 MITK_TEST_CONDITION( ! resultMask.empty() && cv::countNonZero(resultMask != compareMask) == 0,
165 "Filtered image with region just around the model points used should match reference image again.")
166 }
167}
168
169int mitkGrabCutOpenCVImageFilterTest(int argc, char* argv[])
170{
171 MITK_TEST_BEGIN("GrabCutOpenCVImageFilter")
172
173 MITK_TEST_CONDITION_REQUIRED(argc == 4, "Test needs four command line parameters.")
174
175 GrabCutTestLoadedImage(argv[1], argv[2], argv[3]);
176
177 MITK_TEST_END() // always end with this!
178}
int mitkGrabCutOpenCVImageFilterTest(int argc, char *argv[])