diff --git a/Modules/US/USFilters/mitkUSImageVideoSource.cpp b/Modules/US/USFilters/mitkUSImageVideoSource.cpp index 34e9ced354..e80dc4005c 100644 --- a/Modules/US/USFilters/mitkUSImageVideoSource.cpp +++ b/Modules/US/USFilters/mitkUSImageVideoSource.cpp @@ -1,111 +1,128 @@ /*=================================================================== 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" //OpenCV HEADER #include #include //Other #include mitk::USImageVideoSource::USImageVideoSource() : itk::Object() { m_IsVideoReady = false; - m_IsMetadataReady = false; - m_IsGeometryReady = false; m_IsGreyscale = true; this->m_OpenCVToMitkFilter = mitk::OpenCVToMitkImageFilter::New(); } mitk::USImageVideoSource::~USImageVideoSource() { } void mitk::USImageVideoSource::SetVideoFileInput(std::string path) { m_OpenCVVideoSource = mitk::OpenCVVideoSource::New(); - // Example: "C:\\Users\\maerz\\Videos\\Debut\\us.avi" m_OpenCVVideoSource->SetVideoFileInput(path.c_str(),true,false); m_OpenCVVideoSource->StartCapturing(); m_OpenCVVideoSource->FetchFrame(); // Let's see if we have been successful m_IsVideoReady = m_OpenCVVideoSource->IsCapturingEnabled(); } void mitk::USImageVideoSource::SetCameraInput(int deviceID) { m_OpenCVVideoSource = mitk::OpenCVVideoSource::New(); - m_OpenCVVideoSource->SetVideoCameraInput(deviceID); m_OpenCVVideoSource->StartCapturing(); m_OpenCVVideoSource->FetchFrame(); // Let's see if we have been successful m_IsVideoReady = m_OpenCVVideoSource->IsCapturingEnabled(); } void mitk::USImageVideoSource::SetColorOutput(bool isColor){ m_IsGreyscale = !isColor; } +void mitk::USImageVideoSource::SetRegionOfInterest(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY) +{ + // First, let's do some basic checks to make sure rectangle is inside of actual image + if (topLeftX < 0) topLeftX = 0; + if (topLeftY < 0) topLeftY = 0; + + if (bottomRightX > m_OpenCVVideoSource->GetImageWidth()) bottomRightX = m_OpenCVVideoSource->GetImageWidth(); + if (bottomRightX > m_OpenCVVideoSource->GetImageHeight()) bottomRightY = m_OpenCVVideoSource->GetImageHeight(); + + if (topLeftX > bottomRightX) mitkThrow() << "Invalid boundaries supplied to USImageVideoSource::SetRegionOfInterest()"; + if (topLeftY > bottomRightY) mitkThrow() << "Invalid boundaries supplied to USImageVideoSource::SetRegionOfInterest()"; + + m_CropRegion = cv::Rect(topLeftX, topLeftY, bottomRightX - topLeftX, bottomRightY - topLeftY); +} + +void mitk::USImageVideoSource::RemoveRegionOfInterest(){ + m_CropRegion.width = 0; + m_CropRegion.height = 0; +} mitk::USImage::Pointer mitk::USImageVideoSource::GetNextImage() { // Setup Pointers - IplImage *rgbImage = NULL; - IplImage *greyImage = NULL; + cv::Mat image; + cv::Mat buffer; - //Get Dimensions and init rgb + //Get dimensions and init rgb + int width = m_OpenCVVideoSource->GetImageWidth(); int height = m_OpenCVVideoSource->GetImageHeight(); - int width = m_OpenCVVideoSource->GetImageWidth(); - rgbImage = cvCreateImage(cvSize(width,height),8,3); // Get Frame from Source - m_OpenCVVideoSource->GetCurrentFrameAsOpenCVImage(rgbImage); + image = m_OpenCVVideoSource->GetImage(); m_OpenCVVideoSource->FetchFrame(); - // If this is a greyscale image, convert it and put into the filter. - if (m_IsGreyscale){ - greyImage = cvCreateImage(cvSize(width,height),8,1); - cvCvtColor( rgbImage, greyImage, CV_RGB2GRAY ); - this->m_OpenCVToMitkFilter->SetOpenCVImage(greyImage); - } else { - this->m_OpenCVToMitkFilter->SetOpenCVImage(rgbImage); + // if Region of interest is set, crop image + if (m_CropRegion.width > 0){ + buffer = image(m_CropRegion); + image = buffer; + } + // If this is a greyscale image, convert it + if (m_IsGreyscale) + { + cv::cvtColor(image, buffer, CV_RGB2GRAY, 1); + image = buffer; } + IplImage ipl_img = image; + this->m_OpenCVToMitkFilter->SetOpenCVImage(&ipl_img); + this->m_OpenCVToMitkFilter->Update(); - // OpenCVToMitkImageFilter returns a standard mit::image. We then transform it into an USImage + // OpenCVToMitkImageFilter returns a standard mitk::image. We then transform it into an USImage mitk::USImage::Pointer result = mitk::USImage::New(this->m_OpenCVToMitkFilter->GetOutput(0)); - - cvReleaseImage (&rgbImage); - cvReleaseImage (&greyImage); return result; } diff --git a/Modules/US/USFilters/mitkUSImageVideoSource.h b/Modules/US/USFilters/mitkUSImageVideoSource.h index d93e0fdd6e..f527be52ff 100644 --- a/Modules/US/USFilters/mitkUSImageVideoSource.h +++ b/Modules/US/USFilters/mitkUSImageVideoSource.h @@ -1,96 +1,99 @@ /*=================================================================== 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 MITKUSImageVideoSource_H_HEADER_INCLUDED_ #define MITKUSImageVideoSource_H_HEADER_INCLUDED_ #include #include "mitkUSImage.h" #include "mitkOpenCVVideoSource.h" #include "mitkOpenCVToMitkImageFilter.h" namespace mitk { /**Documentation * \brief This class can be pointed to a video file or a videodevice and delivers USImages with default metadata Sets * * \ingroup US */ class MitkUS_EXPORT USImageVideoSource : public itk::Object { public: mitkClassMacro(USImageVideoSource, itk::ProcessObject); itkNewMacro(Self); /** *\brief Opens a video file for streaming. If nothing goes wrong, the * VideoSource is ready to deliver images after calling this function. */ void SetVideoFileInput(std::string path); /** *\brief Opens a video device for streaming. Takes the Device id. Try -1 for "grab the first you can get" * which works quite well if only one device is available. If nothing goes wrong, the * VideoSource is ready to deliver images after calling this function. */ void SetCameraInput(int deviceID); /** - *\brief Opens a video device for streaming. Takes the Device id. Try -1 for "grab the first you can get" - * which works quite well if only one device is available. If nothing goes wrong, the - * VideoSource is ready to deliver images after calling this function. + *\brief Sets the output image to rgb or grayscale. Output is grayscale by default + * and can be set to color by passing true, or to grayscale again by passing false. */ void SetColorOutput(bool isColor); + /** + * /brief Defines the cropping area. The rectangle will be justified to the image borders + * if the given rectangle is larger than the video source. If a correct rectangle is given, + * The dimensions of the output image will be equal to those of the rectangle. + */ + void SetRegionOfInterest(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY); + + /** + * /brief Removes the region of interest. Produced images will be uncropped after call. + */ + void RemoveRegionOfInterest(); + /** *\brief Retrieves the next frame. This will typically be the next frame in a file * or the last cahced file in a devcie. */ mitk::USImage::Pointer GetNextImage(); // Getter & Setter itkGetMacro(OpenCVVideoSource, mitk::OpenCVVideoSource::Pointer); itkSetMacro(OpenCVVideoSource, mitk::OpenCVVideoSource::Pointer); itkGetMacro(IsVideoReady, bool); - itkGetMacro(IsMetadataReady, bool); - itkGetMacro(IsGeometryReady, bool); protected: USImageVideoSource(); virtual ~USImageVideoSource(); - /** * \brief The source of the video */ mitk::OpenCVVideoSource::Pointer m_OpenCVVideoSource; - /** - * \brief The Following flags are used internally, to assure that all necessary steps are taken before capturing - */ bool m_IsVideoReady; - bool m_IsMetadataReady; - bool m_IsGeometryReady; bool m_IsGreyscale; - CvRect m_CropRegion; + cv::Rect m_CropRegion; mitk::OpenCVToMitkImageFilter::Pointer m_OpenCVToMitkFilter; }; } // namespace mitk #endif /* MITKUSImageVideoSource_H_HEADER_INCLUDED_ */