14#include <mitkTestingMacros.h>
16#include <opencv2/imgproc.hpp>
17#include <opencv2/imgproc/imgproc_c.h>
18#include <opencv2/imgcodecs.hpp>
20static bool ImagesAreEqualInGray(
const cv::Mat& img1,
const cv::Mat& img2)
25 cv::cvtColor(img1, grayImg1, CV_RGB2GRAY, 1);
26 cv::cvtColor(img2, grayImg2, CV_RGB2GRAY, 1);
28 return cv::countNonZero(grayImg1 != grayImg2) == 0;
31static void CropTestLoadedImage(std::string mitkImagePath, std::string mitkCroppedImagePath)
33 cv::Mat image = cv::imread(mitkImagePath.c_str());
34 cv::Mat croppedImage = cv::imread(mitkCroppedImagePath.c_str());
36 MITK_INFO << mitkImagePath.c_str();
37 MITK_INFO << mitkCroppedImagePath.c_str();
39 mitk::CropOpenCVImageFilter::Pointer cropFilter = mitk::CropOpenCVImageFilter::New();
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.");
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.");
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.");
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).");
67 roiWrong = cv::Rect( 5,5,-1,-1 );
68 MITK_TEST_FOR_EXCEPTION(mitk::Exception, cropFilter->SetCropRegion(roiWrong));
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).");
90 MITK_TEST_BEGIN(
"CropOpenCVImageFilter")
92 MITK_TEST_CONDITION_REQUIRED(argc > 2,
"At least three parameters needed for this test.");
94 CropTestLoadedImage(argv[1], argv[2]);
int mitkCropOpenCVImageFilterTest(int argc, char *argv[])