ENH: Add img_as_ubyte.

This commit is contained in:
Stefan van der Walt
2011-09-27 01:16:35 -07:00
parent 5ec5c55b1a
commit 0b7df2626a
3 changed files with 29 additions and 3 deletions
+2
View File
@@ -0,0 +1,2 @@
from dtype import *
+23 -1
View File
@@ -1,7 +1,7 @@
from __future__ import division
import numpy as np
__all__ = ['img_as_float', 'img_as_int', 'img_as_uint']
__all__ = ['img_as_float', 'img_as_int', 'img_as_uint', 'img_as_ubyte']
from .. import get_log
log = get_log('dtype_converter')
@@ -146,3 +146,25 @@ def img_as_int(image):
"""
prec_loss = (np.float32, np.float64, np.uint16)
return _convert(image, np.int16, prec_loss)
def img_as_ubyte(image):
"""Convert an image to 8-bit unsigned integer format.
Parameters
----------
image : ndarray
Input image.
Returns
-------
out : ndarray of ubyte (uint8)
Output image.
Notes
-----
If the input data-type is positive-only (e.g., uint16), then
the output image will still only have positive values.
"""
prec_loss = (np.float32, np.float64, np.uint16, np.int16, np.int8)
return _convert(image, np.ubyte, prec_loss)
+4 -2
View File
@@ -1,6 +1,7 @@
import numpy as np
from numpy.testing import assert_equal
from scikits.image import img_as_int, img_as_float, img_as_uint
from scikits.image import img_as_int, img_as_float, \
img_as_uint, img_as_ubyte
dtype_range = {np.uint8: (0, 255),
np.uint16: (0, 65535),
@@ -20,7 +21,8 @@ def test_range():
for (f, dt) in [(img_as_int, np.int16),
(img_as_float, np.float64),
(img_as_uint, np.uint16)]:
(img_as_uint, np.uint16),
(img_as_ubyte, np.ubyte)]:
y = f(x)
omin, omax = dtype_range[dt]