scipy imsave saves wrong values

后端 未结 1 1302
误落风尘
误落风尘 2021-01-18 23:16

I\'m trying to write code that will produce disparity maps using numpy and scipy, but the values that I store in my numpy array for my images are completely different from t

相关标签:
1条回答
  • 2021-01-18 23:48

    The data gets rescaled when the dtype of the array is changed from np.float64 (the data type of disp12im) to the 8 bit values stored in the image.

    To avoid this, convert your image to data type np.uint8 before giving it to imsave:

    misc.imsave('disp121.pgm', disp12im.astype(np.uint8))
    

    For example, I'll save this x as a PGM image:

    In [13]: x
    Out[13]: 
    array([[  1.,   3.,   5.],
           [ 21.,  23.,  25.]])
    
    In [14]: x.dtype
    Out[14]: dtype('float64')
    

    Save x unaltered, and then read it back:

    In [15]: imsave('foo.pgm', x)
    
    In [16]: imread('foo.pgm')
    Out[16]: 
    array([[  0,  21,  42],
           [212, 234, 255]], dtype=uint8)
    

    The values have been scaled up to the full 8-bit range.

    Instead, convert x to np.uint8 before saving, and then read it back:

    In [17]: imsave('foo.pgm', x.astype(np.uint8))
    
    In [18]: imread('foo.pgm')
    Out[18]: 
    array([[ 1,  3,  5],
           [21, 23, 25]], dtype=uint8)
    
    0 讨论(0)
提交回复
热议问题