Hi all, I have a very basic question. I am learning to use ITK in Python, and I can’t seem to get it to work. In a simple example, I am just trying to use a median image filter, but copying online examples doesn’t work. Here is the code:
My image I am processing is a single slice of a CT sequence. I am using Python 3.8.8, Pycharm is my editor (latest version), and Anaconda is the set up for Python. ITK and VTK seemed to install just fine in the Anaconda prompt run in Administrator mode. I have the same problem on a newer Windows 10 computer as well as on an older Windows 7 computer. itk.imread looks ok, but itk.median_image_filter is not available. When I look at the functions available under itk, there are some functions but a lot is missing. Suggestions?
Thanks for the suggestions. I did try a new conda environment, no difference. I did not reinstall conda, as I got the same result on two different computers (Win7 and Win10 computers). I did try using both pip and conda with the same result.
This is very frustrating. I am going to build locally the Python versions of both ITK and VTK to see if that works. I used to do that a while ago and it worked ok for VTK but ITK Python support was just beginning. I will respond when I have some results.
I’m building software, but I had a question. In the Anaconda Navigator, looking at the install I am using, I see itk, itk-core, itk-filtering, itk-io, itk-numerics, itk-registration, and itk-segmentation. Clearly the functionality that I need is in these other Python modules, but it is not being accessed via itk. Are these packages supposed to appear like this?
Thanks for the responses and help. I have given up for now, I will use C++ as always for ITK, I had just hoped it would be easier to make the transition to Python.
I am running Python 3.8.3 in PyCharm 2021.2.1 (Professional Edition).
The hints in PyCharm that show the functions available don’t show much. I see a few pages of things such as data types (e.g. SS, UC, etc.) but none of the other functionality. For example, I am just trying to run a BinaryThresholdImageFilter example, but that function is not available.
When I try to run the script on the command line (in PyCharm), everything is ok until I get to this line:
inputImageType = itk.Image(inputPixelType,2)
where inputPixelType = itk.SS (this line runs fine).
I get a lot of output from this:
********************************************************* / to differentiate the output
Traceback (most recent call last):
File “”, line 1, in
File “C:\Users\ogdenk\anaconda3\envs\py38\lib\site-packages\itk\support\template_class.py”, line 628, in call
filter = self.New(*args, **kwargs)
File “C:\Users\ogdenk\anaconda3\envs\py38\lib\site-packages\itk\support\template_class.py”, line 733, in New
raise itk.TemplateTypeError(self, input_type)
itk.support.extras.TemplateTypeError: itk.Image is not wrapped for input type None.
To limit the size of the package, only a limited number of
types are available in ITK Python. To print the supported
types, run the following command in your python environment:
itk.Image.GetTypes()
Possible solutions:
If you are an application user:
** Convert your input image into a supported format (see below).
** Contact developer to report the issue.
If you are an application developer, force input images to be
loaded in a supported pixel type.
e.g.: instance = itk.Image[itk.RGBPixel[itk.UC], int].New(my_input)
(Advanced) If you are an application developer, build ITK Python yourself and
turned to ON the corresponding CMake option to wrap the pixel type or image
dimension you need. When configuring ITK with CMake, you can set ITK_WRAP_${type} (replace ${type} with appropriate pixel type such as double). If you need to support images with 4 or 5 dimensions, you can add
these dimensions to the list of dimensions in the CMake variable ITK_WRAP_IMAGE_DIMS.
Supported input types:
itk.RGBPixel[itk.UC]
itk.RGBAPixel[itk.UC]
itk.Vector[itk.F,2]
itk.Vector[itk.F,3]
itk.Vector[itk.F,4]
itk.CovariantVector[itk.F,2]
itk.CovariantVector[itk.F,3]
itk.CovariantVector[itk.F,4]
itk.SS
itk.UC itk.US
itk.F
itk.D
itk.complex[itk.D]
itk.complex[itk.F]
itk.Vector[itk.D,2]
itk.Vector[itk.D,3]
itk.Vector[itk.D,4]
itk.SI
itk.UI
itk.UL
itk.ULL
itk.B
itk.FixedArray[itk.F,2]
itk.FixedArray[itk.D,2]
itk.Offset[2]
itk.SymmetricSecondRankTensor[itk.D,2]
itk.SymmetricSecondRankTensor[itk.F,2]
itk.RGBPixel[itk.UC]
itk.RGBAPixel[itk.UC]
itk.Vector[itk.F,2]
itk.Vector[itk.F,3]
itk.Vector[itk.F,4]
itk.CovariantVector[itk.F,2]
itk.CovariantVector[itk.F,3]
itk.CovariantVector[itk.F,4]
itk.SS
itk.UC itk.US
itk.F
itk.D
itk.complex[itk.D]
itk.complex[itk.F]
itk.Vector[itk.D,2]
itk.Vector[itk.D,3]
itk.Vector[itk.D,4]
itk.SI
itk.UI
itk.UL
itk.ULL
itk.B
itk.FixedArray[itk.F,3]
itk.FixedArray[itk.D,3]
itk.Offset[3]
itk.SymmetricSecondRankTensor[itk.D,3]
itk.SymmetricSecondRankTensor[itk.F,3]
itk.RGBPixel[itk.UC]
itk.RGBAPixel[itk.UC]
itk.Vector[itk.F,2]
itk.Vector[itk.F,3]
itk.Vector[itk.F,4]
itk.CovariantVector[itk.F,2]
itk.CovariantVector[itk.F,3]
itk.CovariantVector[itk.F,4]
itk.SS
itk.UC itk.US
itk.F
itk.D
itk.complex[itk.D]
itk.complex[itk.F]
itk.Vector[itk.D,2]
itk.Vector[itk.D,3]
itk.Vector[itk.D,4]
itk.SI
itk.UI
itk.UL
itk.ULL
itk.B
itk.FixedArray[itk.F,4]
itk.FixedArray[itk.D,4]
itk.Offset[4]
itk.SymmetricSecondRankTensor[itk.D,4]
itk.SymmetricSecondRankTensor[itk.F,4]
itk.Vector[itk.F,1]
itk.Vector[itk.F,1]
itk.Vector[itk.D,2]
itk.Vector[itk.D,3]
itk.Vector[itk.D,4]
itk.Vector[itk.F,1]
itk.CovariantVector[itk.D,2]
itk.CovariantVector[itk.D,3]
itk.CovariantVector[itk.D,4]
itk.CovariantVector[itk.D,2]
itk.CovariantVector[itk.D,3]
itk.CovariantVector[itk.D,4]
itk.CovariantVector[itk.D,2]
itk.CovariantVector[itk.D,3]
itk.CovariantVector[itk.D,4]
*************************************************/ End of output
I see that it says the template type is 'None", so that is likely the source of the error? Why would it say that though, I do have a SS template type specified?
To install ITK, I just used:
pip install itk
Is there something else I could try? I did try the conda version but the results were the same.
You got the syntax wrong. You should use square brackets for template parameters:
C:\Users\Dzenan>python
Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import itk
>>> iType=itk.Image[itk.SS,2]
>>> xType=itk.Image(itk.SS,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Python37\lib\site-packages\itk\support\template_class.py", line 628, in __call__
filter = self.New(*args, **kwargs)
File "C:\Program Files\Python37\lib\site-packages\itk\support\template_class.py", line 733, in New
raise itk.TemplateTypeError(self, input_type)
itk.support.extras.TemplateTypeError: itk.Image is not wrapped for input type `None`.
To limit the size of the package, only a limited number of
types are available in ITK Python. To print the supported
types, run the following command in your python environment:
itk.Image.GetTypes()
Possible solutions:
* If you are an application user:
** Convert your input image into a supported format (see below).
** Contact developer to report the issue.
* If you are an application developer, force input images to be
loaded in a supported pixel type.
e.g.: instance = itk.Image[itk.RGBPixel[itk.UC], int].New(my_input)
* (Advanced) If you are an application developer, build ITK Python yourself and
turned to `ON` the corresponding CMake option to wrap the pixel type or image
dimension you need. When configuring ITK with CMake, you can set
`ITK_WRAP_${type}` (replace ${type} with appropriate pixel type such as
`double`). If you need to support images with 4 or 5 dimensions, you can add
these dimensions to the list of dimensions in the CMake variable
`ITK_WRAP_IMAGE_DIMS`.
Supported input types:
itk.RGBPixel[itk.UC]
itk.RGBAPixel[itk.UC]
itk.Vector[itk.F,2]
itk.Vector[itk.F,3]
itk.Vector[itk.F,4]
itk.CovariantVector[itk.F,2]
itk.CovariantVector[itk.F,3]
itk.CovariantVector[itk.F,4]
itk.SS
itk.UC
...
That code looks fine. Does it produce the exact same error message as before?
It is highly recommended to share code using special code formatting. That makes it easy for others to try it. I have some chest CT images on my computer, but I am not going to retype all that.
Running your program (with paths to my files) gives an error on my home computer:
Traceback (most recent call last):
File "testBinaryThresholdImageFilter.py", line 23, in <module>
reader.SetFileName("P:/Dropbox/Biometric/ChestXray/2013-01-28-AP.dcm")
TypeError: Wrong number or type of arguments for overloaded function 'itkImageFileReaderISS2_SetFileName'.
Possible C/C++ prototypes are:
itkImageFileReaderISS2::SetFileName(itkSimpleDataObjectDecoratorstring const *)
itkImageFileReaderISS2::SetFileName(std::string const &)
I tried this code, the imread function looks ok, but the binary_threshold_image_filter gives the error "cannot find reference ‘binary_threshold_image_filter’ in ‘init.py | init.py’ "
I tried adding .New() to the ImageFileReader but it did not help.