Scikit-Image图像处理 (1)——读取图像

1.读取文件并显示

from skimage import io

colorimg = io.imread('Images/Lenna.png') #不处理直接读取彩色图像
print("矩阵次元数:", colorimg.ndim)
print("矩阵形状:", colorimg.shape)
print("数据类型:", colorimg.dtype)
print("最大値:", colorimg.max())
print("最小値:", colorimg.min())
print("平均値:", colorimg.mean())

# 矩阵次元数: 3
# 矩阵形状: (512, 512, 3)
# 数据类型: uint8
# 最大値: 255
# 最小値: 3
# 平均値: 128.22837575276694

显示图像

plt.imshow(colorimg)

用figsize指定图像的大小

figsize = (宽度, 高)   单位为英尺 

plt.figure(figsize=(8,8))
plt.imshow(colorimg)

用最大值255减,进行正负反转

plt.imshow(255-colorimg)

读取灰度图像

添加 as_gray=True 作为 imread 选项以读取灰度(1 通道)图像。

读取为灰度时,转换为float64的二维数组数据:浮点型64位(最小值0,最大值1)

grayimg = io.imread('Images/Lenna.png', as_gray=True) #读取灰度图像
print("配列次元数:", grayimg.ndim)
print("配列形状:", grayimg.shape)
print("データ型:", grayimg.dtype)
print("最大値:", grayimg.max())
print("最小値:", grayimg.min())
print("平均値:", grayimg.mean())

# 配列次元数: 2
# 配列形状: (512, 512)
# データ型: float64
# 最大値: 0.9654356862745097
# 最小値: 0.07254666666666666
# 平均値: 0.4578778306070963
plt.imshow(grayimg)
plt.colorbar() #显示色彩条

要正确显示灰度图像:

  • 为颜色图 (cmap) 指定“灰色”
  • 指定 0 作为最小值 (vmin)
  • 必须将 1 指定为最大值 (vmax)
plt.imshow(grayimg, cmap='gray', vmin=0, vmax=1)
plt.colorbar()

 并列显示:subplot

多个图像可以与subplot并排显示(行数,列数,顺序)

可以使用 plt.axis('off') 关闭坐标轴、

plt.figure(figsize=(10,6))

plt.subplot(121)
plt.imshow(colorimg)
plt.axis('off')

plt.subplot(122)
plt.imshow(grayimg, cmap='gray')
plt.axis('off')

读取的彩色图像可以通过rgb2gray变换成灰度图像(程序中同时需要彩色图像和灰度图像的情况下)。变换的灰度图像为64字节浮点小数型(最小值0,最大值1)

from skimage.color import rgb2gray

grayimg2 = rgb2gray(colorimg)
print(grayimg2.shape, grayimg2.dtype, grayimg2.max())

# (512, 512) float64 0.9654356862745097
plt.figure(figsize=(12,4))

plt.subplot(131)
plt.imshow(grayimg2) #单通道的情况下,默认cmap='viridis'
plt.colorbar()

plt.subplot(132)
plt.imshow(grayimg2, cmap='gray', vmin=0, vmax=1)
plt.colorbar()

plt.subplot(133)
plt.imshow(grayimg2, cmap='hot')
plt.colorbar()

 从彩色图像中抽出rgb三通道

red = colorimg[:,:,0]
green = colorimg[:,:,1]
blue = colorimg[:,:,2]

plt.figure(figsize=(12,4))
plt.subplot(131)
plt.title('R')
plt.imshow(red, cmap='gray', vmin=0, vmax=255)
plt.axis('off')
plt.colorbar()

plt.subplot(132)
plt.title('G')
plt.imshow(green, cmap='gray', vmin=0, vmax=255)
plt.axis('off')
plt.colorbar()

plt.subplot(133)
plt.title('B')
plt.imshow(blue, cmap='gray', vmin=0, vmax=255)
plt.axis('off')
plt.colorbar()

 色彩空间变换

从RGB三原色转为H(色相)S(彩度)V

from skimage.color import rgb2hsv

hsvimg = rgb2hsv(colorimg)

himg = hsvimg[:,:,0]
simg = hsvimg[:,:,1]
vimg = hsvimg[:,:,2]

plt.figure(figsize=(8,8))
plt.subplot(221)
plt.imshow(colorimg)
plt.axis('off')

plt.subplot(222)
plt.title('H')
plt.imshow(himg, cmap='hsv', vmin=0, vmax=1)
plt.axis('off')
plt.colorbar()

plt.subplot(223)
plt.title('S')
plt.imshow(simg, cmap='gray')
plt.axis('off')
plt.colorbar()

plt.subplot(224)
plt.title('V')
plt.imshow(vimg, cmap='gray', vmin=0, vmax=1)
plt.axis('off')
plt.colorbar()

from skimage.color import hsv2rgb

tmpimg = hsvimg.copy() #复制画像
tmpimg[:,:,1] = tmpimg[:,:,1]/3 #彩度/3

procimg = hsv2rgb(tmpimg) #HSV--RGB

plt.imshow(procimg)
plt.axis('off')

 

猜你喜欢

转载自blog.csdn.net/weixin_49828565/article/details/129710288