<div dir="ltr">Hi all,<div><br></div><div style>I am trying to find connected components having the same pixel values, </div><div style>using ConnectedComponentFunctorImageFilter with the equal functor. </div><div style><br>
</div><div style>The Filter correctly identifies connected components, </div><div style>however background pixels are considered to be among of the </div><div style>connected components. I'd expect that pixels that have the value </div>
<div style>specified using SetBackgroundValue() are considered background pixels. </div><div style>Am I missing something, or is this a bug? </div><div style><br></div><div style>Incidentially, GetObjectCount() always returns 0.</div>
<div style><br></div><div style>Thanks,</div><div style>Bastian</div><div style><br></div><div style>Below is some example code that illustrates the issue (based on <a href="http://www.itk.org/Wiki/ITK/Examples/ImageProcessing/ConnectedComponentImageFilter">http://www.itk.org/Wiki/ITK/Examples/ImageProcessing/ConnectedComponentImageFilter</a>):</div>
<div style><br></div><div style><div>####################################################</div><div>ConnectedComponent.cxx:</div><div>###################################################</div></div><div style><br></div><div style>
<div>#include "itkImage.h"</div><div>#include "itkImageFileReader.h"</div><div>#include "itkConnectedComponentImageFilter.h"</div><div>#include <itkConnectedComponentFunctorImageFilter.h></div>
<div>#include "itkLabelToRGBImageFilter.h"</div><div>#include <itkLogicOpsFunctors.h></div><div>#include "itksys/SystemTools.hxx"</div><div>#include <sstream></div><div><br></div><div>#include "QuickView.h"</div>
<div><br></div><div>template <typename TImage></div><div>static void CreateImage(TImage* const image);</div><div><br></div><div>int main( int argc, char *argv[])</div><div>{</div><div> const unsigned int Dimension = 2;</div>
<div> typedef unsigned char PixelType;</div><div> typedef itk::RGBPixel<unsigned char> RGBPixelType;</div><div> typedef itk::Image<PixelType, Dimension> ImageType;</div><div>
typedef itk::Image<RGBPixelType, Dimension> RGBImageType;</div><div><br></div><div> ImageType::Pointer image;</div><div> if( argc < 2 )</div><div> {</div><div> image = ImageType::New();</div><div> CreateImage(image.GetPointer());</div>
<div> }</div><div> else</div><div> {</div><div> typedef itk::ImageFileReader<ImageType> ReaderType;</div><div> ReaderType::Pointer reader = ReaderType::New();</div><div> reader->SetFileName(argv[1]);</div>
<div> reader->Update();</div><div><br></div><div> image = reader->GetOutput();</div><div> }</div><div><br></div><div> typedef itk::Image< unsigned short, Dimension > OutputImageType;</div><div><br></div>
<div><br></div><div><span class="" style="white-space:pre">        </span>typedef itk::Functor::Equal< ImageType::PixelType, ImageType::PixelType, ImageType::PixelType > EqualFunctorType;</div><div><span class="" style="white-space:pre">        </span>typedef itk::ConnectedComponentFunctorImageFilter< ImageType, OutputImageType, EqualFunctorType, ImageType > ConnectedComponentFilterType;</div>
<div><span class="" style="white-space:pre">        </span>ConnectedComponentFilterType::Pointer connected = ConnectedComponentFilterType::New();</div><div><span class="" style="white-space:pre">        </span>connected->SetInput(image);</div>
<div><span class="" style="white-space:pre">        </span>connected->SetBackgroundValue(0);</div><div><span class="" style="white-space:pre">        </span>connected->Update();</div><div> </div><div><br></div><div><br></div><div>
std::cout << "Number of objects: " << connected->GetObjectCount() << std::endl;</div><div><br></div><div> typedef itk::LabelToRGBImageFilter<OutputImageType, RGBImageType> RGBFilterType;</div>
<div> RGBFilterType::Pointer rgbFilter = RGBFilterType::New();</div><div> rgbFilter->SetInput( connected->GetOutput() );</div><div><br></div><div> QuickView viewer;</div><div> viewer.AddImage(</div><div> image.GetPointer(),true,</div>
<div> argc > 1 ? itksys::SystemTools::GetFilenameName(argv[1]) : "Generated image");</div><div><br></div><div> std::stringstream desc;</div><div> desc << "Connected Components: "</div><div>
<< connected->GetObjectCount() << " objects";</div><div> viewer.AddRGBImage(</div><div> rgbFilter->GetOutput(),</div><div> true,</div><div> desc.str());</div><div><br></div><div>
viewer.Visualize();</div><div><br></div><div> return EXIT_SUCCESS;</div><div>}</div><div><br></div><div>template <typename TImage></div><div>void CreateImage(TImage* const image)</div><div>{</div><div> // Create an image with 2 connected components</div>
<div> typename TImage::IndexType start = {{0,0}};</div><div> start[0] = 0;</div><div> start[1] = 0;</div><div><br></div><div> typename TImage::SizeType size;</div><div> unsigned int NumRows = 200;</div><div> unsigned int NumCols = 300;</div>
<div> size[0] = NumRows;</div><div> size[1] = NumCols;</div><div><br></div><div> typename TImage::RegionType region(start, size);</div><div><br></div><div> image->SetRegions(region);</div><div> image->Allocate();</div>
<div><br></div><div> // Make a square</div><div> for(typename TImage::IndexValueType r = 0; r < 100; r++)</div><div> {</div><div> for(typename TImage::IndexValueType c = 30; c < 130; c++)</div><div> {</div>
<div> typename TImage::IndexType pixelIndex = {{r,c}};</div><div><br></div><div> image->SetPixel(pixelIndex, 255);</div><div> }</div><div> }</div><div><br></div><div> // Make another square</div><div>
for(typename TImage::IndexValueType r = 90; r < 200; r++)</div><div> {</div><div> for(typename TImage::IndexValueType c = 115; c < 160; c++)</div><div> {</div><div> typename TImage::IndexType pixelIndex = {{r,c}};</div>
<div><br></div><div> image->SetPixel(pixelIndex, 100);</div><div> }</div><div> }</div><div>}</div><div><br></div><div><br></div><div>####################################################</div><div style>CMakeLists.txt :</div>
<div style>###################################################</div><div><div>cmake_minimum_required(VERSION 2.8)</div><div><br></div><div>project(ConnectedComponentImageFilter)</div><div><br></div><div>find_package(ITK REQUIRED)</div>
<div>include(${ITK_USE_FILE})</div><div>if (ITKVtkGlue_LOADED)</div><div> find_package(VTK REQUIRED)</div><div> include(${VTK_USE_FILE})</div><div>else()</div><div> find_package(ItkVtkGlue REQUIRED)</div><div> include(${ItkVtkGlue_USE_FILE})</div>
<div> set(Glue ItkVtkGlue)</div><div>endif()</div><div><br></div><div>add_executable(ConnectedComponentImageFilter MACOSX_BUNDLE ConnectedComponent.cxx)</div><div>target_link_libraries(ConnectedComponentImageFilter</div>
<div> ${Glue} ${VTK_LIBRARIES} ${ITK_LIBRARIES})</div></div><div><br></div></div></div>