版本信息:Win10(64bit),VS2013(64bit),ITK4.8.2,VTK6.3.0,Qt5.4.2
1 问题描述
使用VTK实现对3D CT数据进行切片重新提取,然后使用ITK对切片进行二维分析,这是需要使用到itkConnectedComponentImageFilter将灰度图转换为Label Image。当切片的尺寸非常小时(如2x2),会出现错误:
Debug Assertion Failed! Program: C:\WINDOWS\SYSTEM32\MSVCP120D.dll File: C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\vector Line: 1201 Expression: vector subscript out of range For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts. (Press Retry to debug the application)上述提示vector下标越界,而 比较令人疑惑的是,我并没有使用std::vector。继续 使用VS2013进行调试发现错误原因为:
std::vector<std::vector<itk::ConnectedComponentImageFilter<itk::Image<unsigned char,2>,itk::Image<unsigned short,2>,itk::Image<unsigned char,2> >::runLength,std::allocator<itk::ConnectedComponentImageFilter<itk::Image<unsigned char,2>,itk::Image<unsigned short,2>,itk::Image<unsigned char,2> >::runLength> >,std::allocator<std::vector<itk::ConnectedComponentImageFilter<itk::Image<unsigned char,2>,itk::Image<unsigned short,2>,itk::Image<unsigned char,2> >::runLength,std::allocator<itk::ConnectedComponentImageFilter<itk::Image<unsigned char,2>,itk::Image<unsigned short,2>,itk::Image<unsigned char,2> >::runLength> > > >::operator[](unsigned __int64 _Pos) 行 1202 C++
也即是说itkConnectedComponentImageFilter里面使用了std::vector,并且产生了下标越界。
2 解决方法
最好的方法就是不要使用itkConnectedComponentImageFilter,而改用其它方法生成Label Image:
//![0] -- UCharImageType typedef unsigned char UCharPixelType; typedef itk::Image<UCharPixelType, 2> ImageType; typedef ImageType itkImageData; typedef ImageType UCharImageType; // [0] //![1] -- UShortImageType typedef unsigned short UShortPixelType; typedef itk::Image<UShortPixelType, 2> UShortImageType; // [1] typedef itk::BinaryImageToLabelMapFilter<UCharImageType> BinaryImageToLabelMapFilterType; typedef itk::LabelMapToLabelImageFilter <BinaryImageToLabelMapFilterType::OutputImageType, UShortImageType> LabelMapToLabelImageFilterType; BinaryImageToLabelMapFilterType::Pointer binary2Label = BinaryImageToLabelMapFilterType::New(); LabelMapToLabelImageFilterType::Pointer labelMap2LabelImage = LabelMapToLabelImageFilterType::New(); binary2Label->SetInput( image ); // image的类型是itkImageData,在上面有定义,此处略去image binary2Label->Update(); // 的产生方法 labelMap2LabelImage->SetInput( binary2Label->GetOutput() ); labelMap2LabelImage->UpdateLargestPossibleRegion(); labelMap2LabelImage->Update();