diff --git a/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.cpp b/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.cpp index 0f0aba8f43..ee58efa78c 100644 --- a/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.cpp +++ b/Modules/OpenCVVideoSupport/Commands/mitkCropOpenCVImageFilter.cpp @@ -1,90 +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) + 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."; + << "Changed too large roi in x direction to fit the image size."; } - if ( cropRegion.y + cropRegion.height >= image.size().height) + 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."; + << "Changed too 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) { 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/US/USFilters/mitkUSImageVideoSource.cpp b/Modules/US/USFilters/mitkUSImageVideoSource.cpp index fbae264b08..493d07140f 100644 --- a/Modules/US/USFilters/mitkUSImageVideoSource.cpp +++ b/Modules/US/USFilters/mitkUSImageVideoSource.cpp @@ -1,154 +1,154 @@ /*=================================================================== 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. ===================================================================*/ // MITK HEADER #include "mitkUSImageVideoSource.h" #include "mitkImage.h" #include "mitkCropOpenCVImageFilter.h" #include "mitkConvertGrayscaleOpenCVImageFilter.h" //OpenCV HEADER #include #include //Other #include mitk::USImageVideoSource::USImageVideoSource() : itk::Object(), m_VideoCapture(new cv::VideoCapture()), m_IsVideoReady(false), m_IsGreyscale(false), m_OpenCVToMitkFilter(mitk::OpenCVToMitkImageFilter::New()), m_ResolutionOverrideWidth(0), m_ResolutionOverrideHeight(0), m_ResolutionOverride(false), m_ImageFilter(0), m_GrayscaleFilter(mitk::ConvertGrayscaleOpenCVImageFilter::New()), m_CropFilter(mitk::CropOpenCVImageFilter::New()) { m_OpenCVToMitkFilter->SetCopyBuffer(false); } mitk::USImageVideoSource::~USImageVideoSource() { } void mitk::USImageVideoSource::SetVideoFileInput(std::string path) { m_VideoCapture->open(path.c_str()); if(!m_VideoCapture->isOpened()) // check if we succeeded m_IsVideoReady = false; else m_IsVideoReady = true; // If Override is enabled, use it if (m_ResolutionOverride) { m_VideoCapture->set(CV_CAP_PROP_FRAME_WIDTH, this->m_ResolutionOverrideWidth); m_VideoCapture->set(CV_CAP_PROP_FRAME_HEIGHT, this->m_ResolutionOverrideHeight); } } void mitk::USImageVideoSource::SetCameraInput(int deviceID) { m_VideoCapture->open(deviceID); if(!m_VideoCapture->isOpened()) // check if we succeeded m_IsVideoReady = false; else m_IsVideoReady = true; // If Override is enabled, use it if (m_ResolutionOverride) { m_VideoCapture->set(CV_CAP_PROP_FRAME_WIDTH, this->m_ResolutionOverrideWidth); m_VideoCapture->set(CV_CAP_PROP_FRAME_HEIGHT, this->m_ResolutionOverrideHeight); } } void mitk::USImageVideoSource::SetColorOutput(bool isColor){ m_IsGreyscale = !isColor; } int mitk::USImageVideoSource::GetImageHeight() { if (m_VideoCapture) return m_VideoCapture->get(CV_CAP_PROP_FRAME_HEIGHT); else return 0; } int mitk::USImageVideoSource::GetImageWidth() { if (m_VideoCapture) return m_VideoCapture->get(CV_CAP_PROP_FRAME_WIDTH); else return 0; } void mitk::USImageVideoSource::SetRegionOfInterest(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY) { m_CropFilter->SetCropRegion(topLeftX, topLeftY, bottomRightX, bottomRightY); m_IsCropped = true; } void mitk::USImageVideoSource::RemoveRegionOfInterest(){ m_IsCropped = false; } mitk::USImage::Pointer mitk::USImageVideoSource::GetNextImage() { // Loop video if necessary if (m_VideoCapture->get(CV_CAP_PROP_POS_AVI_RATIO) >= 0.99 ) m_VideoCapture->set(CV_CAP_PROP_POS_AVI_RATIO, 0); // Setup pointers cv::Mat image; // Retrieve image *m_VideoCapture >> image; // get a new frame from camera // If region of interest was set, crop image - if ( m_IsCropped ) { m_CropFilter->filterImage(image); } + if ( m_IsCropped ) { m_CropFilter->FilterImage(image); } // If this source is set to deliver greyscale images, convert it - if ( m_IsGreyscale ) { m_GrayscaleFilter->filterImage(image); } + if ( m_IsGreyscale ) { m_GrayscaleFilter->FilterImage(image); } // Execute filter, if an additional filter is specified - if ( m_ImageFilter.IsNotNull() ) { m_ImageFilter->filterImage(image); } + if ( m_ImageFilter.IsNotNull() ) { m_ImageFilter->FilterImage(image); } // Convert to MITK-Image IplImage ipl_img = image; this->m_OpenCVToMitkFilter->SetOpenCVImage(&ipl_img); this->m_OpenCVToMitkFilter->Update(); // OpenCVToMitkImageFilter returns a standard mitk::image. We then transform it into an USImage mitk::USImage::Pointer result = mitk::USImage::New(this->m_OpenCVToMitkFilter->GetOutput()); // Clean up image.release(); return result; } void mitk::USImageVideoSource::OverrideResolution(int width, int height){ this->m_ResolutionOverrideHeight = height; this->m_ResolutionOverrideWidth = width; if (m_VideoCapture != 0) { m_VideoCapture->set(CV_CAP_PROP_FRAME_WIDTH, width); m_VideoCapture->set(CV_CAP_PROP_FRAME_HEIGHT, height); } }