diff --git a/Modules/OpenCVVideoSupport/Testing/files.cmake b/Modules/OpenCVVideoSupport/Testing/files.cmake index 6e2ddb0530..374ff4d444 100644 --- a/Modules/OpenCVVideoSupport/Testing/files.cmake +++ b/Modules/OpenCVVideoSupport/Testing/files.cmake @@ -1,13 +1,15 @@ set(MODULE_TESTS ) set(MODULE_IMAGE_TESTS mitkImageToOpenCVImageFilterTest.cpp + mitkConvertGrayscaleOpenCVImageFilterTest.cpp + mitkCropOpenCVImageFilterTest.cpp ) # list of images for which the tests are run set(MODULE_TESTIMAGES Png2D-bw.png RenderingTestData/rgbImage.png #NrrdWritingTestImage.jpg ) diff --git a/Modules/OpenCVVideoSupport/Testing/mitkConvertGrayscaleOpenCVImageFilterTest.cpp b/Modules/OpenCVVideoSupport/Testing/mitkConvertGrayscaleOpenCVImageFilterTest.cpp new file mode 100644 index 0000000000..6c013faf8f --- /dev/null +++ b/Modules/OpenCVVideoSupport/Testing/mitkConvertGrayscaleOpenCVImageFilterTest.cpp @@ -0,0 +1,52 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#include "mitkConvertGrayscaleOpenCVImageFilter.h" +#include + +#include +#include + +static void ConvertTestLoadedImage(std::string mitkImagePath) +{ + cv::Mat image = cvLoadImage(mitkImagePath.c_str()); + + // directly convert the image for comparison + cv::Mat comparisonImg; + cv::cvtColor(image, comparisonImg, CV_RGB2GRAY, 1); + + mitk::ConvertGrayscaleOpenCVImageFilter::Pointer grayscaleFilter + = mitk::ConvertGrayscaleOpenCVImageFilter::New(); + + MITK_TEST_CONDITION_REQUIRED(grayscaleFilter->FilterImage(image), "Filtering should return true for success."); + + MITK_TEST_CONDITION_REQUIRED(image.channels() == 1, "Image must not have more than one channel after grayscale conversion."); + + MITK_TEST_CONDITION_REQUIRED(cv::countNonZero(image != comparisonImg), "All pixel values must be the same between the two converted images."); +} + +/**Documentation + * test for the class "ConvertGrayscaleOpenCVImageFilter". + */ +int mitkConvertGrayscaleOpenCVImageFilterTest(int argc, char* argv[]) +{ + MITK_TEST_BEGIN("ConvertGrayscaleOpenCVImageFilter") + + ConvertTestLoadedImage(argv[1]); + // always end with this! + MITK_TEST_END(); + +} diff --git a/Modules/OpenCVVideoSupport/Testing/mitkCropOpenCVImageFilterTest.cpp b/Modules/OpenCVVideoSupport/Testing/mitkCropOpenCVImageFilterTest.cpp new file mode 100644 index 0000000000..16d3d686d4 --- /dev/null +++ b/Modules/OpenCVVideoSupport/Testing/mitkCropOpenCVImageFilterTest.cpp @@ -0,0 +1,69 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#include "mitkCropOpenCVImageFilter.h" +#include + +#include +#include + +static void CropTestLoadedImage(std::string mitkImagePath) +{ + cv::Mat image = cvLoadImage(mitkImagePath.c_str()); + cv::Mat compareImg = image.clone(); + + mitk::CropOpenCVImageFilter::Pointer cropFilter = mitk::CropOpenCVImageFilter::New(); + + // try to crop without setting a region of interest + MITK_TEST_CONDITION_REQUIRED( ! cropFilter->FilterImage(image), "Filter function must return false if no region of interest is set."); + MITK_TEST_CONDITION_REQUIRED(cv::countNonZero(image != compareImg), "Image should not be changed yet."); + + // set region of interst now and then try to crop again + cv::Rect roi = cv::Rect(0,0, image.cols, image.rows); + cropFilter->SetCropRegion(roi); + MITK_TEST_CONDITION_REQUIRED(cropFilter->FilterImage(image), "Filter function should return successfully."); + MITK_TEST_CONDITION_REQUIRED(cv::countNonZero(image != compareImg), "Image should not be changed if cropping with a roi of the whole image."); + + // test if filter corrects negative roi position + cv::Rect roiWrong = cv::Rect(-1,-1, 2, 2); + roi = cv::Rect(0,0,2,2); + cropFilter->SetCropRegion(roiWrong); + MITK_TEST_CONDITION_REQUIRED(cropFilter->FilterImage(image), "Filter function should return successfully."); + MITK_TEST_CONDITION_REQUIRED(cv::countNonZero(image != compareImg(roi)), "Image should be equal to directly cropped image whith correct roi."); + + // test whith "normal" roi + roi = cv::Rect( 5,5,10,10 ); + cropFilter->SetCropRegion(roi); + MITK_TEST_CONDITION_REQUIRED(cropFilter->FilterImage(image), "Filter function should return successfully."); + MITK_TEST_CONDITION_REQUIRED(cv::countNonZero(image != compareImg(roi)), "Image should be equal to directly cropped image."); + + // test with not correctable roi + roiWrong = cv::Rect( 5,5,-1,-1 ); + MITK_TEST_FOR_EXCEPTION(mitk::Exception, cropFilter->SetCropRegion(roiWrong)); +} + +/**Documentation + * test for the class "CropOpenCVImageFilter". + */ +int mitkCropOpenCVImageFilterTest(int argc, char* argv[]) +{ + MITK_TEST_BEGIN("CropOpenCVImageFilter") + + CropTestLoadedImage(argv[1]); + // always end with this! + MITK_TEST_END(); + +}