MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkOpenCVToMitkImageFilterTest.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
13// mitk includes
15#include <mitkStandardFileLocations.h>
16#include <mitkTestingMacros.h>
17#include <mitkTestFixture.h>
18#include <itksys/SystemTools.hxx>
19#include <mitkIOUtil.h>
20#include <opencv2/imgproc.hpp>
21#include <opencv2/imgcodecs.hpp>
22#include <thread>
23
31class mitkTestOpenCVToMITKImageFilterThread : public itk::Object
32{
33public:
34
37
39
40protected:
41
46
48
49 std::thread Thread;
50
51 cv::Mat currentImage;
52
53 mitk::OpenCVToMitkImageFilter::Pointer m_testedFilter;
54
56 {
57 while (ThreadRunning)
58 {
59 m_testedFilter->SetOpenCVMat(currentImage);
60 m_testedFilter->Update();
61 mitk::Image::Pointer result;
62 result = m_testedFilter->GetOutput();
63 }
64 }
65
66
68 {
69 this->DoSomething();
70 }
71
72public:
73
74 void Start()
75 {
76 ThreadRunning = true;
77 this->Thread = std::thread(&mitkTestOpenCVToMITKImageFilterThread::ThreadStartTracking, this);
78 }
79
80 void Stop()
81 {
82 ThreadRunning = false;
83 if (this->Thread.joinable())
84 this->Thread.join();
85 }
86
87 void setFilter(mitk::OpenCVToMitkImageFilter::Pointer testedFilter)
88 {
89 m_testedFilter = testedFilter;
90 }
91
92 void setImage(cv::Mat image)
93 {
94 currentImage = image;
95 }
96
97};
98
99class mitkOpenCVToMitkImageFilterTestSuite : public mitk::TestFixture
100{
101 CPPUNIT_TEST_SUITE(mitkOpenCVToMitkImageFilterTestSuite);
102 MITK_TEST(TestInitialization);
103 MITK_TEST(TestThreadSafety);
104
105 CPPUNIT_TEST_SUITE_END();
106
107private:
108 cv::Mat image1,image2,image3,image4,image5;
109 mitk::OpenCVToMitkImageFilter::Pointer testFilter;
110
111public:
112
113 void setUp() override
114 {
115 image1 = cv::imread(GetTestDataFilePath("NrrdWritingTestImage.jpg").c_str());
116 image2 = cv::imread(GetTestDataFilePath("Png2D-bw.png").c_str());
117 image3 = cv::imread(GetTestDataFilePath("OpenCV-Data/CroppedImage.png").c_str());
118 image4 = cv::imread(GetTestDataFilePath("OpenCV-Data/GrabCutMask.png").c_str());
119 image5 = cv::imread(GetTestDataFilePath("OpenCV-Data/GrabCutOutput.png").c_str());
120
121 testFilter = mitk::OpenCVToMitkImageFilter::New();
122
123 //change input
124 testFilter->SetOpenCVMat(image1);
125 }
126
127 void tearDown() override
128 {
129 }
130
132 {
133 testFilter = mitk::OpenCVToMitkImageFilter::New();
134 MITK_TEST_OUTPUT(<<"Testing Initialization");
135 }
136
138 {
139 MITK_TEST_OUTPUT(<< "Testing Thread Safety with 2 Threads");
140
141 //create two threads
142 auto newThread1 = mitkTestOpenCVToMITKImageFilterThread::New();
143 newThread1->setFilter(testFilter);
144 newThread1->setImage(image1);
145 auto newThread2 = mitkTestOpenCVToMITKImageFilterThread::New();
146 newThread2->setFilter(testFilter);
147 newThread2->setImage(image1);
148
149 //start both
150 newThread1->Start();
151 newThread2->Start();
152
153 int delay = 1;
154
155 for (int i = 0; i < 100; i++)
156 {
157 //std::cout << "Run " << i << std::endl;
158 //wait a bit
159 itksys::SystemTools::Delay(delay);
160
161 //change input
162 newThread1->setImage(image2);
163 newThread1->setImage(image3);
164
165 //wait a bit
166 itksys::SystemTools::Delay(delay);
167
168 //change input
169 newThread1->setImage(image4);
170 newThread1->setImage(image5);
171
172 //wait a bit
173 itksys::SystemTools::Delay(delay);
174
175 //change input
176 newThread1->setImage(image2);
177 newThread1->setImage(image2);
178
179 //wait a bit
180 itksys::SystemTools::Delay(delay);
181
182 //change input
183 newThread1->setImage(image3);
184 newThread1->setImage(image3);
185
186 //wait a bit
187 itksys::SystemTools::Delay(delay);
188 }
189
190 //stop both threads
191 newThread1->Stop();
192 newThread2->Stop();
193
194 MITK_TEST_OUTPUT(<< "Testing Thread Safety with 2 Threads");
195
196 }
197
198
199private:
200
201};
202MITK_TEST_SUITE_REGISTRATION(mitkOpenCVToMitkImageFilter)
Objects of this class can start an internal thread by calling the Start() method. The thread is then ...
mitkClassMacroItkParent(mitkTestOpenCVToMITKImageFilterThread, itk::Object)
void setFilter(mitk::OpenCVToMitkImageFilter::Pointer testedFilter)
itkNewMacro(mitkTestOpenCVToMITKImageFilterThread)
mitk::OpenCVToMitkImageFilter::Pointer m_testedFilter