MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkCropOpenCVImageFilterTest.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 <opencv2/imgproc.hpp>
17#include <opencv2/imgproc/imgproc_c.h>
18#include <opencv2/imgcodecs.hpp>
19
20static bool ImagesAreEqualInGray(const cv::Mat& img1, const cv::Mat& img2)
21{
22 cv::Mat grayImg1;
23 cv::Mat grayImg2;
24
25 cv::cvtColor(img1, grayImg1, CV_RGB2GRAY, 1);
26 cv::cvtColor(img2, grayImg2, CV_RGB2GRAY, 1);
27
28 return cv::countNonZero(grayImg1 != grayImg2) == 0;
29}
30
31static void CropTestLoadedImage(std::string mitkImagePath, std::string mitkCroppedImagePath)
32{
33 cv::Mat image = cv::imread(mitkImagePath.c_str());
34 cv::Mat croppedImage = cv::imread(mitkCroppedImagePath.c_str());
35
36 MITK_INFO << mitkImagePath.c_str();
37 MITK_INFO << mitkCroppedImagePath.c_str();
38
39 mitk::CropOpenCVImageFilter::Pointer cropFilter = mitk::CropOpenCVImageFilter::New();
40
41 // try to crop without setting a region of interest
42 cv::Mat testImage = image.clone();
43 MITK_TEST_CONDITION( ! cropFilter->FilterImage(testImage), "Filter function must return false if no region of interest is set.");
44 MITK_TEST_CONDITION(ImagesAreEqualInGray(testImage, image), "Image should not be changed yet.");
45
46 // set region of interst now and then try to crop again
47 cv::Rect roi = cv::Rect(0,0, testImage.cols, testImage.rows);
48 cropFilter->SetCropRegion(roi);
49 MITK_TEST_CONDITION(cropFilter->FilterImage(testImage), "Filter function should return successfully.");
50 MITK_TEST_CONDITION(ImagesAreEqualInGray(testImage, image), "Image should not be changed if cropping with a roi of the whole image.");
51
52 // test if filter corrects negative roi position
53 cv::Rect roiWrong = cv::Rect(-1,-1, 2, 2);
54 roi = cv::Rect(0,0,2,2);
55 cropFilter->SetCropRegion(roiWrong);
56 MITK_TEST_CONDITION(cropFilter->FilterImage(testImage), "Filter function should return successfully.");
57 MITK_TEST_CONDITION(ImagesAreEqualInGray(testImage, image(roi)), "Image should be equal to directly cropped image whith correct roi.");
58
59 // test whith "normal" roi
60 testImage = image.clone();
61 roi = cv::Rect( 150,100,100,100 );
62 cropFilter->SetCropRegion(roi);
63 MITK_TEST_CONDITION(cropFilter->FilterImage(testImage), "Filter function should return successfully.");
64 MITK_TEST_CONDITION(ImagesAreEqualInGray(testImage, croppedImage), "Image should be equal to cropped image (loaded from data directory).");
65
66 // test with not correctable roi
67 roiWrong = cv::Rect( 5,5,-1,-1 );
68 MITK_TEST_FOR_EXCEPTION(mitk::Exception, cropFilter->SetCropRegion(roiWrong));
69
70 // test with rois where the top left corner is outside the image boundaries
71 roiWrong = cv::Rect( testImage.cols,0,1,1 );
72 cropFilter->SetCropRegion(roiWrong);
73 MITK_TEST_CONDITION(!cropFilter->FilterImage(testImage),
74 "Filter function should return unsuccessfully if top left corner is outside image boundary (cols).");
75 roiWrong = cv::Rect( 0,testImage.rows,1,1 );
76 cropFilter->SetCropRegion(roiWrong);
77 MITK_TEST_CONDITION(!cropFilter->FilterImage(testImage),
78 "Filter function should return unsuccessfully if top left corner is outside image boundary (rows).");
79 roiWrong = cv::Rect( testImage.cols,testImage.rows,1,1 );
80 cropFilter->SetCropRegion(roiWrong);
81 MITK_TEST_CONDITION(!cropFilter->FilterImage(testImage),
82 "Filter function should return unsuccessfully if top left corner is outside image boundary (cols+rows).");
83}
84
88int mitkCropOpenCVImageFilterTest(int argc, char* argv[])
89{
90 MITK_TEST_BEGIN("CropOpenCVImageFilter")
91
92 MITK_TEST_CONDITION_REQUIRED(argc > 2, "At least three parameters needed for this test.");
93
94 CropTestLoadedImage(argv[1], argv[2]);
95
96 MITK_TEST_END(); // always end with this!
97
98}
int mitkCropOpenCVImageFilterTest(int argc, char *argv[])