Importing DICOM files in my own module C++

I would like to import DICOM files in my own module by calling the import function in the “DICOM” module without actually switching to the “DICOM” module. Is this possible to do?

I was thinking of calling the import functions in the “DICOM” module in the backend, but the DICOM module is written in python and my module is in C++.

The python code is something like this:

plugin = slicer.modules.dicomPlugins’DICOMScalarVolumePlugin’
loadables = plugin.examineFiles(dicom_fileList)
if len(loadables) > 1:
print(“Multiple DICOM scans detected! Not proceeding”)
sys.exit(1)
volume = plugin.load(loadables[0])

The DICOM browser and indexer are written in C++, so you can access them directly from C++. DICOM loader plugins are mostly implemented in Python, so you need to call Python from C++.

You can either use CPython or PythonQt API. For example, to run a Python command and get value of a variable using PythonQt API:

Thank you very much!

I’m currently trying to use PythonQt in my project. One issue I found is that I cannot pass arguments into my python script. my python script that I want to run is:

def dicom2file(path_to_dicom_dir,output_path):
    
## Grab indivial DICOM slices into a filelist
excluded_extensions = ['.txt', '.csv', '.xml', '.tag', '.nii']
dicom_fileList = [os.path.join(path_to_dicom_dir,file) for file in os.listdir(path_to_dicom_dir)
                    if not file.endswith( tuple(excluded_extensions) ) ]
# Loading DICOM using the ScalarVolumePlugin
plugin = slicer.modules.dicomPlugins['DICOMScalarVolumePlugin']()
loadables = plugin.examineFiles(dicom_fileList)
if len(loadables) > 1:
    print("Multiple DICOM scans detected! Not proceeding")
    sys.exit(1)
volume = plugin.load(loadables[0])

## Save the DICOM volume in scalar volume format
slicer.util.saveNode(volume, output_path)

## To-do: exits should be move to high-level wrapper
#exit()

I need to pass in 2 arguments path_to_dicom_dir and output_path
Is there a way for me to pass arguments into my python script in C++ with pythonQT or should I use something else?

You can set simple input values by writing their values in the executed string. For setting more complicated input objects or getting output objects, you can add/get variables in the context. See lots of examples in qSlicerDICOMExportDialog.cxx.

I will look into it, thank you so much!