[Insight-developers] SimpleITK - Image::GetPixel methods

Gaëtan Lehmann gaetan.lehmann at jouy.inra.fr
Tue Apr 26 05:36:31 EDT 2011


Hi Brad,

You can't define two versions of GetPixel() in python - only the last  
one will be there.

I would define the GetPixel() method like that:

    def GetPixel(self, *idx):
         """Returns the value of a pixel.

	This method takes 2 parameters in 2D: the x and y position,
	and 3 parameters in 3D: the x, y and z position."""
         pixelID = self.GetPixelIDValue()
         if pixelID == sitkUInt8 || pixelID == sitkLabelUInt8:
            return self.__GetPixelAsUInt8__( idx )
         if pixelID == sitkInt16:
            return self.__GetPixelAsInt16__( idx )
         if pixelID == sitkUInt16 || pixelID == sitkLabelUInt16:
            return self.__GetPixelAsUInt16__( idx )
         if pixelID == sitkInt32:
            return self.__GetPixelAsInt32__( idx )
         if pixelID == sitkUInt32 || pixelID == sitkLabelUInt32:
            return self.__GetPixelAsUInt32__( idx )
         if pixelID == sitkFloat32:
            return self.__GetPixelAsFloat__( idx )
         if pixelID == sitkFloat64:
            return self.__GetPixelAsDouble__( idx )
         if pixelID == sitkUnknown:
            raise Exception("Logic Error: invalid pixel type")
         raise Exception("Unknown pixel type")

and add an __getitem__() method so the pixel value can be accessed  
with a interface which looks like a python list:

    def __getitem__(self, idx):
         """put the same kind of doc than for GetPixel here"""
         return self.GetPixel(*idx)

I guess you'll want to do the same with SetPixel() - the code is a bit  
more complex in that case:

    def SetPixel(self, *arg):
         """some doc!"""
         if len(args) == 2:
            idx = args[:2]
         elif len(args) == 3:
            idx = args[:3]
         else:
            raise Exception("Wrong number of arguments")
         value = args[-1]
         self[args] = value

    def __setitem__(self, idx, value):
         """some doc"""
         # use the specializations of SetPixel here rather than in  
GetPixel() because
         # idx and value are already splitted


It would be interesting to look at the GetPixel() performance - if it  
is a lot slower than the specialized versions, then it may be better  
to not hide the specialized versions, for the users who wants to write  
faster code.

Gaëtan




Le 22 avr. 11 à 22:25, Bradley Lowekamp a écrit :

> Gaetan,
>
> You have provided valuable feedback with several other python  
> related issues and was looking for comments on the current.
>
> Despite it sounding easy, implementing the sitk::Image::GetPixel  
> method is rather challenging due to the numerous types of images  
> underneath. I have a branch in my SimpleITK github, which will add  
> this much needed feature.
>
> The sitk::Image class will be customized for each language to  
> provide a natural interface. This is what I have done for python:
>
> https://github.com/blowekamp/SimpleITK/commit/b821617f013ee18c7cc2ae727eec3d20ad2b5875
>
> I realize now it needs some comments....
>
> It is not clear to me if the index argument should be multiple  
> arguments or an array, or both. As arguments are not strongly typed,  
> I am not certain of how to do methods overloading, based on the  
> arguments. In another methods I used isinstance(), but some frown  
> about that. Do you have a suggestion on how I should overload  
> methods in python? What do you think of this current interface?
>
> Brad
>
> ========================================================
> Bradley Lowekamp
> Lockheed Martin Contractor for
> Office of High Performance Computing and Communications
> National Library of Medicine
> blowekamp at mail.nih.gov
>
>

-- 
Gaëtan Lehmann
Biologie du Développement et de la Reproduction
INRA de Jouy-en-Josas (France)
tel: +33 1 34 65 29 66    fax: 01 34 65 29 09
http://voxel.jouy.inra.fr  http://www.itk.org
http://www.mandriva.org  http://www.bepo.fr

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 203 bytes
Desc: Ceci est une signature ?lectronique PGP
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20110426/1e9108c6/attachment.pgp>


More information about the Insight-developers mailing list