Hi Gaetan,<br><br>Thank you very much !<br><br>The example code you referred me to showed me how to perform the merge.<br><br>I just had to add the lines:<br><br>//added code per itkMergeLabelMapFilterTest1.cxx<br><br> convertermerge->SetInput( labelMap );<br>
<br> convertermerge->SetInput( 1, labelMap2 );<br><br> //end of added code<br><br>Thanks again,<br>john<br><br><div class="gmail_quote">On Thu, Dec 17, 2009 at 1:12 PM, John Drozd <span dir="ltr"><<a href="mailto:john.drozd@gmail.com">john.drozd@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hi Gaetan,<br><br>My particular brain subject had the left and right brain ventricle as separate regions, so I inputed a seed for the left ventricle and a seed for the right ventricle, and created a separate label map for each ventricle. (I could not figure out how to create a label map with two labelled segmentations, so I am trying to merge these two label maps into a combined label map.)<br>
<br>Below is my code and thank you:<br><br>/*<br>to run type:<br><br>./ConnectedThresholdImagechangedtoFuzzyConnectednessImageFilter "/trumpet/downloads/ReadAtlasDicomSeriesAndReadSubjectDicomSeries_Plugin/ReadAtlasDicomSeriesAndReadSubjectDicomSeries/ReadAtlasDicomSeriesAndReadSubjectDicomSeries3DSlicerDec22009/normal002_S_0295.dcm" outsubjectnormal4final.dcm outfuzzymapnormal.mhd 100 151 95 86 152 76 2.5 10<br>
<br>*/<br><br>#if defined(_MSC_VER)<br>#pragma warning ( disable : 4786 )<br>#endif<br><br>#ifdef __BORLANDC__<br>#define ITK_LEAN_AND_MEAN<br>#endif<br><br><br>#include "itkConnectedThresholdImageFilter.h"<br>
<br>
#include "itkImage.h"<br>#include "itkCastImageFilter.h"<br><br>#include "itkCurvatureFlowImageFilter.h"<br><br>#include "itkImageFileReader.h"<br>#include "itkImageFileWriter.h"<br>
<br>#include "itkGDCMImageIO.h"<br><br>#include "itkVersion.h"<br><br>#include "itkOrientedImage.h"<br>#include "itkMinimumMaximumImageFilter.h"<br><br>#include "itkGDCMImageIO.h"<br>
#include "itkGDCMSeriesFileNames.h"<br>#include "itkNumericSeriesFileNames.h"<br><br>#include "itkImageSeriesReader.h"<br>#include "itkImageSeriesWriter.h"<br><br>#include "itkResampleImageFilter.h"<br>
#include "itkShiftScaleImageFilter.h"<br><br>#include "itkIdentityTransform.h"<br>#include "itkLinearInterpolateImageFunction.h"<br><br>#include <itksys/SystemTools.hxx><br><br>#include "gdcm/src/gdcmFile.h"<br>
#include "gdcm/src/gdcmUtil.h"<br><br>#include <string><br><br>//added per attribute_values.cxx<br>#include "itkImageFileReader.h"<br><br>#include "itkShapeLabelObject.h"<br><br>#include "itkLabelMap.h"<br>
<br>#include "itkBinaryImageToShapeLabelMapFilter.h"<br>//end of added code<br><br>//added per label.cxx<br>#include "itkLabelObject.h"<br>#include "itkLabelMap.h"<br>#include "itkBinaryImageToLabelMapFilter.h"<br>
#include "itkLabelMapToLabelImageFilter.h"<br>//end of added code<br><br>#include "itkSimpleFilterWatcher.h"<br><br>//added per FuzzyConnectednessImageFilter.cxx<br>#include "itkSimpleFuzzyConnectednessScalarImageFilter.h"<br>
#include "itkConfidenceConnectedImageFilter.h"<br>//end of added code<br><br>#include "itkMergeLabelMapFilter.h"<br><br>int main( int argc, char *argv[])<br>{<br> if( argc < 7 )<br> {<br> std::cerr << "Missing Parameters " << std::endl;<br>
std::cerr << "Usage: " << argv[0];<br> <br> std::cerr << " inputImage outputImage outputAffinityMap seedX seedY seedZ seedX seedY seedZ multiplier tolerancedivisor" << std::endl;<br>
<br><br> return 1;<br> }<br><br> typedef unsigned char BinaryPixelType;<br><br> const unsigned int Dimension = 3;<br><br> typedef itk::Image< BinaryPixelType, Dimension > BinaryImageType;<br>
<br><br> typedef signed short InputPixelType;<br><br> typedef itk::Image< InputPixelType, Dimension > InputImageType;<br> typedef unsigned char LabelType;<br> const unsigned int dim = 3;<br> typedef itk::ShapeLabelObject< LabelType, dim > LabelObjectType;<br>
typedef itk::LabelMap< LabelObjectType > LabelMapType;<br><br> typedef itk::LabelMap< LabelObjectType > LabelMapType2;<br><br> typedef itk::BinaryImageToShapeLabelMapFilter< BinaryImageType, LabelMapType> ConverterType;<br>
<br> ConverterType::Pointer converter = ConverterType::New();<br><br> typedef itk::BinaryImageToShapeLabelMapFilter< BinaryImageType, LabelMapType> ConverterType2;<br> ConverterType2::Pointer converter2 = ConverterType2::New();<br>
<br> typedef itk::ImageFileReader< InputImageType > ReaderType;<br> typedef itk::ImageFileWriter< BinaryImageType > WriterType;<br> <br> ReaderType::Pointer reader = ReaderType::New();<br> WriterType::Pointer writer = WriterType::New();<br>
<br> typedef itk::GDCMImageIO ImageIOTypefixed;<br> ImageIOTypefixed::Pointer gdcmImageIOfixed = ImageIOTypefixed::New();<br> reader->SetImageIO( gdcmImageIOfixed );<br><br> typedef itk::GDCMImageIO ImageIOTypefixed2;<br>
ImageIOTypefixed2::Pointer gdcmImageIOfixed2 = ImageIOTypefixed2::New();<br><br> reader->SetFileName( argv[1] );<br> <br> reader->Update();<br> <br> typedef itk::ConfidenceConnectedImageFilter< <br> InputImageType, <br>
BinaryImageType <br> > ConfidenceConnectedFilterType;<br><br> ConfidenceConnectedFilterType::Pointer confidenceConnectedFilter = <br>
ConfidenceConnectedFilterType::New();<br><br> typedef itk::SimpleFuzzyConnectednessScalarImageFilter< <br> InputImageType, <br>
BinaryImageType <br> > FuzzySegmentationFilterType;<br><br> FuzzySegmentationFilterType::Pointer fuzzysegmenter = <br>
FuzzySegmentationFilterType::New();<br><br> typedef FuzzySegmentationFilterType::FuzzySceneType FuzzySceneType; <br><br> typedef itk::ImageFileWriter< FuzzySceneType > FuzzyWriterType;<br>
<br> FuzzyWriterType::Pointer fwriter = FuzzyWriterType::New();<br> <br> fwriter->SetFileName( argv[3] );<br><br> BinaryImageType::IndexType index;<br> <br> index[0] = atoi( argv[4] );<br> index[1] = atoi( argv[5] );<br>
index[2] = atoi( argv[6] );<br><br> const double varianceMultiplier = atof( argv[10] );<br><br> confidenceConnectedFilter->SetInput( reader->GetOutput() );<br> confidenceConnectedFilter->SetMultiplier( varianceMultiplier );<br>
confidenceConnectedFilter->SetNumberOfIterations( 12 );<br> <br> fuzzysegmenter->SetInput( reader->GetOutput() );<br><br> double varianceEstimation = 0;<br><br> double meanEstimation = 0;<br><br> confidenceConnectedFilter->SetSeed( index );<br>
confidenceConnectedFilter->Update();<br><br> meanEstimation = confidenceConnectedFilter->GetMean();<br> varianceEstimation = confidenceConnectedFilter->GetVariance();<br><br> std::cout << "Mean estimation = " << meanEstimation << std::endl;<br>
std::cout << "Variance estimation = " << varianceEstimation << std::endl;<br><br> if( varianceEstimation > meanEstimation/atoi( argv[11] ) )<br> {<br> <br> index[0] = index[0]-10;<br>
index[1] = index[1]-10;<br> index[2] = index[2]-10;<br><br> confidenceConnectedFilter->SetSeed( index );<br> confidenceConnectedFilter->Update();<br><br> meanEstimation = confidenceConnectedFilter->GetMean();<br>
varianceEstimation = confidenceConnectedFilter->GetVariance();<br><br> }<br><br> while( varianceEstimation > meanEstimation/atoi( argv[11] ) )<br> {<br><br> index[0] = index[0] + 1;<br><br> confidenceConnectedFilter->SetSeed( index );<br>
confidenceConnectedFilter->Update();<br><br> meanEstimation = confidenceConnectedFilter->GetMean();<br> varianceEstimation = confidenceConnectedFilter->GetVariance();<br> <br> std::cout << "Mean estimation = " << meanEstimation << std::endl;<br>
std::cout << "Variance estimation = " << varianceEstimation << std::endl;<br><br> if ( varianceEstimation > meanEstimation/atoi( argv[11] ))<br> {<br> index[1] = index[1] + 1;<br>
<br> confidenceConnectedFilter->SetSeed( index );<br> confidenceConnectedFilter->Update();<br><br> meanEstimation = confidenceConnectedFilter->GetMean();<br> varianceEstimation = confidenceConnectedFilter->GetVariance();<br>
<br> std::cout << "Mean estimation = " << meanEstimation << std::endl;<br> std::cout << "Variance estimation = " << varianceEstimation << std::endl;<br>
}<br><br> if ( varianceEstimation > meanEstimation/atoi( argv[11] ))<br> {<br> index[2] = index[2] + 1;<br><br> confidenceConnectedFilter->SetSeed( index );<br> confidenceConnectedFilter->Update();<br>
<br> meanEstimation = confidenceConnectedFilter->GetMean();<br> varianceEstimation = confidenceConnectedFilter->GetVariance();<br> <br> std::cout << "Mean estimation = " << meanEstimation << std::endl;<br>
std::cout << "Variance estimation = " << varianceEstimation << std::endl;<br> }<br><br> }<br><br> std::cout << index << std::endl;<br><br> std::cout << "Mean estimation = " << meanEstimation << std::endl;<br>
std::cout << "Variance estimation = " << varianceEstimation << std::endl;<br><br> fuzzysegmenter->SetObjectSeed( index );<br> <br> fuzzysegmenter->SetMean( meanEstimation );<br> fuzzysegmenter->SetVariance( varianceEstimation );<br>
fuzzysegmenter->SetThreshold( 0.5 );<br><br> fuzzysegmenter->Update();<br><br> //begin second label<br> <br> FuzzySegmentationFilterType::Pointer fuzzysegmenter2 = <br> FuzzySegmentationFilterType::New();<br>
index[0] = atoi( argv[7] );<br> index[1] = atoi( argv[8] );<br> index[2] = atoi( argv[9] );<br><br> confidenceConnectedFilter->SetInput( reader->GetOutput() );<br> confidenceConnectedFilter->SetMultiplier( varianceMultiplier );<br>
confidenceConnectedFilter->SetNumberOfIterations( 12 );<br> <br> fuzzysegmenter2->SetInput( reader->GetOutput() );<br><br> varianceEstimation = 0;<br><br> meanEstimation = 0;<br><br> confidenceConnectedFilter->SetSeed( index );<br>
confidenceConnectedFilter->Update();<br><br> meanEstimation = confidenceConnectedFilter->GetMean();<br> varianceEstimation = confidenceConnectedFilter->GetVariance();<br><br> std::cout << "Mean estimation = " << meanEstimation << std::endl;<br>
std::cout << "Variance estimation = " << varianceEstimation << std::endl;<br><br> if( varianceEstimation > meanEstimation/atoi( argv[11] ) )<br> {<br> <br> index[0] = index[0]-10;<br>
index[1] = index[1]-10;<br> index[2] = index[2]-10;<br><br> confidenceConnectedFilter->SetSeed( index );<br> confidenceConnectedFilter->Update();<br><br> meanEstimation = confidenceConnectedFilter->GetMean();<br>
varianceEstimation = confidenceConnectedFilter->GetVariance();<br><br> }<br><br> while( varianceEstimation > meanEstimation/atoi( argv[11] ) )<br> {<br><br> index[0] = index[0] + 1;<br><br> confidenceConnectedFilter->SetSeed( index );<br>
confidenceConnectedFilter->Update();<br><br> meanEstimation = confidenceConnectedFilter->GetMean();<br> varianceEstimation = confidenceConnectedFilter->GetVariance();<br> <br> std::cout << "Mean estimation = " << meanEstimation << std::endl;<br>
std::cout << "Variance estimation = " << varianceEstimation << std::endl;<br><br> if ( varianceEstimation > meanEstimation/atoi( argv[11] ))<br> {<br> index[1] = index[1] + 1;<br>
<br> confidenceConnectedFilter->SetSeed( index );<br> confidenceConnectedFilter->Update();<br><br> meanEstimation = confidenceConnectedFilter->GetMean();<br> varianceEstimation = confidenceConnectedFilter->GetVariance();<br>
<br> std::cout << "Mean estimation = " << meanEstimation << std::endl;<br> std::cout << "Variance estimation = " << varianceEstimation << std::endl;<br>
}<br><br> if ( varianceEstimation > meanEstimation/atoi( argv[11] ))<br> {<br> index[2] = index[2] + 1;<br><br> confidenceConnectedFilter->SetSeed( index );<br> confidenceConnectedFilter->Update();<br>
<br> meanEstimation = confidenceConnectedFilter->GetMean();<br> varianceEstimation = confidenceConnectedFilter->GetVariance();<br> <br> std::cout << "Mean estimation = " << meanEstimation << std::endl;<br>
std::cout << "Variance estimation = " << varianceEstimation << std::endl;<br> }<br><br> }<br><br> std::cout << index << std::endl;<br><br> std::cout << "Mean estimation = " << meanEstimation << std::endl;<br>
std::cout << "Variance estimation = " << varianceEstimation << std::endl;<br><br> fuzzysegmenter2->SetObjectSeed( index );<br> <br> fuzzysegmenter2->SetMean( meanEstimation );<br> fuzzysegmenter2->SetVariance( varianceEstimation );<br>
fuzzysegmenter2->SetThreshold( 0.5 );<br><br> fuzzysegmenter2->Update();<br><br> //end second label<br><br> typedef itk::MetaDataDictionary DictionaryType;<br> <br> DictionaryType inputdict = reader->GetMetaDataDictionary();<br>
<br> writer->SetMetaDataDictionary( inputdict );<br> <br> writer->SetFileName( argv[2] );<br><br> converter->SetInput( fuzzysegmenter->GetOutput() );<br><br> converter->SetFullyConnected( true );<br>
<br>
converter->SetInputForegroundValue( 255 );<br><br> converter->SetOutputBackgroundValue( 0 );<br><br> converter->Update();<br> <br> converter2->SetInput( fuzzysegmenter2->GetOutput() );<br><br> converter2->SetFullyConnected( true );<br>
<br> converter2->SetInputForegroundValue( 255 );<br><br> converter2->SetOutputBackgroundValue( 0 );<br><br> converter2->Update();<br> <br> LabelMapType::Pointer labelMap = converter->GetOutput();<br><br>
LabelMapType2::Pointer labelMap2 = converter2->GetOutput();<br><br> std::cout.precision(20);<br><br> std::cout << "labelMap->GetNumberOfLabelObjects() = " << labelMap->GetNumberOfLabelObjects() << std::endl;<br>
<br> std::cout << "labelMap2->GetNumberOfLabelObjects() = " << labelMap2->GetNumberOfLabelObjects() << std::endl;<br><br> for( unsigned int label=1; label<=labelMap->GetNumberOfLabelObjects(); label++ )<br>
{<br> // we don't need a SmartPointer of the label object here, because the reference is kept in<br> // in the label map.<br> const LabelObjectType * labelObject = labelMap->GetLabelObject( label );<br>
std::cout << label << "\t" << labelObject->GetSize() << "\t" << labelObject->GetPhysicalSize() << "\t" << labelObject->GetCentroid() << std::endl;<br>
}<br> <br> for( unsigned int label=1; label<=labelMap2->GetNumberOfLabelObjects(); label++ )<br> {<br> // we don't need a SmartPointer of the label object here, because the reference is kept in<br> // in the label map.<br>
const LabelObjectType * labelObject2 = labelMap2->GetLabelObject( label );<br> std::cout << label << "\t" << labelObject2->GetSize() << "\t" << labelObject2->GetPhysicalSize() << "\t" << labelObject2->GetCentroid() << std::endl;<br>
}<br><br> //added to merge the two label maps<br> <br> typedef itk::MergeLabelMapFilter< LabelMapType > ConverterMergeType;<br><br> ConverterMergeType::Pointer convertermerge = ConverterMergeType::New();<br>
<br> /*******************************<br><br> how do I merge labelMap and LabelMap2 ?<br><br> ********************************/<br><br> //end<br><br> typedef itk::LabelMapToLabelImageFilter< LabelMapType, BinaryImageType > L2IType;<br>
<br> L2IType::Pointer l2i = L2IType::New();<br> <br> l2i->SetInput( converter->GetOutput() );<br><br> writer->SetInput( l2i->GetOutput() );<br><br> try<br> {<br> writer->Update();<br> }<br> catch( itk::ExceptionObject & excep )<br>
{<br> std::cerr << "Exception caught !" << std::endl;<br> std::cerr << excep << std::endl;<br> }<br> <br> fwriter->SetInput( fuzzysegmenter->GetFuzzyScene() );<br> fwriter->Update();<br>
<br> return 0;<br>}<br><br><br><div class="gmail_quote">2009/12/17 Gaëtan Lehmann <span dir="ltr"><<a href="mailto:gaetan.lehmann@jouy.inra.fr" target="_blank">gaetan.lehmann@jouy.inra.fr</a>></span><div><div></div>
<div class="h5"><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
Le 17 déc. 09 ŕ 18:22, John Drozd a écrit :<div><br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi,<br>
<br>
Can anyone show me how to use the MergeLabelMapFilter to merge two label maps?<br>
</blockquote>
<br>
<br></div>
Hi John,<br>
<br>
You can look at Testing/Code/Review/itkMergeLabelMapFilterTest1.cxx, as an example.<br>
<br>
Can you be more specific on what you want to do?<br>
<br>
Regards,<br>
<br>
Gaëtan<br><font color="#888888">
<br>
<br>
-- <br>
Gaëtan Lehmann<br>
Biologie du Développement et de la Reproduction<br>
INRA de Jouy-en-Josas (France)<br>
tel: +33 1 34 65 29 66 fax: 01 34 65 29 09<br>
<a href="http://voxel.jouy.inra.fr" target="_blank">http://voxel.jouy.inra.fr</a> <a href="http://www.itk.org" target="_blank">http://www.itk.org</a><br>
<a href="http://www.mandriva.org" target="_blank">http://www.mandriva.org</a> <a href="http://www.bepo.fr" target="_blank">http://www.bepo.fr</a><br>
<br>
</font></blockquote></div></div></div><br>
</blockquote></div><br>