1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: VectorImageWrapper.txx,v $
Language: C++
Date: $Date: 2007/06/06 22:27:21 $
Version: $Revision: 1.1 $
Copyright (c) 2003 Insight Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "VectorImageWrapper.h"
#include "RLEImageRegionIterator.h"
#include "itkImageSliceConstIteratorWithIndex.h"
#include "itkNumericTraits.h"
#include "itkRegionOfInterestImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkIdentityTransform.h"
#include "AdaptiveSlicingPipeline.h"
#include "SNAPSegmentationROISettings.h"
#include "itkCommand.h"
#include "ImageWrapperTraits.h"
#include "itkVectorImageToImageAdaptor.h"
#include "itkMinimumMaximumImageFilter.h"
#include "ThreadedHistogramImageFilter.h"
#include "ScalarImageHistogram.h"
#include "Rebroadcaster.h"
#include <iostream>
#include "itkVectorGradientAnisotropicDiffusionImageFilter.h"
template <class TTraits, class TBase>
VectorImageWrapper<TTraits,TBase>
::VectorImageWrapper()
{
// Initialize the flattened image
m_FlatImage = NULL;
// Initialize the filters
m_MinMaxFilter = MinMaxFilterType::New();
m_HistogramFilter = HistogramFilterType::New();
}
template <class TTraits, class TBase>
VectorImageWrapper<TTraits,TBase>
::~VectorImageWrapper()
{
}
template <class TTraits, class TBase>
typename VectorImageWrapper<TTraits,TBase>::ImagePointer
VectorImageWrapper<TTraits,TBase>
::DeepCopyRegion(const SNAPSegmentationROISettings &roi,
itk::Command *progressCommand) const
{
return Superclass::DeepCopyRegion(roi, progressCommand);
}
template<class TTraits, class TBase>
void
VectorImageWrapper<TTraits, TBase>
::GetRunLengthIntensityStatistics(
const itk::ImageRegion<3> ®ion,
const itk::Index<3> &startIdx, long runlength,
double *out_sum, double *out_sumsq) const
{
ConstIterator it(this->m_Image, region);
it.SetIndex(startIdx);
size_t nc = this->GetNumberOfComponents();
// Perform the integration
for(long q = 0; q < runlength; q++, ++it)
{
PixelType p = it.Get();
for(size_t c = 0; c < nc; c++)
{
double v = (double) p[c];
out_sum[c] += v;
out_sumsq[c] += v * v;
}
}
}
template<class TTraits, class TBase>
void
VectorImageWrapper<TTraits,TBase>
::GetVoxelUnderCursorDisplayedValueAndAppearance(
vnl_vector<double> &out_value, DisplayPixelType &out_appearance)
{
// Get the numerical value
MultiChannelDisplayMode mode = this->m_DisplayMapping->GetDisplayMode();
if(mode.UseRGB || mode.RenderAsGrid)
{
// Look up the actual intensity of the voxel from the slicer
PixelType pixel_value = this->m_Slicer[0]->LookupIntensityAtSliceIndex(this->m_ReferenceSpace);
// Set the output value
out_value.set_size(this->GetNumberOfComponents());
for(int i = 0; i < this->GetNumberOfComponents(); i++)
out_value[i] = this->m_NativeMapping(pixel_value[i]);
// Use the display mapping to map to display pixel
out_appearance = this->m_DisplayMapping->MapPixel(pixel_value);
}
else
{
// Just delegate to the scalar wrapper
ScalarImageWrapperBase *siw =
this->GetScalarRepresentation(mode.SelectedScalarRep, mode.SelectedComponent);
siw->GetVoxelUnderCursorDisplayedValueAndAppearance(out_value, out_appearance);
}
}
template <class TTraits, class TBase>
void
VectorImageWrapper<TTraits,TBase>
::SetNativeMapping(NativeIntensityMapping mapping)
{
Superclass::SetNativeMapping(mapping);
// Propagate the mapping to the histogram
m_HistogramFilter->SetIntensityTransform(mapping.GetScale(), mapping.GetShift());
// Propagate to owned scalar wrappers
for(ScalarRepIterator it = m_ScalarReps.begin(); it != m_ScalarReps.end(); ++it)
{
ScalarRepIndex idx = it->first;
if(idx.first == SCALAR_REP_COMPONENT)
{
// Cast the wrapper the right type
ComponentWrapperType *cw =
dynamic_cast<ComponentWrapperType *>(it->second.GetPointer());
// Pass the native to the component wrapper
cw->SetNativeMapping(mapping);
}
// These are the derived wrappers. They use the identity mapping, but they
// need to know what the source native mapping is.
else if(idx.first == SCALAR_REP_MAGNITUDE)
{
SetNativeMappingInDerivedWrapper<MagnitudeFunctor>(it->second, mapping);
}
else if(idx.first == SCALAR_REP_MAX)
{
SetNativeMappingInDerivedWrapper<MaxFunctor>(it->second, mapping);
}
else if(idx.first == SCALAR_REP_AVERAGE)
{
SetNativeMappingInDerivedWrapper<MeanFunctor>(it->second, mapping);
}
}
}
template <class TTraits, class TBase>
template <class TFunctor>
void
VectorImageWrapper<TTraits,TBase>
::SetNativeMappingInDerivedWrapper(
ScalarImageWrapperBase *w,
NativeIntensityMapping &mapping)
{
typedef VectorDerivedQuantityImageWrapperTraits<TFunctor> WrapperTraits;
typedef typename WrapperTraits::WrapperType DerivedWrapper;
typedef typename DerivedWrapper::ImageType AdaptorType;
typedef typename AdaptorType::AccessorType PixelAccessor;
// Cast to the right type
DerivedWrapper *dw = dynamic_cast<DerivedWrapper *>(w);
// Get the accessor
PixelAccessor &accessor = dw->GetImage()->GetPixelAccessor();
accessor.SetSourceNativeMapping(mapping.GetScale(), mapping.GetShift());
}
template <class TTraits, class TBase>
template <class TFunctor>
SmartPtr<ScalarImageWrapperBase>
VectorImageWrapper<TTraits,TBase>
::CreateDerivedWrapper(ImageType *image, ImageBaseType *refSpace, ITKTransformType *transform)
{
typedef VectorDerivedQuantityImageWrapperTraits<TFunctor> WrapperTraits;
typedef typename WrapperTraits::WrapperType DerivedWrapper;
typedef typename DerivedWrapper::ImageType AdaptorType;
SmartPtr<AdaptorType> adaptor = AdaptorType::New();
adaptor->SetImage(image);
SmartPtr<DerivedWrapper> wrapper = DerivedWrapper::New();
wrapper->InitializeToWrapper(this, adaptor, refSpace, transform);
// Assign a parent wrapper to the derived wrapper
wrapper->SetParentWrapper(this);
// Pass the display geometry to the component wrapper
for(int k = 0; k < 3; k++)
wrapper->SetDisplayViewportGeometry(k, this->GetDisplayViewportGeometry(k));
SmartPtr<ScalarImageWrapperBase> ptrout = wrapper.GetPointer();
// When creating derived wrappers, we need to rebroadcast the events from
// that wrapper as our own events
Rebroadcaster::RebroadcastAsSourceEvent(wrapper, WrapperChangeEvent(), this);
return ptrout;
}
template <class TTraits, class TBase>
void
VectorImageWrapper<TTraits,TBase>
::UpdateImagePointer(ImageType *newImage, ImageBaseType *referenceSpace, ITKTransformType *transform)
{
// Create the component wrappers before calling the parent's method.
int nc = newImage->GetNumberOfComponentsPerPixel();
// The first component image will serve as the reference for the other
// component images
ComponentWrapperType *cref = NULL;
for(int i = 0; i < nc; i++)
{
// Create a component image
typedef itk::VectorImageToImageAdaptor<InternalPixelType,3> ComponentImage;
SmartPtr<ComponentImage> comp = ComponentImage::New();
comp->SetImage(newImage);
comp->SetExtractComponentIndex(i);
// Create a wrapper for this image and assign the component image
SmartPtr<ComponentWrapperType> cw = ComponentWrapperType::New();
// Pass the display geometry to the component wrapper
for(int k = 0; k < 3; k++)
cw->SetDisplayViewportGeometry(k, this->GetDisplayViewportGeometry(k));
// Initialize referencing the current wrapper
cw->InitializeToWrapper(this, comp, referenceSpace, transform);
// Assign a parent wrapper to the derived wrapper
cw->SetParentWrapper(this);
// Store the wrapper
m_ScalarReps[std::make_pair(
SCALAR_REP_COMPONENT, i)] = cw.GetPointer();
// Rebroadcast the events from that wrapper
Rebroadcaster::RebroadcastAsSourceEvent(cw, WrapperChangeEvent(), this);
}
m_ScalarReps[std::make_pair(SCALAR_REP_MAGNITUDE, 0)]
= this->template CreateDerivedWrapper<MagnitudeFunctor>(newImage, referenceSpace, transform);
m_ScalarReps[std::make_pair(SCALAR_REP_MAX, 0)]
= this->template CreateDerivedWrapper<MaxFunctor>(newImage, referenceSpace, transform);
m_ScalarReps[std::make_pair(SCALAR_REP_AVERAGE, 0)]
= this->template CreateDerivedWrapper<MeanFunctor>(newImage, referenceSpace, transform);
// Create a flat representation of the image
m_FlatImage = FlatImageType::New();
typename FlatImageType::SizeType flatsize;
flatsize[0] = newImage->GetPixelContainer()->Size();
m_FlatImage->SetRegions(flatsize);
m_FlatImage->SetPixelContainer(newImage->GetPixelContainer());
// Connect the flat image to the min/max computer
m_MinMaxFilter->SetInput(m_FlatImage);
// Hook up the histogram computer to the flat image and min/max filter
m_HistogramFilter->SetInput(m_FlatImage);
m_HistogramFilter->SetRangeInputs(m_MinMaxFilter->GetMinimumOutput(),
m_MinMaxFilter->GetMaximumOutput());
// Set the number of bins (TODO - how to do this smartly?)
m_HistogramFilter->SetNumberOfBins(DEFAULT_HISTOGRAM_BINS);
/*
// Make sure intensity curve is shared by the components
// TODO: what should be shared is the entire pipeline. That requires us to
// compute the min/max of the vector components and return them as an
// itk::DataObject.
if(i == 0)
{
cref = cw;
}
else
{
typedef typename ComponentWrapperType::DisplayMapping ComponentDM;
SmartPtr<ComponentDM> cdm = cw->GetDisplayMapping();
SmartPtr<ComponentDM> cdmref = cref->GetDisplayMapping();
cdm->SetIntensityCurve(cdmref->GetIntensityCurve());
cdm->SetColorMap(cdmref->GetColorMap());
}
// Store the component
m_ScalarReps[std::make_pair(SCALAR_REP_COMPONENT, i)]
= cw.GetPointer();
}
// Initialize the computed derived wrappers
ColorMap *cm = cref->GetDisplayMapping()->GetColorMap(); */
// Call the parent's method = this will initialize the display mapping
Superclass::UpdateImagePointer(newImage, referenceSpace, transform);
}
template<class TTraits, class TBase>
void
VectorImageWrapper<TTraits,TBase>
::SetITKTransform(ImageBaseType *referenceSpace, ITKTransformType *transform)
{
Superclass::SetITKTransform(referenceSpace, transform);
for(ScalarRepIterator it = m_ScalarReps.begin(); it != m_ScalarReps.end(); ++it)
{
it->second->SetITKTransform(referenceSpace, transform);
}
}
template <class TTraits, class TBase>
inline ScalarImageWrapperBase *
VectorImageWrapper<TTraits,TBase>
::GetDefaultScalarRepresentation()
{
ScalarImageWrapperBase *rep =
this->m_DisplayMapping->GetScalarRepresentation();
if(rep)
return rep;
// TODO: This is somewhat arbitrary! Maybe it should be something the user
// can change under settings, i.e., "Default scalar representation for RGB images".
return this->GetScalarRepresentation(SCALAR_REP_MAX);
}
template<class TTraits, class TBase>
const ScalarImageHistogram *
VectorImageWrapper<TTraits,TBase>
::GetHistogram(size_t nBins)
{
// If the user passes in a non-zero number of bins, we pass that as a
// parameter to the filter
if(nBins > 0)
m_HistogramFilter->SetNumberOfBins(nBins);
m_HistogramFilter->Update();
return m_HistogramFilter->GetHistogramOutput();
}
template <class TTraits, class TBase>
inline ScalarImageWrapperBase *
VectorImageWrapper<TTraits,TBase>
::GetScalarRepresentation(
ScalarRepresentation type,
int index)
{
return m_ScalarReps[std::make_pair(type, index)];
}
template <class TTraits, class TBase>
inline ScalarImageWrapperBase *
VectorImageWrapper<TTraits,TBase>
::GetScalarRepresentation(const ScalarRepresentationIterator &it)
{
assert(!it.IsAtEnd());
return this->GetScalarRepresentation(it.GetCurrent(), it.GetIndex());
}
template <class TTraits, class TBase>
bool
VectorImageWrapper<TTraits,TBase>
::FindScalarRepresentation(
ImageWrapperBase *scalar_rep, ScalarRepresentation &type, int &index) const
{
for(ScalarRepConstIterator it = m_ScalarReps.begin(); it != m_ScalarReps.end(); ++it)
{
if(it->second.GetPointer() == scalar_rep)
{
type = it->first.first;
index = it->first.second;
return true;
}
}
return false;
}
template <class TTraits, class TBase>
typename VectorImageWrapper<TTraits,TBase>::ComponentWrapperType *
VectorImageWrapper<TTraits,TBase>
::GetComponentWrapper(unsigned int index)
{
ScalarRepIndex repidx(SCALAR_REP_COMPONENT, index);
return static_cast<ComponentWrapperType *>(m_ScalarReps[repidx].GetPointer());
}
template <class TTraits, class TBase>
void
VectorImageWrapper<TTraits,TBase>
::SetSliceIndex(const Vector3ui &cursor)
{
Superclass::SetSliceIndex(cursor);
// Propagate to owned scalar wrappers
for(ScalarRepIterator it = m_ScalarReps.begin(); it != m_ScalarReps.end(); ++it)
{
it->second->SetSliceIndex(cursor);
}
}
template <class TTraits, class TBase>
void
VectorImageWrapper<TTraits,TBase>
::SetDisplayViewportGeometry(
unsigned int index,
ImageBaseType *viewport_image)
{
Superclass::SetDisplayViewportGeometry(index, viewport_image);
// Propagate to owned scalar wrappers
for(ScalarRepIterator it = m_ScalarReps.begin(); it != m_ScalarReps.end(); ++it)
{
it->second->SetDisplayViewportGeometry(index, viewport_image);
}
}
template <class TTraits, class TBase>
void
VectorImageWrapper<TTraits,TBase>
::SetDisplayGeometry(const IRISDisplayGeometry &dispGeom)
{
Superclass::SetDisplayGeometry(dispGeom);
for(ScalarRepIterator it = m_ScalarReps.begin(); it != m_ScalarReps.end(); ++it)
it->second->SetDisplayGeometry(dispGeom);
}
template <class TTraits, class TBase>
void
VectorImageWrapper<TTraits,TBase>
::SetDirectionMatrix(const vnl_matrix<double> &direction)
{
Superclass::SetDirectionMatrix(direction);
for(ScalarRepIterator it = m_ScalarReps.begin(); it != m_ScalarReps.end(); ++it)
it->second->SetDirectionMatrix(direction);
}
template <class TTraits, class TBase>
void
VectorImageWrapper<TTraits,TBase>
::CopyImageCoordinateTransform(const ImageWrapperBase *source)
{
Superclass::CopyImageCoordinateTransform(source);
for(ScalarRepIterator it = m_ScalarReps.begin(); it != m_ScalarReps.end(); ++it)
it->second->CopyImageCoordinateTransform(source);
}
template<class TTraits, class TBase>
typename VectorImageWrapper<TTraits,TBase>::ComponentTypeObject *
VectorImageWrapper<TTraits,TBase>
::GetImageMinObject() const
{
return m_MinMaxFilter->GetMinimumOutput();
}
template<class TTraits, class TBase>
typename VectorImageWrapper<TTraits,TBase>::ComponentTypeObject *
VectorImageWrapper<TTraits,TBase>
::GetImageMaxObject() const
{
return m_MinMaxFilter->GetMaximumOutput();
}
/*
template <class TImage, class TBase>
inline double
VectorImageWrapper<TImage,TBase>
::GetVoxelAsDouble(const itk::Index<3> &idx) const
{
// By default, return the first component
return (double) this->GetVoxel(idx)[0];
}
template <class TImage, class TBase>
inline double
VectorImageWrapper<TImage,TBase>
::GetVoxelAsDouble(const Vector3ui &x) const
{
// By default, return the first component
return (double) this->GetVoxel(x)[0];
}
*/
template class VectorImageWrapper<AnatomicImageWrapperTraits<GreyType> >;
|