00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef MITKIMAGE_H_HEADER_INCLUDED_C1C2FCD2
00020 #define MITKIMAGE_H_HEADER_INCLUDED_C1C2FCD2
00021
00022 #include "mitkCommon.h"
00023 #include "mitkSlicedData.h"
00024 #include "mitkPixelType.h"
00025 #include "mitkBaseData.h"
00026 #include "mitkLevelWindow.h"
00027 #include "mitkPlaneGeometry.h"
00028
00029 #ifndef __itkHistogram_h
00030 #include <itkHistogram.h>
00031 #endif
00032
00033 class vtkImageData;
00034
00035 namespace mitk {
00036
00037 class SubImageSelector;
00038 class ImageDataItem;
00039 class ImageTimeSelector;
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 class MITK_CORE_EXPORT Image : public SlicedData
00063 {
00064 friend class SubImageSelector;
00065
00066 public:
00067 mitkClassMacro(Image, SlicedData);
00068
00069 itkNewMacro(Self);
00070
00072 typedef itk::SmartPointerForwardReference<ImageDataItem> ImageDataItemPointer;
00073
00074
00075
00076
00077
00078
00079
00080 enum ImportMemoryManagementType { CopyMemory, ManageMemory, ReferenceMemory, DontManageMemory = ReferenceMemory };
00081
00082
00083
00084
00085
00086 typedef std::vector<ImageDataItemPointer> ImageDataItemPointerArray;
00087
00088 typedef itk::Statistics::Histogram<double> HistogramType;
00089
00090 public:
00091
00092
00093 const mitk::PixelType& GetPixelType(int n = 0) const;
00094
00095
00096
00097
00098 unsigned int GetDimension() const;
00099
00100
00101
00102
00103
00104 unsigned int GetDimension(int i) const;
00105
00106
00107
00108
00109
00110 virtual void* GetData();
00111
00112
00113
00114
00115
00116 double GetPixelValueByIndex(const mitk::Index3D& position, unsigned int timestep = 0);
00117
00118
00119
00120
00121 double GetPixelValueByWorldCoordinate(const mitk::Point3D& position, unsigned int timestep = 0);
00122
00123
00124
00125 virtual vtkImageData* GetVtkImageData(int t = 0, int n = 0);
00126
00127
00128
00129
00130
00131
00132 virtual mitkIpPicDescriptor* GetPic();
00133
00134
00135
00136 virtual bool IsSliceSet(int s = 0, int t = 0, int n = 0) const;
00137
00138
00139
00140 virtual bool IsVolumeSet(int t = 0, int n = 0) const;
00141
00142
00143
00144 virtual bool IsChannelSet(int n = 0) const;
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 virtual bool SetSlice(const void *data, int s = 0, int t = 0, int n = 0);
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 virtual bool SetVolume(const void *data, int t = 0, int n = 0);
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 virtual bool SetChannel(const void *data, int n = 0);
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 virtual bool SetImportSlice(void *data, int s = 0, int t = 0, int n = 0, ImportMemoryManagementType importMemoryManagement = CopyMemory );
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 virtual bool SetImportVolume(void *data, int t = 0, int n = 0, ImportMemoryManagementType importMemoryManagement = CopyMemory );
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 virtual bool SetImportChannel(void *data, int n = 0, ImportMemoryManagementType importMemoryManagement = CopyMemory );
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224 virtual bool SetPicSlice(const mitkIpPicDescriptor *pic, int s = 0, int t = 0, int n = 0, ImportMemoryManagementType importMemoryManagement = CopyMemory );
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 virtual bool SetPicVolume(const mitkIpPicDescriptor *pic, int t = 0, int n = 0, ImportMemoryManagementType importMemoryManagement = CopyMemory );
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 virtual bool SetPicChannel(const mitkIpPicDescriptor *pic, int n = 0, ImportMemoryManagementType importMemoryManagement = CopyMemory );
00247
00248
00249
00250
00251 virtual void Initialize(const mitk::PixelType& type, unsigned int dimension, unsigned int *dimensions, unsigned int channels = 1);
00252
00253
00254
00255
00256
00257 virtual void Initialize(const mitk::PixelType& type, const mitk::Geometry3D& geometry, unsigned int channels = 1, int tDim=-1);
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 virtual void Initialize(const mitk::PixelType& type, int sDim, const mitk::Geometry2D& geometry2d, bool flipped = false, unsigned int channels = 1, int tDim=-1);
00268
00269
00270
00271
00272
00273
00274 virtual void Initialize(const mitk::Image* image);
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 virtual void Initialize(const mitkIpPicDescriptor* pic, int channels = 1, int tDim = -1, int sDim = -1);
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 virtual void Initialize(vtkImageData* vtkimagedata, int channels = 1, int tDim = -1, int sDim = -1);
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306 template <typename itkImageType> void InitializeByItk(const itkImageType* itkimage, int channels = 1, int tDim = -1, int sDim=-1)
00307 {
00308 if(itkimage==NULL) return;
00309
00310 MITK_DEBUG << "Initializing MITK image from ITK image.";
00311
00312 m_Dimension=itkimage->GetImageDimension();
00313 unsigned int i, *tmpDimensions=new unsigned int[m_Dimension>4?m_Dimension:4];
00314 for(i=0;i<m_Dimension;++i)
00315 tmpDimensions[i]=itkimage->GetLargestPossibleRegion().GetSize().GetSize()[i];
00316 if(m_Dimension<4)
00317 {
00318 unsigned int *p;
00319 for(i=0,p=tmpDimensions+m_Dimension;i<4-m_Dimension;++i, ++p)
00320 *p=1;
00321 }
00322
00323
00324 if((m_Dimension>2) && (sDim>=0))
00325 tmpDimensions[2]=sDim;
00326
00327 if((m_Dimension>3) && (tDim>=0))
00328 tmpDimensions[3]=tDim;
00329
00330
00331 Initialize(mitk::PixelType(typeid(typename itkImageType::PixelType)),
00332 m_Dimension,
00333 tmpDimensions,
00334 channels);
00335 const typename itkImageType::SpacingType & itkspacing = itkimage->GetSpacing();
00336
00337 MITK_DEBUG << "ITK spacing " << itkspacing;
00338
00339 Vector3D spacing;
00340 FillVector3D(spacing, itkspacing[0], 1.0, 1.0);
00341 if(m_Dimension >= 2)
00342 spacing[1]=itkspacing[1];
00343 if(m_Dimension >= 3)
00344 spacing[2]=itkspacing[2];
00345
00346
00347 Point3D origin;
00348 const typename itkImageType::PointType & itkorigin = itkimage->GetOrigin();
00349 MITK_DEBUG << "ITK origin " << itkorigin;
00350 FillVector3D(origin, itkorigin[0], 0.0, 0.0);
00351 if(m_Dimension>=2)
00352 origin[1]=itkorigin[1];
00353 if(m_Dimension>=3)
00354 origin[2]=itkorigin[2];
00355
00356
00357 const typename itkImageType::DirectionType & itkdirection = itkimage->GetDirection();
00358 MITK_DEBUG << "ITK direction " << itkdirection;
00359 mitk::Matrix3D matrix;
00360 matrix.SetIdentity();
00361 unsigned int j, itkDimMax3 = (m_Dimension >= 3? 3 : m_Dimension);
00362
00363 bool itkdirectionOk = true;
00364 mitk::ScalarType columnSum;
00365 for( j=0; j < itkDimMax3; ++j )
00366 {
00367 columnSum = 0.0;
00368 for ( i=0; i < itkDimMax3; ++i)
00369 {
00370 columnSum += fabs(itkdirection[i][j]);
00371 }
00372 if(columnSum < mitk::eps)
00373 {
00374 itkdirectionOk = false;
00375 }
00376 if ( (spacing[j] < - mitk::eps)
00377 && (j==2) && (m_Dimensions[2] == 1) )
00378 {
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389 MITK_WARN << "Illegal value of itk::Image::GetSpacing()[" << j <<"]=" << spacing[j] << ". Using inverted value " << -spacing[j];
00390 spacing[j] = -spacing[j];
00391 }
00392 else if (spacing[j] < mitk::eps)
00393 {
00394 MITK_ERROR << "Illegal value of itk::Image::GetSpacing()[" << j <<"]=" << spacing[j] << ". Using 1.0 instead.";
00395 spacing[j] = 1.0;
00396 }
00397 }
00398 if(itkdirectionOk == false)
00399 {
00400 MITK_ERROR << "Illegal matrix returned by itk::Image::GetDirection():" << itkdirection << " Using identity instead.";
00401 for ( i=0; i < itkDimMax3; ++i)
00402 for( j=0; j < itkDimMax3; ++j )
00403 if ( i == j )
00404 matrix[i][j] = spacing[j];
00405 else
00406 matrix[i][j] = 0.0;
00407 }
00408 else
00409 {
00410 for ( i=0; i < itkDimMax3; ++i)
00411 for( j=0; j < itkDimMax3; ++j )
00412 matrix[i][j] = itkdirection[i][j]*spacing[j];
00413 }
00414
00415
00416 PlaneGeometry* planeGeometry = static_cast<PlaneGeometry*>(GetSlicedGeometry(0)->GetGeometry2D(0));
00417 planeGeometry->SetOrigin(origin);
00418 planeGeometry->GetIndexToWorldTransform()->SetMatrix(matrix);
00419
00420
00421 SlicedGeometry3D* slicedGeometry = GetSlicedGeometry(0);
00422 slicedGeometry->InitializeEvenlySpaced(planeGeometry, m_Dimensions[2]);
00423 slicedGeometry->SetSpacing(spacing);
00424
00425
00426 GetTimeSlicedGeometry()->InitializeEvenlyTimed(slicedGeometry, m_Dimensions[3]);
00427
00428
00429 delete [] tmpDimensions;
00430
00431 this->Initialize();
00432 };
00433
00434
00435
00436
00437 virtual bool IsValidSlice(int s = 0, int t = 0, int n = 0) const;
00438
00439
00440
00441
00442 virtual bool IsValidVolume(int t = 0, int n = 0) const;
00443
00444
00445
00446
00447 virtual bool IsValidChannel(int n = 0) const;
00448
00449
00450
00451
00452
00453 bool IsRotated() const;
00454
00455
00456
00457
00458
00459 unsigned int* GetDimensions() const;
00460
00461 virtual void SetGeometry(Geometry3D* aGeometry3D);
00462
00463 virtual const HistogramType* GetScalarHistogram(int t=0) const;
00464
00465
00466
00467 virtual ScalarType GetScalarValueMin(int t=0) const;
00468
00469
00470
00471 virtual ScalarType GetScalarValueMax(int t=0) const;
00472
00473
00474
00475 virtual ScalarType GetScalarValue2ndMin(int t=0) const;
00476
00477
00478
00479 virtual mitk::ScalarType GetScalarValueMinNoRecompute( unsigned int t = 0 ) const
00480 {
00481 if ( t < m_ScalarMin.size() )
00482 return m_ScalarMin[t];
00483 else return itk::NumericTraits<ScalarType>::max();
00484 }
00485
00486
00487
00488 virtual mitk::ScalarType GetScalarValue2ndMinNoRecompute( unsigned int t = 0 ) const
00489 {
00490 if ( t < m_Scalar2ndMin.size() )
00491 return m_Scalar2ndMin[t];
00492 else return itk::NumericTraits<ScalarType>::max();
00493 }
00494
00495
00496
00497 virtual ScalarType GetScalarValue2ndMax(int t=0) const;
00498
00499
00500
00501 virtual mitk::ScalarType GetScalarValueMaxNoRecompute( unsigned int t = 0 ) const
00502 {
00503 if ( t < m_ScalarMax.size() )
00504 return m_ScalarMax[t];
00505 else return itk::NumericTraits<ScalarType>::NonpositiveMin();
00506 }
00507
00508
00509
00510 virtual mitk::ScalarType GetScalarValue2ndMaxNoRecompute( unsigned int t = 0 ) const
00511 {
00512 if ( t < m_Scalar2ndMax.size() )
00513 return m_Scalar2ndMax[t];
00514 else return itk::NumericTraits<ScalarType>::NonpositiveMin();
00515 }
00516
00517
00518
00519 mitk::ScalarType GetCountOfMinValuedVoxels(int t = 0) const;
00520
00521
00522
00523 mitk::ScalarType GetCountOfMaxValuedVoxels(int t = 0) const;
00524
00525
00526
00527 virtual unsigned int GetCountOfMaxValuedVoxelsNoRecompute( unsigned int t = 0 ) const
00528 {
00529 if ( t < m_CountOfMaxValuedVoxels.size() )
00530 return m_CountOfMaxValuedVoxels[t];
00531 else return 0;
00532 }
00533
00534
00535
00536 virtual unsigned int GetCountOfMinValuedVoxelsNoRecompute( unsigned int t = 0 ) const
00537 {
00538 if ( t < m_CountOfMinValuedVoxels.size() )
00539 return m_CountOfMinValuedVoxels[t];
00540 else return 0;
00541 }
00542
00543
00544
00545 virtual ImageDataItemPointer GetSliceData(int s = 0, int t = 0, int n = 0, void *data = NULL, ImportMemoryManagementType importMemoryManagement = CopyMemory);
00546
00547
00548
00549 virtual ImageDataItemPointer GetVolumeData(int t = 0, int n = 0, void *data = NULL, ImportMemoryManagementType importMemoryManagement = CopyMemory);
00550
00551
00552
00553 virtual ImageDataItemPointer GetChannelData(int n = 0, void *data = NULL, ImportMemoryManagementType importMemoryManagement = CopyMemory);
00554
00555 template < typename ItkImageType >
00556 friend void _ComputeExtremaInItkImage(ItkImageType* itkImage, mitk::Image * mitkImage, int t);
00557
00558 protected:
00559
00560 int GetSliceIndex(int s = 0, int t = 0, int n = 0) const;
00561
00562 int GetVolumeIndex(int t = 0, int n = 0) const;
00563
00564 void ComputeOffsetTable();
00565
00566 virtual void Expand( int timeSteps ) const;
00567
00568 virtual bool IsValidTimeStep(int t) const;
00569
00570 virtual void ResetImageStatistics() const;
00571
00572 virtual void ComputeImageStatistics(int t=0) const;
00573
00574 virtual ImageDataItemPointer AllocateSliceData(int s = 0, int t = 0, int n = 0, void *data = NULL, ImportMemoryManagementType importMemoryManagement = CopyMemory);
00575
00576 virtual ImageDataItemPointer AllocateVolumeData(int t = 0, int n = 0, void *data = NULL, ImportMemoryManagementType importMemoryManagement = CopyMemory);
00577
00578 virtual ImageDataItemPointer AllocateChannelData(int n = 0, void *data = NULL, ImportMemoryManagementType importMemoryManagement = CopyMemory);
00579
00580 Image();
00581
00582 virtual ~Image();
00583
00584 virtual void Clear();
00585
00586
00587 virtual void Initialize();
00588
00589 ImageTimeSelector* GetTimeSelector() const;
00590
00591 virtual void PrintSelf(std::ostream& os, itk::Indent indent) const;
00592
00593 mutable ImageDataItemPointerArray m_Channels;
00594 mutable ImageDataItemPointerArray m_Volumes;
00595 mutable ImageDataItemPointerArray m_Slices;
00596
00597 unsigned int m_Dimension;
00598 unsigned int *m_Dimensions;
00599 size_t *m_OffsetTable;
00600 ImageDataItemPointer m_CompleteData;
00601 PixelType m_PixelType;
00602
00603 mutable itk::Object::Pointer m_HistogramGeneratorObject;
00604
00605 mutable itk::Object::Pointer m_TimeSelectorForExtremaObject;
00606 mutable std::vector<unsigned int> m_CountOfMinValuedVoxels;
00607 mutable std::vector<unsigned int> m_CountOfMaxValuedVoxels;
00608 mutable std::vector<ScalarType> m_ScalarMin;
00609 mutable std::vector<ScalarType> m_ScalarMax;
00610 mutable std::vector<ScalarType> m_Scalar2ndMin;
00611 mutable std::vector<ScalarType> m_Scalar2ndMax;
00612
00613 itk::TimeStamp m_LastRecomputeTimeStamp;
00614
00615 };
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625 template <typename ItkOutputImageType>
00626 void CastToMitkImage(const itk::SmartPointer<ItkOutputImageType>& itkimage, itk::SmartPointer<mitk::Image>& mitkoutputimage)
00627 {
00628 if(mitkoutputimage.IsNull())
00629 {
00630 mitkoutputimage = mitk::Image::New();
00631 }
00632 mitkoutputimage->InitializeByItk(itkimage.GetPointer());
00633 mitkoutputimage->SetChannel(itkimage->GetBufferPointer());
00634 }
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644 template <typename ItkOutputImageType>
00645 void CastToMitkImage(const ItkOutputImageType* itkimage, itk::SmartPointer<mitk::Image>& mitkoutputimage)
00646 {
00647 if(mitkoutputimage.IsNull())
00648 {
00649 mitkoutputimage = mitk::Image::New();
00650 }
00651 mitkoutputimage->InitializeByItk(itkimage);
00652 mitkoutputimage->SetChannel(itkimage->GetBufferPointer());
00653 }
00654 }
00655
00656 #endif