diff --git a/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.cpp b/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.cpp index 980abde20c..4249e125c1 100644 --- a/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.cpp +++ b/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.cpp @@ -1,111 +1,110 @@ /*=================================================================== 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 { CropOpenCVImageFilter::CropOpenCVImageFilter( ) : m_NewCropRegionSet(false) { } bool CropOpenCVImageFilter::OnFilterImage( cv::Mat& image ) { if (m_CropRegion.width == 0) { MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter") << "Cropping cannot be done without setting a non-empty crop region first."; return false; } cv::Size imageSize = image.size(); if (m_CropRegion.x >= imageSize.width || m_CropRegion.y >= imageSize.height) { MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter") << "Cannot crop if top left corner of the roi is outside the image boundaries."; return false; } // We can try and correct too large boundaries (do this only once // after a new crop region was set. if (m_NewCropRegionSet) { m_NewCropRegionSet = false; if ( m_CropRegion.x + m_CropRegion.width > imageSize.width) { m_CropRegion.width = imageSize.width - m_CropRegion.x; MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter") << "Changed too large roi in x direction to fit the image size."; } if ( m_CropRegion.y + m_CropRegion.height > imageSize.height) { m_CropRegion.height = imageSize.height - m_CropRegion.y; MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter") << "Changed too large roi in y direction to fit the image size."; } } // crop image and copy cropped region into the input image cv::Mat buffer = image(m_CropRegion); buffer.copyTo(image); 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) { 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; } bool CropOpenCVImageFilter::GetIsCropRegionEmpty( ) { return m_CropRegion.width == 0; } } // namespace mitk diff --git a/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.h b/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.h index 77a6d72c0c..054b4bda48 100644 --- a/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.h +++ b/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.h @@ -1,79 +1,79 @@ /*=================================================================== 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" +#include "opencv2/core.hpp" //itk headers #include namespace mitk { class MITKOPENCVVIDEOSUPPORT_EXPORT CropOpenCVImageFilter : public AbstractOpenCVImageFilter { public: mitkClassMacro(CropOpenCVImageFilter, AbstractOpenCVImageFilter); itkFactorylessNewMacro(Self) itkCloneMacro(Self) CropOpenCVImageFilter( ); /** * \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 OnFilterImage( cv::Mat& image ) override; /** * \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( ); /** * \return True if a non-empty crop region was set before. */ bool GetIsCropRegionEmpty( ); protected: /** * \brief Defines the region which will be cropped from the image. */ cv::Rect m_CropRegion; /** * \brief True if no image was filtered since last set of a crop region. */ bool m_NewCropRegionSet; }; } // namespace mitk #endif // mitkAbstractOpenCVImageFilter_h