This article mainly introduces “ How do you use it? python Handle data type and color space conversion ” Knowledge about , Xiaobian shows you the operation process through practical cases , The operation method is simple and fast , Practical , Hope this article “ How do you use it? python Handle data type and color space conversion ” The article can help you solve problems .
stay 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 :
The 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
from skimage import data,img_as_floatimg=data.chelsea()print(img.dtype.name)dst=img_as_float(img)print(dst.dtype.name)
Output :
uint8
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 :
As 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 .
from skimage import io,data,colorimg=data.lena()gray=color.rgb2gray(img)io.imshow(gray)
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 .
from 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)
About “ How do you use it? python Handle data type and color space conversion ” That's all for , Thanks for reading . If you want to know more about the industry , You can pay attention to the Yisu cloud industry information channel , Xiaobian will update different knowledge points for you every day .