File: itkOpenCVImageBridgeRGBTest.cxx

package info (click to toggle)
insighttoolkit5 5.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704,404 kB
  • sloc: cpp: 783,697; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 461; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (301 lines) | stat: -rw-r--r-- 8,883 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         https://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/

#include <iostream>

#include "itkOpenCVImageBridge.h"
#include "itkImageFileReader.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "itkOpenCVVideoIOFactory.h"
#include "itkOpenCVTestHelper.h"
#include "itkTestingMacros.h"

#if defined(CV_VERSION_EPOCH) // OpenCV 2.4.x
#  include "highgui.h"
#else                                  // OpenCV >= 3.x
#  include "opencv2/imgcodecs.hpp"     // cv::imread
#  include "opencv2/imgproc/types_c.h" // CV_RGB2BGR
#  if (CV_VERSION_MAJOR == 3)
#    include "opencv2/imgcodecs/imgcodecs_c.h"        // CV_LOAD_IMAGE_COLOR
#  else                                               // OpenCV 4.x and later
#    include "opencv2/imgcodecs/legacy/constants_c.h" // CV_LOAD_IMAGE_COLOR
#  endif
#endif

// Compare RGBPixel Images
template <typename TPixelValue, unsigned int VDimension>
TPixelValue
RGBImageTotalAbsDifference(const itk::Image<itk::RGBPixel<TPixelValue>, VDimension> * valid,
                           const itk::Image<itk::RGBPixel<TPixelValue>, VDimension> * test)
{
  using PixelType = itk::RGBPixel<TPixelValue>;
  using RGBImageType = itk::Image<PixelType, VDimension>;
  using IterType = itk::ImageRegionConstIteratorWithIndex<RGBImageType>;

  IterType validIt(valid, valid->GetLargestPossibleRegion());
  validIt.GoToBegin();

  IterType testIt(test, test->GetLargestPossibleRegion());
  testIt.GoToBegin();

  TPixelValue totalDiff = 0;

  while (!validIt.IsAtEnd())
  {
    PixelType validPx = validIt.Get();
    PixelType testPx = testIt.Get();

    if (validIt.GetIndex() != testIt.GetIndex())
    {
      std::cerr << "validIt.GetIndex() != testIt.GetIndex()" << std::endl;
      return 1;
    }

    TPixelValue localDiff{};

    for (unsigned int i = 0; i < 3; ++i)
    {
      localDiff += itk::Math::abs(validPx[i] - testPx[i]);
    }

    if (localDiff != TPixelValue{})
    {
      IterType testIt2 = testIt;
      ++testIt2;
      IterType validIt2 = validIt;
      ++validIt2;
      std::cerr << testIt.GetIndex() << " [ " << validPx << ' ' << validIt2.Get() << "] != [ " << testPx << ' '
                << testIt2.Get() << " ]" << std::endl;
      return localDiff;
    }

    totalDiff += localDiff;

    ++validIt;
    ++testIt;
  }

  return totalDiff;
}


// Convert the data in the IplImage to the templated type
template <typename TPixelType>
IplImage *
ConvertIplImageDataType(IplImage * in)
{
  int depth = 0;

  // Figure out the right output type
  if (typeid(TPixelType) == typeid(unsigned char))
  {
    depth = IPL_DEPTH_8U;
  }
  else if (typeid(TPixelType) == typeid(char))
  {
    depth = IPL_DEPTH_8S;
  }
  else if (typeid(TPixelType) == typeid(unsigned short))
  {
    depth = IPL_DEPTH_16U;
  }
  else if (typeid(TPixelType) == typeid(short))
  {
    depth = IPL_DEPTH_16S;
  }
  else if (typeid(TPixelType) == typeid(float))
  {
    depth = IPL_DEPTH_32F;
  }
  else if (typeid(TPixelType) == typeid(double))
  {
    depth = IPL_DEPTH_64F;
  }
  else
  {
    itkGenericExceptionMacro("OpenCV doesn't support the requested type");
  }

  IplImage * out = cvCreateImage(cvSize(in->width, in->height), depth, in->nChannels);
  cvConvertScale(in, out);
  return out;
}

// Templated test function to do the heavy lifting for RGB case
template <typename TValue, unsigned int VDimension>
int
itkOpenCVImageBridgeTestTemplatedRGB(char * argv0, char * argv1)
{
  // type alias
  const unsigned int Dimension = VDimension;
  using ValueType = TValue;
  using PixelType = itk::RGBPixel<ValueType>;
  using ComponentType = typename PixelType::ComponentType;
  using ImageType = itk::Image<PixelType, Dimension>;
  using ReaderType = itk::ImageFileReader<ImageType>;

  // Read the image directly
  auto reader = ReaderType::New();
  reader->SetFileName(argv1);

  ITK_TRY_EXPECT_NO_EXCEPTION(reader->Update());


  typename ImageType::Pointer baselineImage = reader->GetOutput();

  // Test IplImage -> itk::Image
  IplImage * inIpl = cvLoadImage(argv0, CV_LOAD_IMAGE_COLOR);
  if (!inIpl)
  {
    std::cerr << "Could not load input as IplImage " << argv0 << std::endl;
    return EXIT_FAILURE;
  }
  typename ImageType::Pointer outIplITK = itk::OpenCVImageBridge::IplImageToITKImage<ImageType>(inIpl);

  ComponentType itkIplDiff1 = RGBImageTotalAbsDifference<ComponentType, Dimension>(baselineImage, outIplITK);

  // Check results of IplImage -> itk::Image
  if (itkIplDiff1 != ComponentType{})
  {
    std::cerr << "Images didn't match for pixel type " << typeid(PixelType).name()
              << " for IplImage -> ITK (RGB), with image difference = " << itkIplDiff1 << std::endl;
    cvReleaseImage(&inIpl);
    return EXIT_FAILURE;
  }

  // Test cv::Mat -> itk::Image
  cv::Mat                     inMat = cv::imread(argv0);
  typename ImageType::Pointer outMatITK = itk::OpenCVImageBridge::CVMatToITKImage<ImageType>(inMat);

  ComponentType itkCvMatDiff = RGBImageTotalAbsDifference<ComponentType, Dimension>(baselineImage, outMatITK);

  // Check results of cv::Mat -> itk::Image
  if (itkCvMatDiff != ComponentType{})
  {
    std::cerr << "Images didn't match for pixel type " << typeid(PixelType).name() << " for cv::Mat -> ITK (RGB)"
              << std::endl;
    cvReleaseImage(&inIpl);
    return EXIT_FAILURE;
  }

  // Test itk::Image -> IplImage
  IplImage * outIpl = itk::OpenCVImageBridge::ITKImageToIplImage<ImageType>(baselineImage);

  // check results of itk::Image -> IplImage
  IplImage * dataConvertedInIpl = ConvertIplImageDataType<ValueType>(inIpl);

  double itkIplDiff = cvNorm(outIpl, dataConvertedInIpl);

  if (itkIplDiff != 0.0)
  {
    std::cerr << "Images didn't match for pixel type " << typeid(ValueType).name()
              << " for ITK -> IplImage (RGB), with image difference = " << itkIplDiff << std::endl;
    cvReleaseImage(&dataConvertedInIpl);
    cvReleaseImage(&inIpl);
    cvReleaseImage(&outIpl);
    return EXIT_FAILURE;
  }

  // Test itk::Image -> cv::Mat
  cv::Mat outMat = itk::OpenCVImageBridge::ITKImageToCVMat<ImageType>(baselineImage);

  // check results of itk::Image -> IplImage
  IplImage outMatAsIpl = cvIplImage(outMat);
  double   itkMatDiff = cvNorm(&outMatAsIpl, dataConvertedInIpl);
  if (itkMatDiff != 0.0)
  {
    std::cerr << "Images didn't match for pixel type " << typeid(PixelType).name() << " for ITK -> cv::Mat (RGB)"
              << std::endl;
    cvReleaseImage(&dataConvertedInIpl);
    cvReleaseImage(&inIpl);
    cvReleaseImage(&outIpl);
    return EXIT_FAILURE;
  }

  // Clean up and return successfully
  cvReleaseImage(&dataConvertedInIpl);
  cvReleaseImage(&inIpl);
  cvReleaseImage(&outIpl);
  return EXIT_SUCCESS;
}

template <typename TValue>
int
itkRunRGBTest(char * argv0, char * argv1)
{
  if (itkOpenCVImageBridgeTestTemplatedRGB<TValue, 2>(argv0, argv1) == EXIT_FAILURE)
  {
    return EXIT_FAILURE;
  }
  if (itkOpenCVImageBridgeTestTemplatedRGB<TValue, 3>(argv0, argv1) == EXIT_FAILURE)
  {
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

int
itkOpenCVImageBridgeRGBTest(int argc, char * argv[])
{

  if (argc != 4)
  {
    std::cerr << "Missing parameters." << std::endl;
    std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv);
    std::cerr << " RGBJPGImage RGBMHAImage RGBImage2" << std::endl;
    return EXIT_FAILURE;
  }

  // Test for RGB types
  // Note: OpenCV only supports unsigned char, unsigned short, and float for
  // color conversion
  std::cout << "rgb" << std::endl;

  if (itkRunRGBTest<unsigned char>(argv[1], argv[2]) == EXIT_FAILURE)
  {
    return EXIT_FAILURE;
  }
  if (itkRunRGBTest<unsigned short>(argv[1], argv[2]) == EXIT_FAILURE)
  {
    return EXIT_FAILURE;
  }
  if (itkRunRGBTest<float>(argv[1], argv[2]) == EXIT_FAILURE)
  {
    return EXIT_FAILURE;
  }

  std::cout << "rgb 513x512" << std::endl;

  if (itkRunRGBTest<unsigned char>(argv[3], argv[3]) == EXIT_FAILURE)
  {
    return EXIT_FAILURE;
  }
  if (itkRunRGBTest<unsigned short>(argv[3], argv[3]) == EXIT_FAILURE)
  {
    return EXIT_FAILURE;
  }
  if (itkRunRGBTest<float>(argv[3], argv[3]) == EXIT_FAILURE)
  {
    return EXIT_FAILURE;
  }


  std::cout << "Test finished." << std::endl;
  return EXIT_SUCCESS;
}