图像分割之基于阙值的分割方法
发布时间
阅读量:
阅读量
基于图像的空间分布特性来计算一组适合的灰度阈值,并对图像中的每个像元进行其灰度强度与预设阈值进行对比分析;进而根据比较结果将像元分配至相应的类别中。其中最关键的就是通过特定准则函数求取最佳适应性灰度阈值。
阈值法特别适用于目标和背景占据不同灰度级范围的图。
图像若仅分为两类(即只有一个主要对象和背景),则只须选择一个适当的门槛即可实现单门类分割;但如果遇到需要同时提取多个主要对象的情况,则单门类的划分就可能出现误判,在这种情况下就需要采用多门类的门槛划分才能准确分离每一个所需的主要对象。
阀值分割方法的优缺点:
- 计算便捷且计算效率显著;
- 该方法仅基于像素点灰度值的特性进行分析,在通常情况下不考虑其空间分布特性而导致对噪声敏感的问题以及鲁棒性较差。
- 当背景亮度高于物体时难以处理,并且这一问题是较为突出的问题

好了,废话不多说,直接上代码
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
img1 = cv.imread('E:/BaoSteel/2013.11.1/1-1-0-3(50).jpg')
img = cv.cvtColor(img1, cv.COLOR_BGR2GRAY)
# ret, thresh = cv.threshold(gray,0,255,cv.THRESH_BINARY_INV+cv.THRESH_OTSU)
# img = cv.medianBlur(img, 5)
ret, th1 = cv.threshold(img, 67, 255, cv.THRESH_BINARY)
print(ret)
th2 = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 11, 2)
th3 = cv.adaptiveThreshold(img, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 11, 2)
# Otsu's thresholding after Gaussian filtering
blur = cv.GaussianBlur(img, (5, 5), 0)
ret4, th4 = cv.threshold(blur, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
# Otsu's thresholding
ret2, th5 = cv.threshold(img, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
# Otsu's thresholding after Gaussian filtering
blur = cv.GaussianBlur(img, (5, 5), 0)
ret3, th6 = cv.threshold(blur, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
titles = ['Original Image', 'Original Image1', 'Global Thresholding (v = 67)',
'Adaptive Mean Thresholding', 'Adaptive Gaussian Thresholding', 'Adaptive Blur Gaussian Thresholding',
"Otsu's Thresholding1", "Otsu's Thresholding"]
images = [img1, img, th1, th2, th3, th4, th5, th6]
for i in range(8):
plt.subplot(2, 4, i + 1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]), plt.yticks([])
plt.show()
结果如下:

好吧,效果有点差。。。
全部评论 (0)
还没有任何评论哟~
