! This method initializes a mitk::LevelWindow from an mitk::Image. The algorithm is as follows: Default to taking the central image slice for quick analysis. Compute the smallest (minValue), second smallest (min2ndValue), second largest (max2ndValue), and largest (maxValue) data value by traversing the pixel values only once. In the same scan it also computes the count of minValue values and max
| 258 | std:max values or has only 1 or 2 or 3 data values. |
| 259 | */ |
| 260 | void mitk::LevelWindow::SetAuto(const mitk::Image *image, |
| 261 | bool /*tryPicTags*/, |
| 262 | bool guessByCentralSlice, |
| 263 | unsigned selectedComponent) |
| 264 | { |
| 265 | if (IsFixed()) |
| 266 | return; |
| 267 | |
| 268 | if (image == nullptr || !image->IsInitialized()) |
| 269 | return; |
| 270 | |
| 271 | if (itk::IOComponentEnum::FLOAT == image->GetPixelType().GetComponentType() |
| 272 | || itk::IOComponentEnum::DOUBLE == image->GetPixelType().GetComponentType()) |
| 273 | { |
| 274 | m_IsFloatingImage = true; |
| 275 | } |
| 276 | else |
| 277 | { |
| 278 | m_IsFloatingImage = false; |
| 279 | } |
| 280 | |
| 281 | const mitk::Image *wholeImage = image; |
| 282 | ScalarType minValue = 0.0; |
| 283 | ScalarType maxValue = 0.0; |
| 284 | ScalarType min2ndValue = 0.0; |
| 285 | ScalarType max2ndValue = 0.0; |
| 286 | mitk::ImageSliceSelector::Pointer sliceSelector = mitk::ImageSliceSelector::New(); |
| 287 | if (guessByCentralSlice) |
| 288 | { |
| 289 | sliceSelector->SetInput(image); |
| 290 | sliceSelector->SetSliceNr(image->GetDimension(2) / 2); |
| 291 | sliceSelector->SetTimeNr(image->GetDimension(3) / 2); |
| 292 | sliceSelector->SetChannelNr(image->GetDimension(4) / 2); |
| 293 | sliceSelector->Update(); |
| 294 | image = sliceSelector->GetOutput(); |
| 295 | if (image == nullptr || !image->IsInitialized()) |
| 296 | return; |
| 297 | |
| 298 | minValue = image->GetStatistics()->GetScalarValueMin(0, selectedComponent); |
| 299 | maxValue = image->GetStatistics()->GetScalarValueMaxNoRecompute(); |
| 300 | min2ndValue = image->GetStatistics()->GetScalarValue2ndMinNoRecompute(); |
| 301 | max2ndValue = image->GetStatistics()->GetScalarValue2ndMaxNoRecompute(); |
| 302 | if (minValue == maxValue) |
| 303 | { |
| 304 | // guessByCentralSlice seems to have failed, lets look at all data |
| 305 | image = wholeImage; |
| 306 | minValue = image->GetStatistics()->GetScalarValueMin(0, selectedComponent); |
| 307 | maxValue = image->GetStatistics()->GetScalarValueMaxNoRecompute(); |
| 308 | min2ndValue = image->GetStatistics()->GetScalarValue2ndMinNoRecompute(); |
| 309 | max2ndValue = image->GetStatistics()->GetScalarValue2ndMaxNoRecompute(); |
| 310 | |
| 311 | if (minValue == maxValue) |
| 312 | { |
| 313 | // Same result, also look at data at other time steps if present... |
| 314 | auto numTimeSteps = image->GetTimeGeometry()->CountTimeSteps(); |
| 315 | |
| 316 | if (numTimeSteps > 1) |
| 317 | { |