# python数字图像处理（4）：图像数据类型及颜色空间转换

Data typeRange
uint8 0 to 255
uint16 0 to 65535
uint32 0 to 232
float -1 to 1 or 0 to 1
int8 -128 to 127
int16 -32768 to 32767
int32 -231 to 231 - 1

from skimage import io,data
img=data.chelsea()
print(img.dtype.name)

1、unit8转float

from skimage import data,img_as_float
img=data.chelsea()
print(img.dtype.name)
dst=img_as_float(img)
print(dst.dtype.name)

uint8
float64

2、float转uint8

from skimage import img_as_ubyte
import numpy as np
img = np.array([0, 0.5, 1], dtype=float)
print(img.dtype.name)
dst=img_as_ubyte(img)
print(dst.dtype.name)

float64
uint8

float转为unit8，有可能会造成数据的损失，因此会有警告提醒。

Function nameDescription
img_as_float Convert to 64-bit floating point.
img_as_ubyte Convert to 8-bit uint.
img_as_uint Convert to 16-bit uint.
img_as_int Convert to 16-bit int.

from skimage import io,data,color
img=data.lena()
gray=color.rgb2gray(img)
io.imshow(gray)

skimage.color.rgb2grey(rgb)

skimage.color.rgb2hsv(rgb)

skimage.color.rgb2lab(rgb)

skimage.color.gray2rgb(image)

skimage.color.hsv2rgb(hsv)

skimage.color.lab2rgb(lab)

实际上，上面的所有转换函数，都可以用一个函数来代替

skimage.color.convert_colorspace(arrfromspacetospace)

from skimage import io,data,color
img=data.lena()
hsv=color.convert_colorspace(img,'RGB','HSV')
io.imshow(hsv)

skimage.color.label2rgb(arr), 可以根据标签值对图片进行着色。以后的图片分类后着色就可以用这个函数。

from skimage import io,data,color
import numpy as np
img=data.lena()
gray=color.rgb2gray(img)
rows,cols=gray.shape
labels=np.zeros([rows,cols])
for i in range(rows):
for j in range(cols):
if(gray[i,j]<0.4):
labels[i,j]=0
elif(gray[i,j]<0.75):
labels[i,j]=1
else:
labels[i,j]=2
dst=color.label2rgb(labels)
io.imshow(dst)

posted @ 2016-01-11 19:29  denny402  阅读(46100)  评论(2编辑  收藏