NifTK  16.4.1 - 0798f20
CMIC's Translational Medical Imaging Platform
itkIsImageBinary.h
Go to the documentation of this file.
1 /*=============================================================================
2 
3  NifTK: A software platform for medical image computing.
4 
5  Copyright (c) University College London (UCL). All rights reserved.
6 
7  This software is distributed WITHOUT ANY WARRANTY; without even
8  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9  PURPOSE.
10 
11  See LICENSE.txt in the top level directory for details.
12 
13 =============================================================================*/
14 
15 #ifndef __itkIsImageBinary_h
16 #define __itkIsImageBinary_h
17 
18 #include <itkImageRegionConstIterator.h>
19 
20 
21 namespace itk
22 {
23 
24 // --------------------------------------------------------------------------
26 // --------------------------------------------------------------------------
27 
28 template < typename TImage >
29 bool
30 IsImageBinary( typename TImage::Pointer image )
31 {
32  typename TImage::PixelType intensity1;
33  typename TImage::PixelType intensity2;
34 
35  return IsImageBinary< TImage >( image, intensity1, intensity2 );
36 }
37 
38 
39 // --------------------------------------------------------------------------
41 // --------------------------------------------------------------------------
42 
43 template < typename TImage >
44 bool
45 IsImageBinary( typename TImage::Pointer image,
46  typename TImage::PixelType &intensity1,
47  typename TImage::PixelType &intensity2 )
48 {
49  itk::ImageRegionIterator< TImage >
50  itImage( image, image->GetLargestPossibleRegion() );
51 
52  itImage.GoToBegin();
53 
54  // Get the first pixel's intensity
55 
56  if ( ! itImage.IsAtEnd() )
57  {
58  intensity1 = itImage.Get();
59  ++itImage;
60  }
61  else
62  {
63  return false;
64  }
65 
66 
67  // Get the next pixel with a different intensity
68 
69  while ( ( ! itImage.IsAtEnd() ) && ( itImage.Get() == intensity1 ) )
70  {
71  ++itImage;
72  }
73 
74  if ( ! itImage.IsAtEnd() )
75  {
76  intensity2 = itImage.Get();
77  ++itImage;
78  }
79  else
80  {
81  return false;
82  }
83 
84 
85  // Are any pixels not equal to these two intensities?
86 
87  for ( ;
88  ! itImage.IsAtEnd();
89  ++itImage )
90  {
91  if ( ( itImage.Get() != intensity1 ) &&
92  ( itImage.Get() != intensity2 ) )
93  {
94  return false;
95  }
96  }
97 
98  if ( intensity1 > intensity2 )
99  {
100  typename TImage::PixelType tmpIntensity;
101 
102  tmpIntensity = intensity2;
103  intensity2 = intensity1;
104  intensity1 = tmpIntensity;
105  }
106 
107  return true;
108 }
109 
110 
111 } // end namespace itk
112 
113 #endif /* __itkIsImageBinary_h */
GLenum GLsizei GLenum GLenum const GLvoid * image
Definition: glew.h:4052
Definition: niftkITKAffineResampleImage.cxx:74
bool IsImageBinary(typename TImage::Pointer image)
Return whether an image is binary or not.
Definition: itkIsImageBinary.h:30
float PixelType
Definition: niftkBreastDCEandADC.cxx:88