MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkOpenCVMitkConversionTest.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
16#include <mitkTestingMacros.h>
17#include <mitkITKImageImport.h>
18#include <mitkImageAccessByItk.h>
19#include <mitkIOUtil.h>
20#include "mitkImageReadAccessor.h"
21#include "mitkImageSliceSelector.h"
22
23// itk includes
24#include <itkRGBPixel.h>
25
26#include <iostream>
27
28// define test pixel indexes and intensities and other values
29typedef itk::RGBPixel< unsigned char > TestUCRGBPixelType;
30
32
33cv::Point pos1;
34cv::Point pos2;
35cv::Point pos3;
36
37cv::Vec3b color1;
38cv::Vec3b color2;
39cv::Vec3b color3;
40
44
45
46
52// Some declarations
53template<typename TPixel, unsigned int VImageDimension>
54void ComparePixels( itk::Image<itk::RGBPixel<TPixel>,VImageDimension>* image );
55void ReadImageDataAndConvertForthAndBack(std::string imageFileName);
56void ConvertCVMatForthAndBack(mitk::Image::Pointer inputForCVMat, std::string imageFileName);
57
58
59// Begin the test for mitkImage to OpenCV image conversion and back.
60int mitkOpenCVMitkConversionTest(int argc, char* argv[])
61{
62 MITK_TEST_BEGIN("ImageToOpenCVImageFilter")
63
64 // the first part of this test checks the conversion of a cv::Mat style OpenCV image.
65
66 // we build an cv::Mat image
67 MITK_INFO << "setting test values";
68 testImageSize = cv::Size(11,11);
69
70 pos1 = cv::Point(0,0);
71 pos2 = cv::Point(5,5);
72 pos3 = cv::Point(10,10);
73
74 color1 = cv::Vec3b(50,0,0);
75 color2 = cv::Vec3b(0,128,0);
76 color3 = cv::Vec3b(0,0,255);
77
78 greyValue1 = 0;
79 greyValue2 = 128;
80 greyValue3 = 255;
81
82 MITK_INFO << "generating test OpenCV image (RGB)";
83 cv::Mat testRGBImage = cv::Mat::zeros( testImageSize, CV_8UC3 );
84
85 // generate some test intensity values
86 testRGBImage.at<cv::Vec3b>(pos1)= color1;
87 testRGBImage.at<cv::Vec3b>(pos2)= color2;
88 testRGBImage.at<cv::Vec3b>(pos3)= color3;
89
90 // convert it to a mitk::Image
91 MITK_INFO << "converting OpenCV test image to mitk image and comparing scalar rgb values";
92 mitk::OpenCVToMitkImageFilter::Pointer openCvToMitkFilter =
93 mitk::OpenCVToMitkImageFilter::New();
94 openCvToMitkFilter->SetOpenCVMat( testRGBImage );
95 openCvToMitkFilter->Update();
96
97 mitk::Image::Pointer mitkImage = openCvToMitkFilter->GetOutput();
98 AccessFixedTypeByItk(mitkImage.GetPointer(), ComparePixels,
99 (itk::RGBPixel<unsigned char>), // rgb image
100 (2) );
101
102 // convert it back to OpenCV image
103 MITK_INFO << "converting mitk image to OpenCV image and comparing scalar rgb values";
104 mitk::ImageToOpenCVImageFilter::Pointer mitkToOpenCv = mitk::ImageToOpenCVImageFilter::New();
105 mitkToOpenCv->SetImage( mitkImage );
106 cv::Mat openCvImage = mitkToOpenCv->GetOpenCVMat();
107
108 // and test equality of the sentinel pixel
109 cv::Vec3b convertedColor1 = openCvImage.at<cv::Vec3b>(pos1);
110 cv::Vec3b convertedColor2 = openCvImage.at<cv::Vec3b>(pos2);
111 cv::Vec3b convertedColor3 = openCvImage.at<cv::Vec3b>(pos3);
112
113 MITK_TEST_CONDITION( color1 == convertedColor1, "Testing if initially created color values " << static_cast<int>( color1[0] ) << ", " << static_cast<int>( color1[1] ) << ", " << static_cast<int>( color1[2] ) << " matches the color values " << static_cast<int>( convertedColor1[0] ) << ", " << static_cast<int>( convertedColor1[1] ) << ", " << static_cast<int>( convertedColor1[2] ) << " at the same position " << pos1.x << ", " << pos1.y << " in the back converted OpenCV image" )
114
115 MITK_TEST_CONDITION( color2 == convertedColor2, "Testing if initially created color values " << static_cast<int>( color2[0] ) << ", " << static_cast<int>( color2[1] ) << ", " << static_cast<int>( color2[2] ) << " matches the color values " << static_cast<int>( convertedColor2[0] ) << ", " << static_cast<int>( convertedColor2[1] ) << ", " << static_cast<int>( convertedColor2[2] ) << " at the same position " << pos2.x << ", " << pos2.y << " in the back converted OpenCV image" )
116
117 MITK_TEST_CONDITION( color3 == convertedColor3, "Testing if initially created color values " << static_cast<int>( color3[0] ) << ", " << static_cast<int>( color3[1] ) << ", " << static_cast<int>( color3[2] ) << " matches the color values " << static_cast<int>( convertedColor3[0] ) << ", " << static_cast<int>( convertedColor3[1] ) << ", " << static_cast<int>( convertedColor3[2] ) << " at the same position " << pos3.x << ", " << pos3.y << " in the back converted OpenCV image" )
118
119 // the second part of this test checks the conversion of mitk::Image to cv::Mat and back.
120 for (int i = 1; i < argc; ++i)
121 {
123 }
124
125 MITK_TEST_END();
126}
127
128template<typename TPixel, unsigned int VImageDimension>
129void ComparePixels( itk::Image<itk::RGBPixel<TPixel>,VImageDimension>* image )
130{
131 typedef itk::RGBPixel<TPixel> PixelType;
132 typedef itk::Image<PixelType, VImageDimension> ImageType;
133
134 typename ImageType::IndexType pixelIndex;
135 pixelIndex[0] = pos1.x;
136 pixelIndex[1] = pos1.y;
137 PixelType onePixel = image->GetPixel( pixelIndex );
138
139 MITK_TEST_CONDITION( color1[0] == onePixel.GetBlue(), "Testing if blue value (= " << static_cast<int>(color1[0]) << ") at postion "
140 << pos1.x << ", " << pos1.y << " in OpenCV image is "
141 << "equals the blue value (= " << static_cast<int>(onePixel.GetBlue()) << ")"
142 << " in the generated mitk image");
143
144
145 pixelIndex[0] = pos2.x;
146 pixelIndex[1] = pos2.y;
147 onePixel = image->GetPixel( pixelIndex );
148
149 MITK_TEST_CONDITION( color2[1] == onePixel.GetGreen(), "Testing if green value (= " << static_cast<int>(color2[1]) << ") at postion "
150 << pos2.x << ", " << pos2.y << " in OpenCV image is "
151 << "equals the green value (= " << static_cast<int>(onePixel.GetGreen()) << ")"
152 << " in the generated mitk image");
153
154
155 pixelIndex[0] = pos3.x;
156 pixelIndex[1] = pos3.y;
157 onePixel = image->GetPixel( pixelIndex );
158
159 MITK_TEST_CONDITION( color3[2] == onePixel.GetRed(), "Testing if red value (= " << static_cast<int>(color3[2]) << ") at postion "
160 << pos3.x << ", " << pos3.y << " in OpenCV image is "
161 << "equals the red value (= " << static_cast<int>(onePixel.GetRed()) << ")"
162 << " in the generated mitk image");
163
164}
165
166void ReadImageDataAndConvertForthAndBack(std::string imageFileName)
167{
168 // first we load an mitk::Image from the data repository
169 mitk::Image::Pointer mitkTestImage = mitk::IOUtil::Load<mitk::Image>(imageFileName);
170
171 // some format checking
172 mitk::Image::Pointer resultImg = nullptr;
173 if( mitkTestImage->GetDimension() <= 3 )
174 {
175 if( mitkTestImage->GetDimension() > 2 && mitkTestImage->GetDimension(2) == 1 )
176 {
177 mitk::ImageSliceSelector::Pointer sliceSelector = mitk::ImageSliceSelector::New();
178 sliceSelector->SetInput(mitkTestImage);
179 sliceSelector->SetSliceNr(0);
180 sliceSelector->Update();
181 resultImg = sliceSelector->GetOutput()->Clone();
182 }
183 else if(mitkTestImage->GetDimension() < 3)
184 {
185 resultImg = mitkTestImage;
186 }
187 else
188 {
189 return; // 3D images are not supported, except with just one slice.
190 }
191 }
192 else
193 {
194 return; // 4D images are not supported!
195 }
196
197 ConvertCVMatForthAndBack(resultImg, imageFileName);
198}
199
200void ConvertCVMatForthAndBack(mitk::Image::Pointer inputForCVMat, std::string)
201{
202 // now we convert it to OpenCV Mat
203 mitk::ImageToOpenCVImageFilter::Pointer toOCvConverter = mitk::ImageToOpenCVImageFilter::New();
204 toOCvConverter->SetImage(inputForCVMat);
205 cv::Mat cvmatTestImage = toOCvConverter->GetOpenCVMat();
206
207 MITK_TEST_CONDITION_REQUIRED( !cvmatTestImage.empty(), "Conversion to cv::Mat successful!");
208
209 mitk::OpenCVToMitkImageFilter::Pointer toMitkConverter = mitk::OpenCVToMitkImageFilter::New();
210 toMitkConverter->SetOpenCVMat(cvmatTestImage);
211 toMitkConverter->Update();
212
213 // initialize the image with the input image, since we want to test equality and OpenCV does not feature geometries and spacing
214 mitk::Image::Pointer result = inputForCVMat->Clone();
215 mitk::ImageReadAccessor resultAcc(toMitkConverter->GetOutput(), toMitkConverter->GetOutput()->GetSliceData());
216 result->SetImportSlice(const_cast<void*>(resultAcc.GetData()));
217
218 if( result->GetPixelType().GetNumberOfComponents() == 1 )
219 {
220 MITK_ASSERT_EQUAL( result, inputForCVMat, "Testing equality of input and output image of cv::Mat conversion" );
221 }
222 else if( result->GetPixelType().GetNumberOfComponents() == 3 )
223 {
224 MITK_ASSERT_EQUAL( result, inputForCVMat, "Testing equality of input and output image of cv::Mat conversion" );
225 }
226 else
227 {
228 MITK_WARN << "Unhandled number of components used to test equality, please enhance test!";
229 }
230
231 // change OpenCV image to test if the filter gets updated
232 cv::Mat changedcvmatTestImage = cvmatTestImage.clone();
233 if (result->GetPixelType().GetBitsPerComponent() == sizeof(char)*8)
234 {
235 changedcvmatTestImage.at<char>(0,0) = cvmatTestImage.at<char>(0,0) != 0 ? 0 : 1;
236 }
237 else if (result->GetPixelType().GetBitsPerComponent() == sizeof(float)*8)
238 {
239 changedcvmatTestImage.at<float>(0,0) = cvmatTestImage.at<float>(0,0) != 0 ? 0 : 1;
240 }
241
242 toMitkConverter->SetOpenCVMat(changedcvmatTestImage);
243 toMitkConverter->Update();
244
245 MITK_TEST_NOT_EQUAL(toMitkConverter->GetOutput(), inputForCVMat, "Converted image must not be the same as before.");
246}
itk::Image< int, 3 > ImageType
int mitkOpenCVMitkConversionTest(int argc, char *argv[])
void ComparePixels(itk::Image< itk::RGBPixel< TPixel >, VImageDimension > *image)
void ConvertCVMatForthAndBack(mitk::Image::Pointer inputForCVMat, std::string imageFileName)
cv::Size testImageSize
void ReadImageDataAndConvertForthAndBack(std::string imageFileName)
itk::RGBPixel< unsigned char > TestUCRGBPixelType