diff --git a/Modules/OpenCVVideoSupport/Testing/CMakeLists.txt b/Modules/OpenCVVideoSupport/Testing/CMakeLists.txt index 5d19e2f864..5cc25bd8f1 100644 --- a/Modules/OpenCVVideoSupport/Testing/CMakeLists.txt +++ b/Modules/OpenCVVideoSupport/Testing/CMakeLists.txt @@ -1,7 +1,8 @@ MITK_CREATE_MODULE_TESTS() -if(BUILD_TESTING AND MODULE_IS_ENABLED) -mitkAddCustomModuleTest(mitkImageToOpenCVImageFilterTest mitkImageToOpenCVImageFilterTest - ${MITK_DATA_DIR}/RenderingTestData/rgbImage.png -) -endif() + +# if(BUILD_TESTING AND MODULE_IS_ENABLED) +# mitkAddCustomModuleTest(mitkImageToOpenCVImageFilterTest mitkImageToOpenCVImageFilterTest +# ${MITK_DATA_DIR}/RenderingTestData/rgbImage.png +# ) +# endif() diff --git a/Modules/OpenCVVideoSupport/Testing/files.cmake b/Modules/OpenCVVideoSupport/Testing/files.cmake index ab90f6e626..e09748753f 100644 --- a/Modules/OpenCVVideoSupport/Testing/files.cmake +++ b/Modules/OpenCVVideoSupport/Testing/files.cmake @@ -1,11 +1,13 @@ set(MODULE_TESTS ) set(MODULE_IMAGE_TESTS mitkImageToOpenCVImageFilterTest.cpp ) # list of images for which the tests are run set(MODULE_TESTIMAGES Png2D-bw.png -) \ No newline at end of file + RenderingTestData/rgbImage.png +NrrdWritingTestImage.jpg +) diff --git a/Modules/OpenCVVideoSupport/Testing/mitkImageToOpenCVImageFilterTest.cpp b/Modules/OpenCVVideoSupport/Testing/mitkImageToOpenCVImageFilterTest.cpp index d6bd26f3f6..e6c43902cf 100644 --- a/Modules/OpenCVVideoSupport/Testing/mitkImageToOpenCVImageFilterTest.cpp +++ b/Modules/OpenCVVideoSupport/Testing/mitkImageToOpenCVImageFilterTest.cpp @@ -1,167 +1,177 @@ /*=================================================================== 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 "mitkImageToOpenCVImageFilter.h" #include "mitkOpenCVToMitkImageFilter.h" #include #include #include #include // #include #include mitk::Image::Pointer LoadImage( std::string filename ) { mitk::ItkImageFileReader::Pointer reader = mitk::ItkImageFileReader::New(); reader->SetFileName ( filename.c_str() ); reader->Update(); if ( reader->GetOutput() == NULL ) itkGenericExceptionMacro("File "<GetOutput(); return image; } static void testGeneratedImage() { // create itk rgb image typedef unsigned char PixelType; typedef itk::Image< itk::RGBPixel, 2 > ImageType; ImageType::Pointer itkImage = ImageType::New(); ImageType::IndexType start; start[0] = 0; // first index on X start[1] = 0; // first index on Y ImageType::SizeType size; size[0] = 50; // size along X size[1] = 40; // size along Y ImageType::RegionType region; region.SetSize( size ); region.SetIndex( start ); itkImage->SetRegions( region ); itkImage->Allocate(); typedef itk::ImageRegionIterator IteratorType; IteratorType it(itkImage, region); float twoThirdsTheWidth = size[0] / 4; unsigned int x=0, y=0; // create rgb pic for ( it.GoToBegin(); !it.IsAtEnd(); ++it ) { ImageType::PixelType newPixel; newPixel.SetRed(0); newPixel.SetGreen(0); // create asymmetric pic if( x > twoThirdsTheWidth ) newPixel.SetBlue(0); else newPixel.SetBlue(255); it.Set(newPixel); ++x; // next line found if( x == size[0] ) x = 0; } // debugging // itk::ImageFileWriter< ImageType >::Pointer writer = itk::ImageFileWriter< ImageType >::New(); // writer->SetFileName( "c:\\image.png" ); // writer->SetInput ( itkImage ); // writer->Update(); // import rgb image as MITK image mitk::Image::Pointer mitkImage = mitk::ImportItkImage( itkImage ); mitk::ImageToOpenCVImageFilter::Pointer _ImageToOpenCVImageFilter = mitk::ImageToOpenCVImageFilter::New(); _ImageToOpenCVImageFilter->SetImage( mitkImage ); IplImage* openCVImage = _ImageToOpenCVImageFilter->GetOpenCVImage(); MITK_TEST_CONDITION_REQUIRED( openCVImage != NULL, "Image returned by filter is not null."); // check byte size const unsigned int expectedSize = size[0] * size[1] * 3 * sizeof( unsigned char);// sizeof( PixelType ); const unsigned int realSize = openCVImage->width * openCVImage->height * openCVImage->nChannels * sizeof( unsigned char);//* sizeof ( PixelType ); MITK_TEST_CONDITION_REQUIRED( expectedSize == realSize, "Test expectedSize == realSize"); // check pixel values PixelType expectedBlueValue; CvScalar s; for (y = 0; (int)y < openCVImage->height; ++y) { for (x = 0; (int)x < openCVImage->width; ++x) { expectedBlueValue = 255; if(x > twoThirdsTheWidth) expectedBlueValue = 0; s = cvGet2D(openCVImage,y,x); if( s.val[0] != expectedBlueValue || s.val[1] != 0 || s.val[2] != 0 ) { std::cout << "Wrong RGB values in created OpenCV image" << std::endl; throw mitk::TestFailedException(); } } } // cvNamedWindow( "test" ); // cvShowImage( "test" , openCVImage ); // cvWaitKey(); } -static void testLoadedImage(mitk::Image::Pointer mitkImage) +static void testLoadedImage(std::string mitkImagePath) { + mitk::Image::Pointer testImage = LoadImage(mitkImagePath); mitk::ImageToOpenCVImageFilter::Pointer _ImageToOpenCVImageFilter = mitk::ImageToOpenCVImageFilter::New(); - _ImageToOpenCVImageFilter->SetImage( mitkImage ); + _ImageToOpenCVImageFilter->SetImage( testImage ); IplImage* openCVImage = _ImageToOpenCVImageFilter->GetOpenCVImage(); + IplImage* openCVImage_Ref = cvLoadImage(mitkImagePath.c_str()); MITK_TEST_CONDITION_REQUIRED( openCVImage != NULL, "Image returned by filter is not null."); - CvScalar s; - s=cvGet2D(img,i,j); // get the (i,j) pixel value - printf("B=%f, G=%f, R=%f\n",s.val[0],s.val[1],s.val[2]); - s.val[0]=111; - s.val[1]=111; - s.val[2]=111; + for(int i = 0 ; iheight ; i++) + { + for(int j = 0 ; jwidth ; j++) + { + CvScalar s; + s=cvGet2D(openCVImage,i,j); // get the (i,j) pixel value + CvScalar sRef; + sRef=cvGet2D(openCVImage_Ref,i,j); // get the (i,j) pixel value + for(int c = 0 ; c < openCVImage->nChannels ; c++) + { + MITK_TEST_CONDITION_REQUIRED( s.val[c] == sRef.val[c] , "All pixel values have to be equal"); + } + } + } + // cvNamedWindow( "test" ); // cvShowImage( "test" , openCVImage ); // cvWaitKey(); } /**Documentation * test for the class "ImageToOpenCVImageFilter". */ int mitkImageToOpenCVImageFilterTest(int argc, char* argv[]) { MITK_TEST_BEGIN("ImageToOpenCVImageFilter") -testGeneratedImage(); - mitk::Image::Pointer testImage = LoadImage(argv[1]); - testLoadedImage(testImage); + testGeneratedImage(); + testLoadedImage(argv[1]); // always end with this! MITK_TEST_END(); } diff --git a/Modules/OpenCVVideoSupport/mitkImageToOpenCVImageFilter.cpp b/Modules/OpenCVVideoSupport/mitkImageToOpenCVImageFilter.cpp index 73bd9bfb2b..e54d71aed4 100644 --- a/Modules/OpenCVVideoSupport/mitkImageToOpenCVImageFilter.cpp +++ b/Modules/OpenCVVideoSupport/mitkImageToOpenCVImageFilter.cpp @@ -1,122 +1,125 @@ /*=================================================================== 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 "mitkImageToOpenCVImageFilter.h" #include #include #include mitk::ImageToOpenCVImageFilter::ImageToOpenCVImageFilter() : m_OpenCVImage(0) { } mitk::ImageToOpenCVImageFilter::~ImageToOpenCVImageFilter() { m_OpenCVImage = 0; } int mitk::ImageToOpenCVImageFilter::GetDepth(const std::type_info& typeInfo) const { if(typeInfo == typeid(unsigned char)) { return IPL_DEPTH_8U; } else if(typeInfo == typeid(char)) { return IPL_DEPTH_8S; } else if(typeInfo == typeid(unsigned short)) { return IPL_DEPTH_16U; } else if(typeInfo == typeid(short)) { return IPL_DEPTH_16S; } else if(typeInfo == typeid(int)) { return IPL_DEPTH_32S; } else if(typeInfo == typeid(float)) { return IPL_DEPTH_32F; } else if(typeInfo == typeid(double)) { return IPL_DEPTH_64F; } // rgb image else if(typeInfo == typeid(UCRGBPixelType)) { return IPL_DEPTH_8U; } else if(typeInfo == typeid(USRGBPixelType)) { return IPL_DEPTH_16U; } else if(typeInfo == typeid(FloatRGBPixelType)) { return IPL_DEPTH_64F; } else if(typeInfo == typeid(DoubleRGBPixelType)) { return IPL_DEPTH_64F; } else return -1; } bool mitk::ImageToOpenCVImageFilter::CheckImage( mitk::Image* image ) { if(image == 0) { MITK_WARN << "MITK Image is 0"; return false; } if(image->GetDimension() != 2) { MITK_WARN << "Only 2D Images allowed"; return false; } return true; } IplImage* mitk::ImageToOpenCVImageFilter::GetOpenCVImage() { - if(!m_OpenCVImage && this->CheckImage( m_Image ) == true ) + if(!this->CheckImage( m_Image )) + return 0; + if(!m_OpenCVImage) { try { AccessFixedTypeByItk(m_Image.GetPointer(), ItkImageProcessing, MITK_ACCESSBYITK_PIXEL_TYPES_SEQ // gray image (UCRGBPixelType)(USRGBPixelType)(FloatRGBPixelType)(DoubleRGBPixelType), // rgb image (2) // dimensions ) } catch (const AccessByItkException& e) { std::cout << "Caught exception [from AccessFixedTypeByItk]: \n" << e.what() << "\n"; + return 0; } } return m_OpenCVImage; } void mitk::ImageToOpenCVImageFilter::SetImage( mitk::Image* _Image ) { - if(m_Image == _Image) return; +// if(m_Image == _Image) return; m_Image = _Image; m_OpenCVImage = 0; }