MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkCropOpenCVImageFilter.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
14
15
16namespace 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
67void 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
92void 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
void SetCropRegion(cv::Rect cropRegion)
Set region of interest for cropping.
cv::Rect m_CropRegion
Defines the region which will be cropped from the image.
cv::Rect GetCropRegion()
Returns region, which was set by mitk::CropOpenCVImageFilter::SetCropRegion().
bool m_NewCropRegionSet
True if no image was filtered since last set of a crop region.
bool OnFilterImage(cv::Mat &image) override
Crops image to rectangle given by mitk::CropOpenCVImageFilter::SetCropRegion.
IGT Exceptions.