Casting image


(Marco Festugato) #1

Hello, I don’t understand if when I cast an image from one type to another (for example using itk Castimage Filter) , I also change the values of the pixels.

Example: if I cast an image from ‘signed short’ (range: 0 to 255) to ‘double’, will the range remain the same? And if not, how can I do in order to maintain the same range?

Thanks in advance for any help :blush:


(Bradley Lowekamp) #2

The itk::ShiftScaleImageFilter will do what you want.
You set the correct input and output pixel types in the typedef, then compute the desired shift and scale which maps the input range to the desired output range.


(Pablo Hernandez-Cerdan) #3

For the case you are describing, double encloses signed short, so you don’t need to perform any extra transform. The double image after the cast will have the same values in the same range, from 0.0 to 255.0.


(Dženan Zukić) #4

Cast filter was meant for casts which involve little or no loss, e.g. from float to double or vice versa, or from short to int, or from int to short once you somehow know values will fit into short etc.

If you go from short to double, values will not be changed because double can exactly represent all values a short can have. And what kind of range change would you expect to see by such a cast? 0…255 is maximum range of unsigned char, not short. And would you expect to end with a range of 0.0-1.0, or 0.0-1000.0, or 0.0-DBL_MAX? And why?


(Marco Festugato) #5

Ty all for your quick and helpful answers. Yes probably my example wasn’t satisfying… What I want to do is convert my dicom images from signed short to double because, for what I know, signed short is an integer type, so I want to avoid the errors I will do if I perform mathematical operations (ex: filtering).
So, what I wanna do is converting my images from signed short to double, scale everything between 0 - 1, perform some filtering and then convert again the images from double to signed short (i have to write them in the dicom format)… Will these conversions change the range of my images? I think yes because if I go from double to short (that for what I know is an integer type) it will approximate the value to the nearest integer right?
Thanks in advance for helping me!


(Dženan Zukić) #6

If you use only ITK’s regular filters, there is absolutely no need to convert values to [0,1] range. Additionally, if you will be writing the filtered output to the same pixel format you originally read it in, there is not much benefit if you go to double as an internal pixel format.

For example, if the original pixel’s value is 7, but after smoothing in double it is 8.17, when you convert it back to integral pixel type it will become 8. If you skip conversion to double, smoothing filter will do conversion back to integral type and directly calculate 8.

Edit: if you have a lot of internal processing steps, then it makes a little more sense to use a floating point type. But I recommend float, not double. Doing computations on floats is faster, and they take up half as much memory.


(Marco Festugato) #7

Thank u @dzenanz for helping me once again. I understood that in this particular case casting would not be of any help. I’ll try something else!
Thank u all again for your help :blush: