mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-24 13:20:43 +08:00
ENH: Add img_as_ubyte.
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
from dtype import *
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user