InsightSoftwareConsortium / ITK

Insight Toolkit (ITK) -- Official Repository. ITK builds on a proven, spatially-oriented architecture for processing, segmentation, and registration of scientific images in two, three, or more dimensions.
https://itk.org
Apache License 2.0
1.37k stars 660 forks source link

Segmentation fault #4604

Open Mr-Blue-Sky-Candy opened 2 months ago

Mr-Blue-Sky-Candy commented 2 months ago

I only get segmentation fault. I can share the code I wrote but not the Dicom series. The code I wrote works for all others but not for some particular CT scans. How can I help you resolve this?

The code I wrote is the following:

names_generator = itk.GDCMSeriesFileNames.New()
names_generator.SetUseSeriesDetails(True)
names_generator.SetGlobalWarningDisplay(True)
names_generator.SetNumberOfWorkUnits(os.cpu_count())
names_generator.SetLoadPrivateTags(False)
names_generator.DebugOn()

dicomIO = itk.GDCMImageIO.New()
dicomIO.LoadPrivateTagsOff()
dicomIO.SetGlobalWarningDisplay(True)
reader = itk.ImageSeriesReader[itk.Image[itk.ctype("int"), 3]].New()
reader.SetGlobalWarningDisplay(True)
reader.SetMetaDataDictionaryArrayUpdate(True)
reader.SetNumberOfWorkUnits(os.cpu_count())
reader.SetImageIO(dicomIO)
reader.ForceOrthogonalDirectionOff()

It gives segmentation fault at the line names_generator.SetDirectory(folder)

This code also gives segmentation fault:

reader = sitk.ImageSeriesReader()
dicom_names = reader.GetGDCMSeriesFileNames(folder)
reader.SetFileNames(dicom_names)
image = reader.Execute()

Pydicom is able to read it but its metadata is very messy and hard to format besides it cannot read other particular Dicom series of CT scans.

ITK version is 5.3 on both ubuntu 22 and archlinux

github-actions[bot] commented 2 months ago

Thank you for contributing an issue! šŸ™

Welcome to the ITK community! šŸ¤—šŸ‘‹ā˜€ļø

We are glad you are here and appreciate your contribution. Please keep in mind our community participation guidelines. šŸ“œ Also, please check existing open issues and consider discussion on the ITK Discourse. šŸ“–

This is an automatic message. Allow for time for the ITK community to be able to read the issue and comment on it.

dzenanz commented 2 months ago

I assume that this is not related to Python packaging, but to underlying GDCM code. Reproducing without data will probably be hard. Can you try anonymizing your DICOM file(s) and then sharing them? Use this or one of many other tools.

Mr-Blue-Sky-Candy commented 2 months ago

I agree it is due to binded cpp or c code but I cannot share the dicom files. I know how to code cpp as well but I don't know how to debug a python binding. If there is a way I can help about this, I would be happy to do so

dzenanz commented 2 months ago

Can you try C++ code from this example on your DICOM data? Does it also crash?

thewtex commented 2 months ago

Hi, could you please try with itk-5.4rc4? Do the same results occur?

thewtex commented 2 months ago

Also,

image = itk.imread('path/to/dicom/dir/')
Mr-Blue-Sky-Candy commented 2 months ago

Hi, could you please try with itk-5.4rc4? Do the same results occur?

this also failed

Mr-Blue-Sky-Candy commented 2 months ago

Also,

image = itk.imread('path/to/dicom/dir/')

I already tried but forgot to mention. The outcome is the same

thewtex commented 2 months ago

If you set up a debug build and get a backtrace, it will likely be evident where the issue exists. Please see Section 9.5.3 in The ITK Software Guide, Book 1 for guidance.

Mr-Blue-Sky-Candy commented 2 months ago

Hey, I had to refresh my cpp skills. I ran with debug mode but I always used qt creator run debug mode. So I can see this:

1  ??                                                                                                 0x7ffff4cab32c 
2  raise                                                                                              0x7ffff4c5a6c8 
3  abort                                                                                              0x7ffff4c424b8 
4  std::__glibcxx_assert_fail                                             debug.cc                 61 0x7ffff4edd3b2 
5  ??                                                                                                 0x7ffff654881e 
6  gdcm::PixmapReader::ReadImageInternal(gdcm::MediaStorage const&, bool)                             0x7ffff654ab16 
7  gdcm::ImageReader::ReadImage(gdcm::MediaStorage const&)                                            0x7ffff654c880 
8  gdcm::PixmapReader::Read()                                                                         0x7ffff65472da 
9  gdcm::SerieHelper::AddFileName(std::string const&)                                                 0x7ffff6522c7d 
10 gdcm::SerieHelper::SetDirectory(std::string const&, bool)                                          0x7ffff6522f3c 
11 itk::GDCMSeriesFileNames::SetInputDirectory(std::string const&)                                    0x7ffff698ebb4 
12 itk::GDCMSeriesFileNames::SetDirectory                                 itkGDCMSeriesFileNames.h 98 0x555555567b5d 
13 main                                                                   main.cpp                 32 0x555555566dc5 

Is this good? It also show disassembler output. If you need me to share something else, let me know. I don't know how to proceed from this point on: image

thewtex commented 2 months ago

Nice!

It would be helpful to build as Debug CMAKE_BUILD_TYPE and find out the line number that identifies which assert is failing:

https://github.com/InsightSoftwareConsortium/ITK/blob/596ed5fd6dd52f0beb2c6af6147015fe09923736/Modules/ThirdParty/GDCM/src/gdcm/Source/MediaStorageAndFileFormat/gdcmPixmapReader.cxx#L623-L1132

CC @malaterre

Mr-Blue-Sky-Candy commented 2 months ago

I can see this in the command line output:

$ ./test_itk ../../../../work/AIRTQA/r-008-023-2-corrupt/
/usr/include/c++/13.2.1/bits/stl_vector.h:1125: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = gdcm::Curve; _Alloc = std::allocator<gdcm::Curve>; reference = gdcm::Curve&; size_type = long unsigned int]: Assertion '__n < this->size()' failed.
Aborted (core dumped)
thewtex commented 2 months ago

@Mr-Blue-Sky-Candy I expect that there is a line number associated with gdcmPixmapReader.cxx that has an assert statement when a Debug build is created and it is debugged as described in the ITK Software Guide.

Mr-Blue-Sky-Candy commented 2 months ago

I am already building Debug builds but as it is described in ITK software guide I switched to RelWithDebInfo build anyway. Unsurprisingly, the result is the same.

thewtex commented 2 months ago

@Mr-Blue-Sky-Candy there are no line numbers for the screenshot shared in the GDCM calls -- is ITK_USE_SYSTEM_GDCM OFF?

Mr-Blue-Sky-Candy commented 2 months ago

I added set(ITK_USE_SYSTEM_GDCM ON) to the CMakeLists.txt but the result is the same

thewtex commented 2 months ago

The setting should be:

set(ITK_USE_SYSTEM_GDCM OFF)

So GDCM with a Debug build is used. Examining with gdb may be helpful as suggested in the ITK Software Guide.

Mr-Blue-Sky-Candy commented 2 months ago

I am already running in qtcreator with gdb.

@Mr-Blue-Sky-Candy there are no line numbers for the screenshot shared in the GDCM calls -- is ITK_USE_SYSTEM_GDCM OFF?

Actually there is. I will share a different view with set(ITK_USE_SYSTEM_GDCM OFF): image

thewtex commented 2 months ago

We are looking for what is happening at Level 7:

image

Mr-Blue-Sky-Candy commented 2 months ago

Ahh OK. Thanks. I recompiled the system gdcm with debug symbols. Here you go: image

Mr-Blue-Sky-Candy commented 2 months ago

All functions look better here image

thewtex commented 2 months ago

Thanks, closer!

You will need to step through the code in gdcmPixmapReader (set a breakpoint in QtCreater) to understand what content that is expected is not present.

Mr-Blue-Sky-Candy commented 2 months ago

image Here return Curves[i] in the file gdcmPixmap.h:47, i is 7 but it is larger than the size of Curves

thewtex commented 2 months ago

@malaterre any suggestions?

malaterre commented 2 months ago

@thewtex Curve module have been deprecated for years now. Without the actual DICOM DataSet there are plenty ways to create an illegal structure.

@Mr-Blue-Sky-Candy If you cannot share the DICOM file, I suggest building with 'Debug', you should at least get the proper behavior for 'assert' and hopefully track the root issue (not symptom).