基于阈值的7种图像分割方法以及Python实现
阈值图像分割的7种方法
1. 什么是阈值分割
阈值分割是根据图像的灰度特征按照设定的阈值将图像分割成不同的子区域。简单的理解就是先将图像进行灰度处理,然后根据灰度值和设定的灰度范围将图像灰度分类。比如0-128的是一类,129-255是一类。
根据不同的分类方法,阈值分割有以下7种方法:
- 固定阈值分割
- 直方图双峰法
- 迭代阈值图像分割
- 自适应阈值图像分割
- 大津法 OTSU
- 均值法
- 最佳阈值
2. 固定阈值分割
固定阈值分割是最简单的阈值分割方法,其方法就是将灰度值大于某一阈值的像素点置为255,而小于等于该阈值的点设置为0。这是最简单的图像分割方法,适用范围很窄。对于比较简单的图像有较好的效果。对应的CV2函数为cv2.threshold(src, thresh, maxval,cv2.THRESH_BINARY)。用numpy实现此功能非常简单,只需要1行代码:
return np.where(((img>thresh) & (img<maxval)),255,0)
np.where的第一个参数是条件,可以设置多条件。第二个是条件成立时,numpy数组的取值,而第三个参数是不成立的取值。
完成的比较代码如下:
# coding:utf8
import cv2
import numpy as np
import matplotlib.pyplot as plt
def fix_threshold(img, thresh, maxval=255):
return np.where(((img > thresh) & (img < maxval)), 255, 0)
img = cv2.imread('xk.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
ret, th = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
fix = fix_threshold(img_gray, 127, 255)
plt.subplot(131), plt.imshow(img_gray, cmap='gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(132), plt.imshow(th, cmap='gray')
plt.title('CV2 Image1'), plt.xticks([]), plt.yticks([])
plt.subplot(133), plt.imshow(fix, cmap='gray')
plt.title('Fix Image2'), plt.xticks([]), plt.yticks([])
plt.show()
3.灰度直方图双峰法
灰度直方图通常双峰属性(bimodal,一个前景峰值,另一个为背景峰值)。两个峰值之间的最小值可以认为是最优二值化的分界点。
算法如下:
(1)查找直方图最大值firstPeak
(2)通过下面的公式结算第二峰:
s
e
c
o
n
d
P
e
a
k
=
max
x
[
(
x
−
f
r
i
s
t
P
e
a
k
)
2
∗
h
i
s
t
(
x
)
]
secondPeak = maxlimits_{x}[(x-fristPeak)^2*hist(x)]
secondPeak=xmax[(x−fristPeak)2∗hist(x)]
(3)双峰之间的灰度最小值为分界点(需要考虑最大峰值的灰度数值可能小于第二大峰值的灰度值)
此方法得到的阈值与CV2中threshold函数得到的阈值并不是完全一致的。本方法较为简单,对于处理简单的图像比较有效,但是对于复杂的图片,比如遥感图片效果就很差。后面的文章会增加解读遥感图片的部分。
# coding:utf8
import cv2
import numpy as np
from matplotlib import pyplot as plt
def GrayHist(img):
grayHist = np.zeros(256,dtype=np.uint64)
for v in range(256):
grayHist[v] = np.sum(img==v)
return grayHist
def threshTwoPeaks(image):
# 计算灰度直方图
hist = GrayHist(image)
# 寻找灰度直方图的最大峰值对应的灰度值
maxLoc = np.where(hist == np.max(hist)) #maxLoc 中存放的位置
firstPeak = maxLoc[0][0]
# 寻找灰度直方图的第二个峰值对应的灰度值
elementList = np.arange(256,dtype = np.uint64)
measureDists = np.power(elementList - firstPeak,2) * hist
maxLoc2 = np.where(measureDists == np.max(measureDists))
secondPeak = maxLoc2[0][0]
# 找到两个峰值之间的最小值对应的灰度值,作为阈值
thresh = 0
if secondPeak > firstPeak:
firstPeak,secondPeak=secondPeak,firstPeak
temp = hist[secondPeak:firstPeak]
minloc = np.where(temp == np.min(temp))
thresh = secondPeak + minloc[0][0] + 1
# 找到阈值之后进行阈值处理,得到二值图
threshImage_out = image.copy()
# 大于阈值的都设置为255
threshImage_out[threshImage_out > thresh] = 255
# 小于阈值的都设置为0
threshImage_out[threshImage_out <= thresh] = 0
return thresh, threshImage_out
if __name__ == "__main__":
img = cv2.imread('dt.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th, img_new = threshTwoPeaks(img_gray)
th1,img_new_1 = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_TRIANGLE)
print(th,th1)
plt.subplot(131), plt.imshow(img_gray, cmap='gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(132), plt.imshow(img_new, cmap='gray')
plt.title('Image'), plt.xticks([]), plt.yticks([])
plt.subplot(133), plt.imshow(img_new_1, cmap='gray')
plt.title('CV2 Image1'), plt.xticks([]), plt.yticks([])
plt.show()
相关文章
- 【技术种草】cdn+轻量服务器+hugo=让博客“云原生”一下
- CLB运维&运营最佳实践 ---访问日志大洞察
- vnc方式登陆服务器
- 轻松学排序算法:眼睛直观感受几种常用排序算法
- 十二个经典的大数据项目
- 为什么使用 CDN 内容分发网络?
- 大数据——大数据默认端口号列表
- Weld 1.1.5.Final,JSR-299 的框架
- JavaFX 2012:彻底开源
- 提升as3程序性能的十大要点
- 通过凸面几何学进行独立于边际的在线多类学习
- 利用行动影响的规律性和部分已知的模型进行离线强化学习
- ModelLight:基于模型的交通信号控制的元强化学习
- 浅谈Visual Source Safe项目分支
- 基于先验知识的递归卡尔曼滤波的代理人联合状态和输入估计
- 结合网络结构和非线性恢复来提高声誉评估的性能
- 最佳实践丨云开发CloudBase多环境管理实践
- TimeVAE:用于生成多变量时间序列的变异自动编码器
- 具有线性阈值激活的神经网络:结构和算法
- 内网渗透之横向移动 -- 从域外向域内进行密码喷洒攻击