Type Error while executing algorithms with python SDK

Hello,

I am getting this python TypeError TypeError: sequence item 0: Unable to cast Python instance to C++ type (compile in debug mode for details) everytime I try to run an algorithm on a basic ultrasound image. I followed the steps on the Python SDK documentations Basic usage part. Here is the code:

import imfusion

imfusion.init()

img = imfusion.open("images/testImages/test_snap1.png")

imfusion.executeAlgorithm('Filters;Morphological Operations', [img])

What can I do to solve this problem?

Thanks in advance

Hi Demir,

Can you try

imfusion.executeAlgorithm('Filters;Morphological Operations', img)

instead? I believe imfusion.open already returns a list.

Thanks Raphael,

Just passing the image as an argument fixed the issue. Now the problem I am having is that the “Generate confidence maps” algorithm does not accept my images.

This is the code I am using:

imfusion.init()

img = imfusion.open("images/testImages/test_snap1.png")

img2 = imfusion.executeAlgorithm('Set Modality', img, { 'modality': 'Ultrasound' })

imfusion.executeAlgorithm('Ultrasound;Compute Confidence Maps', img2)

At first I thought the issue was that the images were not ultrasound images, which the confidence map needs. So I added the img2 line where I set the modality of the image to an ultrasound but I still get the following error:

imfusion._bindings.IncompatibleError: No algorithm called 'Ultrasound;Compute Confidence Maps' is compatible with the given data.

Do you know how I can run the confidence maps algorithm correctly?

I believe the SetModality algorithm works in-place and does not return any output.
Can you try

img = imfusion.open("images/testImages/test_snap1.png")
imfusion.executeAlgorithm('Set Modality', img, { 'modality': 'Ultrasound' })
imfusion.executeAlgorithm('Ultrasound;Compute Confidence Maps', img)

?

Unfortunately that did not help me. I tried the code in your comment and I got the following error:

Traceback (most recent call last):
  File "/home/demir/thesis_catkin/src/force_adjustment/scripts/test.py", line 9, in <module>
    imfusion.executeAlgorithm('Ultrasound;Compute Confidence Maps', img)
imfusion._bindings.IncompatibleError: No algorithm called 'Ultrasound;Compute Confidence Maps' is compatible with the given data.
[] ***** Encountered fatal exception: SIGSEGV - Segmentation violation signal, PID 6289 *****
        stack dump [1]  /lib/x86_64-linux-gnu/libc.so.6+0x3ef10 [0x7fe26b4abf10]
        stack dump [2]  [0x2581010]

Segmentation fault (core dumped)

Do you have any other ideas how I can run the confidence maps algorithm?

I had a closer look at the SetModalityAlgorithm and it seems that you need to pass the modality not as a string but rather as the Data::Modality enum value (if you have the SDK, have a look at Data.h)

		enum Modality
		{
			NA = 0,            ///< Not applicable
			XRAY = 1,          ///< 2D X-Ray based data
			CT = 2,            ///< X-Ray Computed Tomography
			MRI = 3,           ///< Magnetic Resonance Imaging (MRI)
			ULTRASOUND = 4,    ///< Ultrasound image data
			VIDEO = 5,         ///< Video / optical image data
			NM = 6,            ///< Nuclear Medicine (SPECT or PET)
			OCT = 7,           ///< Optical Coherence Tomography
			LABEL = 8          ///< Labeling data/label map
		};

So basically, you need to execute

imfusion.executeAlgorithm('Set Modality', img, { 'modality': 4 })

I believe the error might be caused because there is only a single image in the sharedImageSet. In the ImFusionSuite application I also was not able to generate confidence maps of single ultrasound images. I would need to add multiple images to the SharedImageSet for that.
I will be getting images either as openCV images or numpy arrays. I want to add them to the ImageSet and set their modality to ultrasound and finally convert them to confidence maps. For a quick test I have the code below but setting the modality gives an error.


imfusion.init()
# Generate empty image set
imageset = imfusion.SharedImageSet(np.zeros([2, 714, 816, 1], dtype='uint8'))
# Open US image
img = imfusion.open("images/testImages/test_snap1.png")
# Add the same image to the set twice
np.append(imageset, img)
np.append(imageset, img)

# This line causes an error, I also tried running it with imageset[0] but I got the same error
imfusion.executeAlgorithm('Set Modality', imageset, { 'modality': 4 })


imfusion.executeAlgorithm('Ultrasound;Compute Confidence Maps', imageset)

Here is the error message:

TypeError: executeAlgorithm(): incompatible function arguments. The following argument types are supported:
    1. (name: str, data: list = [], properties: imfusion._bindings.Properties = None) -> list

Invoked with: 'Set Modality', imfusion.SharedImage(UBYTE width: 816 height: 714), {'modality': 4}
[] ***** Encountered fatal exception: SIGSEGV - Segmentation violation signal, PID 20154 *****
        stack dump [1]  /lib/x86_64-linux-gnu/libc.so.6+0x3ef10 [0x7fef31bccf10]
        stack dump [2]  [0x1573010]

Segmentation fault (core dumped)