[Insight-developers] select an item from a binary image

Bradley Lowekamp blowekamp at mail.nih.gov
Wed Nov 13 08:54:49 EST 2013


Hi,

That is what the BoundingBoxImageLabelMapFilter is suppose to do. It crops an axis aligned bounding box around a label in the label map infrastructure.

What are you trying to do?

And why did you try this approach?

There is the MaskImageFilter[1] that may suit your needs.

Brad

[1] http://www.itk.org/Doxygen/html/classitk_1_1MaskImageFilter.html

On Nov 13, 2013, at 8:25 AM, elhadj meljane <elhadj.meljane at gmail.com> wrote:

> Hi Bradley,
> Thanks, it worked when I added a label image. But it crops the item in a smaller image. Is there a way to keep the initial image size: just mask the other items in the initial image ...
>  
> Thanks.
> Best,
> EM
> 
> 
> On Tue, Nov 12, 2013 at 9:50 PM, Bradley Lowekamp <blowekamp at mail.nih.gov> wrote:
> Hello,
> 
> Around ITK programs you need exception handling to determine the errors thrown.
> 
> I added the following around your code:
> 
> try
> {
> ...
> }
> catch (std::exception &e)
>   {
> std::cerr << "Exception: " << e.what() << std::endl;
> return 1;
> }
> catch (...)
>   {
> std::cerr << "Unknown Exception!" << std::endl;
> throw;
> }
> 
> Note that the itk::ExceptionObject class is derived from std::exception so this will catch that too.
> 
> The gave me the following output:
> 
> Exception: /Users/blowekamp/src/ITK/Modules/Core/Common/src/itkProcessObject.cxx:1380:
> itk::ERROR: BoundingBoxImageLabelMapFilter(0x7f92bb8bce90): Input FeatureImage is required but not set.
> 
> Which says you are missing an input to this filter.
> 
> You can provide the input label image as the feature image like so:
> 
> toBBILabelMap->SetFeatureImage(reader->GetOutput());
> 
> The examples in that modules should be usable. You just need to get your labels correct for the binary/label image. Not too hard to run a ConnectedComponets filter if needed.
> 
> Brad
> 
> On Nov 12, 2013, at 2:43 PM, elhadj meljane <elhadj.meljane at gmail.com> wrote:
> 
>>  I don't have a label image to use the example: https://github.com/blowekamp/itkOBBLabelMap/blob/master/test/itkOBBExample.cxx
>>  
>> However, I used to write the main method ... where only a binary image is used as input.
>>  
>> Thanks!
>> EM
>> 
>> 
>> On Tue, Nov 12, 2013 at 2:26 PM, elhadj meljane <elhadj.meljane at gmail.com> wrote:
>> Hello!
>> I am using your github extension (I included the files manually in my project)... and wrote the main method below. The code compile but at the run time I got the error:
>> 
>> "program.exe has stopped working. A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available. "
>> When I click to debug with MS Visual Studio, I got the warning: 
>> Unhandled exception at 0x01... in program.exe: 0xC00000005: Acces violation reading location 0x18...
>>  
>> Here's the code:
>>  
>> #include "itkImageFileReader.h"
>> 
>> #include "itkImageFileWriter.h"
>> 
>> #include "itkBoundingBoxImageLabelMapFilter.h"
>> 
>> #include "itkAttributeImageLabelObject.h"
>> 
>> #include "itkBinaryImageToShapeLabelMapFilter.h"
>> 
>>  
>> const unsigned int Dimension = 2;
>> 
>> typedef unsigned char LabelPixelType;
>> 
>> typedef itk::Image< LabelPixelType, Dimension > LabelImageType;
>> 
>> typedef unsigned char OutputPixelType;
>> 
>> typedef itk::Image< OutputPixelType, Dimension > OutputImageType;
>> 
>> typedef itk::ImageFileReader< OutputImageType > ReaderType;
>> 
>> typedef itk::ImageFileWriter< OutputImageType > WriterType;
>> 
>> typedef itk::AttributeImageLabelObject< LabelPixelType, Dimension, OutputImageType > LabelObjectAttributeType;
>> 
>> typedef itk::LabelMap<LabelObjectAttributeType> LabelMapAttributeType;
>> 
>> typedef itk::BinaryImageToShapeLabelMapFilter
>> 
>> <OutputImageType, LabelMapAttributeType> LabelMapperAttributeType;
>> 
>> typedef itk::LabelMapToLabelImageFilter<LabelMapAttributeType, OutputImageType> LabelMapToLabelImageFilterType;
>> 
>> typedef itk::BoundingBoxImageLabelMapFilter<LabelMapAttributeType> BBILabelMapFilter;
>> 
>>  
>>  
>> int main( int argc, char *argv[] )
>> 
>> {
>> 
>> 
>> ReaderType::Pointer reader = ReaderType::New();
>> 
>> typedef itk::ImageFileWriter< OutputImageType > WriterType;
>> 
>>  
>> //input
>> 
>> reader->SetFileName( argv[1] );
>> 
>> 
>> //label image
>> 
>> LabelMapperAttributeType::Pointer labelerAttribute = LabelMapperAttributeType::New();
>> 
>> labelerAttribute->SetInput( reader->GetOutput() );
>> 
>> labelerAttribute->SetComputePerimeter( false );
>> 
>> labelerAttribute->SetOutputBackgroundValue(0);
>> 
>> labelerAttribute->Update();
>> 
>> LabelMapAttributeType::Pointer labelMapAttribute = labelerAttribute->GetOutput();
>> 
>> 
>> //extract the component with the label 1
>> 
>> BBILabelMapFilter::Pointer toBBILabelMap = BBILabelMapFilter::New();
>> 
>> toBBILabelMap->SetInput(labelMapAttribute);
>> 
>> toBBILabelMap->Update();
>> 
>> const LabelObjectAttributeType* labelObjectAttribute = toBBILabelMap->GetOutput()->GetLabelObject(1);
>> 
>> 
>> //write
>> 
>> WriterType::Pointer writer = WriterType::New();
>> 
>> writer->SetFileName( "copy_input.jpg" );
>> 
>> writer->SetInput( reader->GetOutput());
>> 
>> writer->Update();
>> 
>> 
>> return 0;
>> 
>> }
>> 
>>  
>>  
>>  
>> 
>>  
>>  
>> 
>>  
>> On Tue, Nov 12, 2013 at 1:53 PM, Bradley Lowekamp <blowekamp at mail.nih.gov> wrote:
>> Hello,
>> 
>> It appears that you are trying to use some code from my github extension:
>> 
>> https://github.com/blowekamp/itkOBBLabelMap
>> 
>> I am not sure where to begin helping you. You didn't include the run-time error message, and your code fragment is missing key parts. Can you post a minimal compilable example which illustrates the problem? Sometime just narrowing down you code to this part help you figure out your issue.
>> 
>> Do the tests for the extension pass for you?
>> 
>> Are you able to run the example?
>> 
>> https://github.com/blowekamp/itkOBBLabelMap/blob/master/test/itkOBBExample.cxx
>> 
>> Brad
>> 
>> 
>> On Nov 12, 2013, at 1:43 PM, elhadj meljane <elhadj.meljane at gmail.com> wrote:
>> 
>>> Hi all, 
>>> I am trying to use itk to select a component from a binary image. The binary image contains several items and I want to separate them in different images.
>>>    
>>> I used the code below but it doesn't work. I got an error message at the run time. It looks from the debugger that the line
>>>  
>>> labelerAttribute->Update()
>>>  
>>> causes the error. I gave a valid file as input and  checked that ITK reads correctly this file.  I would appreciate your help to fix this!
>>> 
>>> #include "itkAttributeImageLabelObject.h"
>>> 
>>> const      unsigned int    Dimension = 2;
>>> typedef  unsigned char LabelPixelType;
>>> typedef  itk::Image< LabelPixelType, Dimension >  LabelImageType;
>>> typedef  unsigned char                           OutputPixelType;
>>> typedef  itk::Image< OutputPixelType, Dimension > OutputImageType;
>>> typedef   itk::ImageFileReader< OutputImageType > ReaderType;
>>> typedef   itk::ImageFileWriter<  OutputImageType  > WriterType;
>>> typedef  itk::AttributeImageLabelObject< LabelPixelType, Dimension, OutputImageType > LabelObjectAttributeType;
>>> typedef  itk::LabelMap<LabelObjectAttributeType> LabelMapAttributeType;
>>> typedef  itk::BinaryImageToShapeLabelMapFilter 
>>>    <OutputImageType, LabelMapAttributeType>  LabelMapperAttributeType;
>>> typedef  itk::LabelMapToLabelImageFilter<LabelMapAttributeType, OutputImageType> LabelMapToLabelImageFilterType;
>>> typedef  itk::BoundingBoxImageLabelMapFilter<LabelMapAttributeType> BBILabelMapFilter;
>>> typedef   itk::ImageFileWriter<  OutputImageType  > WriterType;
>>> 
>>> ReaderType::Pointer reader = ReaderType::New();
>>> 
>>> reader->SetFileName( argv[1] );
>>>  
>>> LabelMapperAttributeType::Pointer labelerAttribute = LabelMapperAttributeType::New();
>>> 
>>> 
>>> labelerAttribute->SetInput( reader->GetOutput() );
>>> labelerAttribute->SetComputePerimeter( false ); 
>>> labelerAttribute->SetOutputBackgroundValue(0); 
>>> labelerAttribute->Update();
>>>  
>>>  
>>> //select the item with the label 1
>>> const LabelObjectAttributeType* labelObjectAttribute = toBBILabelMap->GetOutput()->GetLabelObject(1);
>>> 
>>> BBILabelMapFilter::Pointer toBBILabelMap =  BBILabelMapFilter::New();
>>> toBBILabelMap->SetInput(labelMapAttribute);
>>> toBBILabelMap->Update();
>>> 
>>> 
>>> Thanks.
>>> Best 
>>> EM
>>> 
>>> _______________________________________________
>>> Powered by www.kitware.com
>>> 
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>> 
>>> Kitware offers ITK Training Courses, for more information visit:
>>> http://kitware.com/products/protraining.php
>>> 
>>> Please keep messages on-topic and check the ITK FAQ at:
>>> http://www.itk.org/Wiki/ITK_FAQ
>>> 
>>> Follow this link to subscribe/unsubscribe:
>>> http://www.itk.org/mailman/listinfo/insight-developers
>> 
>> 
>> 
> 
> 
> _______________________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
> 
> Kitware offers ITK Training Courses, for more information visit:
> http://kitware.com/products/protraining.php
> 
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-developers
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-developers/attachments/20131113/14970cd1/attachment.htm>


More information about the Insight-developers mailing list