Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkCropOpenCVImageFilter.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
15 
16 namespace mitk {
18  : m_NewCropRegionSet(false)
19 {
20 }
21 
23 {
24  if (m_CropRegion.width == 0)
25  {
26  MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter")
27  << "Cropping cannot be done without setting a non-empty crop region first.";
28  return false;
29  }
30 
31  cv::Size imageSize = image.size();
32 
33  if (m_CropRegion.x >= imageSize.width || m_CropRegion.y >= imageSize.height)
34  {
35  MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter")
36  << "Cannot crop if top left corner of the roi is outside the image boundaries.";
37  return false;
38  }
39 
40  // We can try and correct too large boundaries (do this only once
41  // after a new crop region was set.
43  {
44  m_NewCropRegionSet = false;
45 
46  if ( m_CropRegion.x + m_CropRegion.width > imageSize.width)
47  {
48  m_CropRegion.width = imageSize.width - m_CropRegion.x;
49  MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter")
50  << "Changed too large roi in x direction to fit the image size.";
51  }
52  if ( m_CropRegion.y + m_CropRegion.height > imageSize.height)
53  {
54  m_CropRegion.height = imageSize.height - m_CropRegion.y;
55  MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter")
56  << "Changed too large roi in y direction to fit the image size.";
57  }
58  }
59 
60  // crop image and copy cropped region into the input image
61  cv::Mat buffer = image(m_CropRegion);
62  buffer.copyTo(image);
63 
64  return true;
65 }
66 
67 void CropOpenCVImageFilter::SetCropRegion( cv::Rect cropRegion )
68 {
69  // First, let's do some basic checks to make sure rectangle is inside of actual image
70  if (cropRegion.x < 0)
71  {
72  MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter")
73  << "Changed negative x value in roi to 0.";
74  cropRegion.x = 0;
75  }
76  if (cropRegion.y < 0)
77  {
78  cropRegion.y = 0;
79  MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter")
80  << "Changed negative y value in roi to 0.";
81  }
82 
83  // Nothing to save, throw an exception
84  if ( cropRegion.height < 0 || cropRegion.width < 0 )
85  {
86  mitkThrow() << "Invalid boundaries supplied to USImageVideoSource::SetRegionOfInterest()";
87  }
88 
89  m_CropRegion = cropRegion;
90 }
91 
92 void CropOpenCVImageFilter::SetCropRegion( int topLeftX, int topLeftY, int bottomRightX, int bottomRightY )
93 {
94  this->SetCropRegion( cv::Rect(topLeftX, topLeftY, bottomRightX - topLeftX, bottomRightY - topLeftY) );
95 }
96 
98 {
99  return m_CropRegion;
100 }
101 
103 {
104  return m_CropRegion.width == 0;
105 }
106 } // namespace mitk
cv::Rect m_CropRegion
Defines the region which will be cropped from the image.
DataCollection - Class to facilitate loading/accessing structured data.
cv::Rect GetCropRegion()
Returns region, which was set by mitk::CropOpenCVImageFilter::SetCropRegion().
#define MITK_WARN
Definition: mitkLogMacros.h:19
bool OnFilterImage(cv::Mat &image) override
Crops image to rectangle given by mitk::CropOpenCVImageFilter::SetCropRegion.
void SetCropRegion(cv::Rect cropRegion)
Set region of interest for cropping.
#define mitkThrow()
mitk::Image::Pointer image
bool m_NewCropRegionSet
True if no image was filtered since last set of a crop region.