程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to use Python to process data type and color space conversion

編輯:Python

How do you use it? python Handle data type and color space conversion

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 .

One 、 Image data type and conversion

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 :

Data typeRangeuint80 to 255uint160 to 65535uint320 to 232float-1 to 1 or 0 to 1int8-128 to 127int16-32768 to 32767int32-231 to 231 - 1

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

1、unit8 turn 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

2、float turn uint8

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 nameDescriptionimg_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 transformation

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 .

example :rgb Go to grayscale

from 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 hsv

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved