Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
19 #include "cv.h"
20 
21 namespace mitk {
23  : m_NewCropRegionSet(false)
24 {
25 }
26 
28 {
29  if (m_CropRegion.width == 0)
30  {
31  MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter")
32  << "Cropping cannot be done without setting a non-empty crop region first.";
33  return false;
34  }
35 
36  cv::Size imageSize = image.size();
37 
38  if (m_CropRegion.x >= imageSize.width || m_CropRegion.y >= imageSize.height)
39  {
40  MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter")
41  << "Cannot crop if top left corner of the roi is outside the image boundaries.";
42  return false;
43  }
44 
45  // We can try and correct too large boundaries (do this only once
46  // after a new crop region was set.
48  {
49  m_NewCropRegionSet = false;
50 
51  if ( m_CropRegion.x + m_CropRegion.width > imageSize.width)
52  {
53  m_CropRegion.width = imageSize.width - m_CropRegion.x;
54  MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter")
55  << "Changed too large roi in x direction to fit the image size.";
56  }
57  if ( m_CropRegion.y + m_CropRegion.height > imageSize.height)
58  {
59  m_CropRegion.height = imageSize.height - m_CropRegion.y;
60  MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter")
61  << "Changed too large roi in y direction to fit the image size.";
62  }
63  }
64 
65  // crop image and copy cropped region into the input image
66  cv::Mat buffer = image(m_CropRegion);
67  buffer.copyTo(image);
68 
69  return true;
70 }
71 
72 void CropOpenCVImageFilter::SetCropRegion( cv::Rect cropRegion )
73 {
74  // First, let's do some basic checks to make sure rectangle is inside of actual image
75  if (cropRegion.x < 0)
76  {
77  MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter")
78  << "Changed negative x value in roi to 0.";
79  cropRegion.x = 0;
80  }
81  if (cropRegion.y < 0)
82  {
83  cropRegion.y = 0;
84  MITK_WARN("AbstractOpenCVImageFilter")("CropOpenCVImageFilter")
85  << "Changed negative y value in roi to 0.";
86  }
87 
88  // Nothing to save, throw an exception
89  if ( cropRegion.height < 0 || cropRegion.width < 0 )
90  {
91  mitkThrow() << "Invalid boundaries supplied to USImageVideoSource::SetRegionOfInterest()";
92  }
93 
94  m_CropRegion = cropRegion;
95 }
96 
97 void CropOpenCVImageFilter::SetCropRegion( int topLeftX, int topLeftY, int bottomRightX, int bottomRightY )
98 {
99  this->SetCropRegion( cv::Rect(topLeftX, topLeftY, bottomRightX - topLeftX, bottomRightY - topLeftY) );
100 }
101 
103 {
104  return m_CropRegion;
105 }
106 
108 {
109  return m_CropRegion.width == 0;
110 }
111 } // 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:23
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()
bool m_NewCropRegionSet
True if no image was filtered since last set of a crop region.