ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkConnectedComponentAlgorithm.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkConnectedComponentAlgorithm_h
00019 #define __itkConnectedComponentAlgorithm_h
00020 
00021 #include "itkImage.h"
00022 #include "itkShapedNeighborhoodIterator.h"
00023 
00024 namespace itk
00025 {
00026 template< class TIterator >
00027 TIterator *
00028 setConnectivity(TIterator *it, bool fullyConnected = false)
00029 {
00030   typename TIterator::OffsetType offset;
00031   it->ClearActiveList();
00032   if ( !fullyConnected )
00033     {
00034     // only activate the neighbors that are face connected
00035     // to the current pixel. do not include the center pixel
00036     offset.Fill(0);
00037     for ( unsigned int d = 0; d < TIterator::Dimension; ++d )
00038       {
00039       offset[d] = -1;
00040       it->ActivateOffset(offset);
00041       offset[d] = 1;
00042       it->ActivateOffset(offset);
00043       offset[d] = 0;
00044       }
00045     }
00046   else
00047     {
00048     // activate all neighbors that are face+edge+vertex
00049     // connected to the current pixel. do not include the center pixel
00050     unsigned int centerIndex = it->GetCenterNeighborhoodIndex();
00051     for ( unsigned int d = 0; d < centerIndex * 2 + 1; d++ )
00052       {
00053       offset = it->GetOffset(d);
00054       it->ActivateOffset(offset);
00055       }
00056     offset.Fill(0);
00057     it->DeactivateOffset(offset);
00058     }
00059   return it;
00060 }
00061 
00062 template< class TIterator >
00063 TIterator *
00064 setConnectivityPrevious(TIterator *it, bool fullyConnected = false)
00065 {
00066   // activate the "previous" neighbours
00067   typename TIterator::OffsetType offset;
00068   it->ClearActiveList();
00069   if ( !fullyConnected )
00070     {
00071     // only activate the neighbors that are face connected
00072     // to the current pixel. do not include the center pixel
00073     offset.Fill(0);
00074     for ( unsigned int d = 0; d < TIterator::Dimension; ++d )
00075       {
00076       offset[d] = -1;
00077       it->ActivateOffset(offset);
00078       offset[d] = 0;
00079       }
00080     }
00081   else
00082     {
00083     // activate all neighbors that are face+edge+vertex
00084     // connected to the current pixel. do not include the center pixel
00085     unsigned int centerIndex = it->GetCenterNeighborhoodIndex();
00086     for ( unsigned int d = 0; d < centerIndex; d++ )
00087       {
00088       offset = it->GetOffset(d);
00089       it->ActivateOffset(offset);
00090       }
00091     offset.Fill(0);
00092     it->DeactivateOffset(offset);
00093     }
00094   return it;
00095 }
00096 
00097 template< class TIterator >
00098 TIterator *
00099 setConnectivityLater(TIterator *it, bool fullyConnected = false)
00100 {
00101   // activate the "later" neighbours
00102   typename TIterator::OffsetType offset;
00103   it->ClearActiveList();
00104   if ( !fullyConnected )
00105     {
00106     // only activate the neighbors that are face connected
00107     // to the current pixel. do not include the center pixel
00108     offset.Fill(0);
00109     for ( unsigned int d = 0; d < TIterator::Dimension; ++d )
00110       {
00111       offset[d] = 1;
00112       it->ActivateOffset(offset);
00113       offset[d] = 0;
00114       }
00115     }
00116   else
00117     {
00118     // activate all neighbors that are face+edge+vertex
00119     // connected to the current pixel. do not include the center pixel
00120     unsigned int centerIndex = it->GetCenterNeighborhoodIndex();
00121     for ( unsigned int d = centerIndex + 1; d < 2 * centerIndex + 1; d++ )
00122       {
00123       offset = it->GetOffset(d);
00124       it->ActivateOffset(offset);
00125       }
00126     offset.Fill(0);
00127     it->DeactivateOffset(offset);
00128     }
00129   return it;
00130 }
00131 }
00132 
00133 #endif
00134