diff --git a/Modules/OpenCVVideoSupport/Commands/mitkConvertGrayscaleOpenCVImageFilter.cpp b/Modules/OpenCVVideoSupport/Commands/mitkConvertGrayscaleOpenCVImageFilter.cpp index fc70d7685c..3e73885fe1 100644 --- a/Modules/OpenCVVideoSupport/Commands/mitkConvertGrayscaleOpenCVImageFilter.cpp +++ b/Modules/OpenCVVideoSupport/Commands/mitkConvertGrayscaleOpenCVImageFilter.cpp @@ -1,34 +1,36 @@ /*=================================================================== 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 "cv.h" namespace mitk { bool ConvertGrayscaleOpenCVImageFilter::FilterImage( cv::Mat& image ) { cv::Mat buffer; cv::cvtColor(image, buffer, CV_RGB2GRAY, 1); + + // content of buffer should now be the conten of image image.release(); image = buffer; return true; } } // namespace mitk diff --git a/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.cpp b/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.cpp index 85c2145e17..0f0aba8f43 100644 --- a/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.cpp +++ b/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.cpp @@ -1,76 +1,90 @@ /*=================================================================== 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 "cv.h" namespace mitk { bool CropOpenCVImageFilter::FilterImage( cv::Mat& image ) { if (m_CropRegion.width == 0) { MITK_ERROR("AbstractOpenCVImageFilter")("CropOpenCVImageFilter") << "Cropping cannot be done without setting a non-empty crop region first."; return false; } cv::Rect cropRegion = m_CropRegion; // We can try and correct too large boundaries if ( cropRegion.x + cropRegion.width >= image.size().width) { cropRegion.width = image.size().width - cropRegion.x; + MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter") + << "Changed to large roi in x direction to fit the image size."; } if ( cropRegion.y + cropRegion.height >= image.size().height) { cropRegion.height = image.size().height - cropRegion.y; + MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter") + << "Changed to large roi in y direction to fit the image size."; } cv::Mat buffer = image(cropRegion); image.release(); image = buffer; return true; } void CropOpenCVImageFilter::SetCropRegion( cv::Rect cropRegion ) { // First, let's do some basic checks to make sure rectangle is inside of actual image - if (cropRegion.x < 0) { cropRegion.x = 0; } - if (cropRegion.y < 0) { cropRegion.y = 0; } + if (cropRegion.x < 0) + { + MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter") + << "Changed negative x value in roi to 0."; + cropRegion.x = 0; + } + if (cropRegion.y < 0) + { + cropRegion.y = 0; + MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter") + << "Changed negative y value in roi to 0."; + } // Nothing to save, throw an exception if ( cropRegion.height < 0 || cropRegion.width < 0 ) { mitkThrow() << "Invalid boundaries supplied to USImageVideoSource::SetRegionOfInterest()"; } m_CropRegion = cropRegion; } void CropOpenCVImageFilter::SetCropRegion( int topLeftX, int topLeftY, int bottomRightX, int bottomRightY ) { this->SetCropRegion( cv::Rect(topLeftX, topLeftY, bottomRightX - topLeftX, bottomRightY - topLeftY) ); } cv::Rect CropOpenCVImageFilter::GetCropRegion( ) { return m_CropRegion; } } // namespace mitk diff --git a/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.h b/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.h index f3eb88ccc5..2f8b9d32ed 100644 --- a/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.h +++ b/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.h @@ -1,55 +1,66 @@ /*=================================================================== 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. ===================================================================*/ #ifndef mitkAbstractOpenCVImageFilter_h #define mitkAbstractOpenCVImageFilter_h #include "mitkAbstractOpenCVImageFilter.h" #include "cv.h" //itk headers #include namespace mitk { class MITK_OPENCVVIDEOSUPPORT_EXPORT CropOpenCVImageFilter : public AbstractOpenCVImageFilter { public: mitkClassMacro(CropOpenCVImageFilter, AbstractOpenCVImageFilter); itkNewMacro(Self); /** * \brief Crops image to rectangle given by mitk::CropOpenCVImageFilter::SetCropRegion. * \return false if no crop region was set or the crop region width is zero, true otherwise. */ bool FilterImage( cv::Mat& image ); + /** + * \brief Set region of interest for cropping. + */ void SetCropRegion( cv::Rect cropRegion ); + + /** + * \brief Set region of interest for cropping. + */ void SetCropRegion( int topLeftX, int topLeftY, int bottomRightX, int bottomRightY ); + + /** + * \brief Returns region, which was set by mitk::CropOpenCVImageFilter::SetCropRegion(). + */ cv::Rect GetCropRegion( ); protected: /** * \brief Defines the region which will be cropped from the image. */ cv::Rect m_CropRegion; }; } // namespace mitk #endif // mitkAbstractOpenCVImageFilter_h