Scikit-Image图像处理 (3)——二值化

1.读取图像并显示

import numpy as np
import matplotlib.pyplot as plt
from skimage import data


image = data.camera()

#数据类型和最大值
print(image.dtype, image.max())  # uint8 255

plt.imshow(image, cmap='gray', vmin=0, vmax=255)
plt.colorbar()
plt.show()

生成像素直方图

plt.hist(image.flatten(), bins=range(0,257))
plt.show()

 2.图像二值化

(1)手动设置阈值

根据像素直方图,大概估计选取阈值

#阈值设为128,超过128的像素点为白色(1),低于128的像素点为黑色(0)
binimg0 = image>128

plt.imshow(binimg0, cmap='gray')
plt.axis('off')
plt.colorbar()
plt.show()

(2)自动选取阈值  skimage.filters.threshold_otsu

from skimage.filters import threshold_otsu

thresh = threshold_otsu(image)
print("threshold =", thresh)  # threshold = 102

binimg = image>thresh

plt.imshow(binimg, cmap='gray')
plt.show()

 (3)一次尝试所有选择阈值方法   skimage.filters.try_all_threshold

from skimage.filters import try_all_threshold

try_all_threshold(image, figsize=(6,10))

plt.show()

# skimage.filters.thresholding.threshold_isodata
# skimage.filters.thresholding.threshold_li
# skimage.filters.thresholding.threshold_mean
# skimage.filters.thresholding.threshold_minimum
# skimage.filters.thresholding.threshold_otsu
# skimage.filters.thresholding.threshold_triangle
# skimage.filters.thresholding.threshold_yen

 

 (4)局部阈值选定  skimage.filters.threshold_local

from skimage.filters import threshold_local

block_size = 25 #局部区块大小


#设置offset的值时注意像素点的值 (0~255的话10左右,0~1的话0.05左右)
#local_thresh画像整体的阈值
local_thresh = threshold_local(image, block_size, offset=10) 
#local_thresh = threshold_local(image, block_size, offset=-10) #offset值为负时,背景向黑色靠近

binary_local = image > local_thresh

titles = ['original', 'binary_local', 'local_thresh']
imgs = [image, binary_local, local_thresh]

plt.figure(figsize=(12,6))
for i in range(3):
    plt.subplot(1,3,i+1)
    plt.imshow(imgs[i], cmap='gray')
    plt.title(titles[i])
    plt.axis('off')

plt.show()

 局部阈值方法更适合局部背景区分不明显的图像

3.二值图像的处理(膨胀收缩)

膨胀:将黑黑的噪点膨胀出去

收缩:将白色的噪点收缩进去

(1)读取图像

from skimage import io, img_as_ubyte
binimg = io.imread('Images/binimg.png',as_gray=True)
print(binimg.shape, binimg.dtype, binimg.max())  # (200, 200) uint8 255

plt.imshow(binimg, cmap='gray')
plt.show()

(2)膨胀收缩处理

from skimage.morphology import erosion, dilation, disk

fig, ax = plt.subplots(nrows=2, ncols=5, figsize=(16,6))
plt.gray()

ax[0,0].imshow(binimg)
ax[1,0].imshow(binimg)
ax[0,0].set_title('original')

for i in range(1,5):
    eroded = erosion(binimg, disk(i))
    ax[0,i].imshow(eroded)
    ax[0,i].set_title('erode{}'.format(i))
    eroded = dilation(binimg, disk(i))
    ax[1,i].imshow(eroded)
    ax[1,i].set_title('dilate{}'.format(i))

plt.show()

膨胀收缩反复操作可以得到较好效果,比如膨胀2次——收缩4次—— 膨胀2次...

(3) opening closing

from skimage.morphology import opening, closing

fig, ax = plt.subplots(nrows=2, ncols=5, figsize=(16,6))
plt.gray()
ax[0,0].imshow(binimg)
ax[1,0].imshow(binimg)
ax[0,0].set_title('original')

for i in range(1,5):
    opened = opening(binimg,disk(i))
    ax[0,i].imshow(opened)
    ax[0,i].set_title('open{}'.format(i))
    closed = closing(binimg,disk(i))
    ax[1,i].imshow(closed)
    ax[1,i].set_title('close{}'.format(i))

plt.show()

猜你喜欢

转载自blog.csdn.net/weixin_49828565/article/details/129724303
今日推荐