Frustration with 12 bit images

Hi All, I am new to python and image processing in general. I am having trouble working with 12 bit images to do some very simple calculations. Here is some example code:
#!/usr/local/bin/ipython
#import Imaging Library
from PIL import Image
from skimage import io, filters
from skimage import img_as_float
from skimage import exposure
#import pylab for plotting
from pylab import *
Aim = io.imread("A.tiff")
Bim = io.imread("B.tiff")
Cim = io.imread("C.tiff")
Dim = Aim - Cim
Eim = Bim - Cim
#print min and max values of the background subtracted images
print("min %d max %d" % (Aim.min(),Aim.max()))
print("min %d max %d" % (Bim.min(),Bim.max()))
print("min %d max %d" % (Dim.min(),Dim.max()))
print("min %d max %d" % (Eim.min(),Eim.max()))
Input images A,B and C are 12 bit greyscale TIFFs. Output:
min 0 max 4095
min 0 max 4095
min 0 max 65533
min 0 max 65533
The input image data response to min and max make good 12bit sense, but it is totally beyond me how I am getting 16bit responses for D and E. I want to understand what's happening here so that I don't get bitten when I try to do transformations that require me to use floating point math.
thanks, Johnny

Hi Johnny
On 2015-07-29 11:50:59, goettj@gmail.com wrote:
The input image data response to min and max make good 12bit sense, but
it is totally beyond me how I am getting 16bit responses for D and E.
Your images are loaded in with dtype uint16, which has range 0 to 65535 (even though your 12-bit max value is 4095). So, when you subtract a value and it goes below 0, wraparound occurs:
In [2]: np.array([0, 1, 2], dtype=np.uint16) - 5 Out[2]: array([65531, 65532, 65533], dtype=uint16)
One way to solve the problem is to first change the type of your images to np.int32 or np.int64.
Regards Stéfan
participants (2)
-
goettj@gmail.com
-
Stefan van der Walt