CustusX  15.8
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxImageDefaultTFGenerator.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
34 
35 #include "vtkImageData.h"
36 #include "cxImage.h"
37 #include "cxImageLUT2D.h"
38 #include "cxImageTF3D.h"
39 #include "math.h"
40 
41 namespace cx
42 {
43 
45 {
46 
47 }
48 
50 {
51  // add shading for known preoperative modalities
52  if (mImage->getModality().contains("CT") || mImage->getModality().contains("MR"))
53  mImage->setShadingOn(true);
54 }
55 
57 {
58  ImageLUT2DPtr tf(new ImageLUT2D());
59 
60  double_pair fullRange = this->getFullScalarRange();
61  double_pair range = this->guessInitialScalarRange();
62  double smin = range.first;
63  double smax = range.second;
64 
65  IntIntMap opacity;
66  opacity[fullRange.first - 1] = 0;
67  opacity[fullRange.first] = 255;
68  tf->resetAlpha(opacity);
69 
70  ColorMap colors;
71  colors[smin] = QColor(Qt::black);
72  colors[smax] = QColor(Qt::white);
73  tf->resetColor(colors);
74 
75  return tf;
76 }
77 
78 namespace
79 {
80 int myround(double val)
81 {
82  return int(val+0.5);
83 }
84 }
85 
87 {
88  ImageTF3DPtr tf(new ImageTF3D());
89 
90  double_pair range = this->guessInitialScalarRange();
91 
92  double smin = range.first;
93  double smax = range.second;
94  double srange = smax - smin;
95 
96  IntIntMap opacity;
97  // Note the ordering: add in descending order to ensure zero is
98  // always written into smin, also for binary volumes
99  // Round is required for binary volumes.
100  opacity[smin + myround(0.5*srange)] = 255;
101  opacity[smin + myround(0.3*srange)] = 255.0 * 0.7;
102  opacity[smin + myround(0.1*srange)] = 0;
103  tf->resetAlpha(opacity);
104 
105  ColorMap colors;
106  colors[smin] = QColor(Qt::black);
107  colors[smax] = QColor(Qt::white);
108  tf->resetColor(colors);
109 
110  return tf;
111 }
112 
113 bool ImageDefaultTFGenerator::hasValidInitialWindow() const
114 {
115  return mImage->getInitialWindowWidth()>0;
116 }
117 
118 double_pair ImageDefaultTFGenerator::guessInitialScalarRange() const
119 {
120  double_pair srange = this->getFullScalarRange();
121 
122  if (this->hasValidInitialWindow())
123  {
124  srange = this->getInitialWindowRange();
125  }
126  // no initial window: add heuristics for each modality
127  if (!this->hasValidInitialWindow())
128  {
129  if (this->isUnsignedChar())
130  {
131  srange.first = 0;
132  srange.second = 255;
133 
134  if (this->looksLikeBinaryImage())
135  {
136  srange.first = 0;
137  srange.second = 1;
138  }
139  }
140  if (mImage->getModality().contains("CT"))
141  {
142  srange = this->guessCTRange();
143  }
144  if (mImage->getModality().contains("MR"))
145  {
146  srange = this->guessMRRange();
147  }
148  }
149 
150  srange = this->ensureNonZeroRoundedRange(srange);
151  return srange;
152 }
153 
154 bool ImageDefaultTFGenerator::isUnsignedChar() const
155 {
156  return mImage->getBaseVtkImageData()->GetScalarType() == VTK_UNSIGNED_CHAR;
157 }
158 
159 bool ImageDefaultTFGenerator::looksLikeBinaryImage() const
160 {
161  double_pair r = this->getFullScalarRange();
162  return similar(r.first, 0) && similar(r.second, 1);
163 }
164 
165 
166 double_pair ImageDefaultTFGenerator::ensureNonZeroRoundedRange(double_pair range) const
167 {
168  range.first = int(range.first+0.5);
169  range.second = int(range.second+0.5);
170  range.second = std::max(range.second, range.first+1);
171  return range;
172 }
173 
174 double_pair ImageDefaultTFGenerator::getFullScalarRange() const
175 {
176  double smin = mImage->getBaseVtkImageData()->GetScalarRange()[0];
177  double smax = mImage->getBaseVtkImageData()->GetScalarRange()[1];
178  return std::make_pair(smin, smax);
179 }
180 
181 double_pair ImageDefaultTFGenerator::getInitialWindowRange() const
182 {
183  double smin = mImage->getInitialWindowLevel() - mImage->getInitialWindowWidth()/2;
184  double smax = mImage->getInitialWindowLevel() + mImage->getInitialWindowWidth()/2;
185  return std::make_pair(smin, smax);
186 }
187 
188 double_pair ImageDefaultTFGenerator::guessCTRange() const
189 {
190  // signed: [-1024...3072]
191  // choose a default from lung to bone, approximately.
192  double smin = -500;
193  double smax = 900;
194  if (0 >= mImage->getMin()) // unsigned: [0..4096]
195  {
196  int ct_signed2unsigned = 1024;
197  smin += ct_signed2unsigned;
198  smax += ct_signed2unsigned;
199  }
200  return std::make_pair(smin, smax);
201 }
202 
203 double_pair ImageDefaultTFGenerator::guessMRRange() const
204 {
205  double_pair srange = this->getFullScalarRange();
206  srange.second *= 0.25; // usually lots of high-intensity noise of no interest
207  return srange;
208 }
209 
210 
211 } // namespace cx
Handler for the transfer functions used in 3d image volumes.
Definition: cxImageTF3D.h:72
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
Handling of color and opacity for 2D slices.
Definition: cxImageLUT2D.h:68
std::pair< double, double > double_pair
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
std::map< int, QColor > ColorMap
Definition: cxImage.h:57
boost::shared_ptr< class ImageLUT2D > ImageLUT2DPtr
std::map< int, int > IntIntMap
Definition: cxImage.h:56
boost::shared_ptr< class ImageTF3D > ImageTF3DPtr