One 、 Image data type and conversion
1、unit8 turn float
2、float turn uint8
Two 、 Color space and its transformation
example :rgb Go to grayscale
Other conversions
example :rgb turn hsv
One 、 Image data type and conversionstay skimage in , A picture is a simple numpy Array , There are many data types for arrays , They can also be converted to each other . These data types and value ranges are shown in the following table :
Data typeRange uint80 to 255uint160 to 65535uint320 to 232float-1 to 1 or 0 to 1int8-128 to 127int16-32768 to 32767int32-231 to 231 - 1The pixel value range of a picture is [0,255], So the default type is unit8, The following code can be used to view the data type :
from skimage import io,dataimg=data.chelsea()print(img.dtype.name)
In the table above , Special note float type , Its scope is [-1,1] or [0,1] Between . After a color picture is converted into a gray image , Its type is determined by unit8 Turned into float
1、unit8 turn floatfrom skimage import data,img_as_floatimg=data.chelsea()print(img.dtype.name)dst=img_as_float(img)print(dst.dtype.name)
Output :
2、float turn uint8uint8
float64
from skimage import img_as_ubyteimport numpy as npimg = np.array([0, 0.5, 1], dtype=float)print(img.dtype.name)dst=img_as_ubyte(img)print(dst.dtype.name)
Output :
float64
uint8
float To unit8, It may cause data loss , So there will be warnings .
In addition to the two most commonly used conversions , There are actually some other type conversions , The following table :
Function nameDescription img_as_floatConvert to 64-bit floating point.img_as_ubyteConvert to 8-bit uint.img_as_uintConvert to 16-bit uint.img_as_intConvert to 16-bit int. Two 、 Color space and its transformationAs mentioned earlier , Except that direct conversion can change the data type , The data type can also be changed through the color space conversion of the image .
The commonly used color space is gray space 、rgb Space 、hsv Space and cmyk Space . After color space conversion , The types of pictures have become float type .
All color space conversion functions , All put in skimage Of color In module .
example :rgb Go to grayscalefrom skimage import io,data,colorimg=data.lena()gray=color.rgb2gray(img)io.imshow(gray)
Other conversions The usage is the same , List the common ones as follows :
skimage.color.rgb2grey(rgb)
skimage.color.rgb2hsv(rgb)
skimage.color.rgb2lab(rgb)
skimage.color.gray2rgb(image)
skimage.color.hsv2rgb(hsv)
skimage.color.lab2rgb(lab)
actually , All the above conversion functions , Can be replaced by a function
skimage.color.convert_colorspace(arr, fromspace, tospace)
It means that you will arr from fromspace Color space to tospace Color space .
example :rgb turn hsvfrom skimage import io,data,colorimg=data.lena()hsv=color.convert_colorspace(img,'RGB','HSV')io.imshow(hsv)
stay color Module's color space conversion function , Another useful function is
skimage.color.label2rgb(arr), You can color the picture according to the tag value . In the future, this function can be used for coloring after image classification .
example : take lena Pictures fall into three categories , Then use the default color to shade the three categories
from skimage import io,data,colorimport numpy as npimg=data.lena()gray=color.rgb2gray(img)rows,cols=gray.shapelabels=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]=2dst=color.label2rgb(labels)io.imshow(dst)
That's all python Details of digital image processing data types and color space conversion , More about python For information about image data types and color spaces, please pay attention to other relevant articles on the software development network !