Python+OpenCV实现阈值分割的方法详解
作者:用余生去守护 时间:2023-08-13 02:24:00
一、全局阈值
原图:
整幅图采用一个阈值,与图片的每一个像素灰度进行比较,重新赋值;
1.效果图
2.源码
import cv2
import matplotlib.pyplot as plt
#设定阈值
thresh=130
#载入原图,并转化为灰度图像
img_original=cv2.imread(r'E:\py\python3.7\test2\test14yuzhi\cell.png',0)
img_original=cv2.resize(img_original,(0,0),fx=0.3,fy=0.3)
#采用5种阈值类型(thresholding type)分割图像
retval1,img_binary=cv2.threshold(img_original,thresh,255,cv2.THRESH_BINARY)
retval2,img_binary_invertion=cv2.threshold(img_original,thresh,255,cv2.THRESH_BINARY_INV)
retval3,img_trunc=cv2.threshold(img_original,thresh,255,cv2.THRESH_TRUNC)
retval4,img_tozero=cv2.threshold(img_original,thresh,255,cv2.THRESH_TOZERO)
retval5,img_tozero_inversion=cv2.threshold(img_original,thresh,255,cv2.THRESH_TOZERO_INV)
#采用plt.imshow()显示图像
imgs=[img_original,img_binary,img_binary_invertion,img_trunc,img_tozero,img_tozero_inversion]
titles=['original','binary','binary_inv','trunc','tozero','tozero_inv']
for i in range(6):
plt.subplot(2,3,i+1)
plt.imshow(imgs[i],'gray')
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.show()
二、滑动改变阈值(滑动条)
1.效果图
2.源码
代码如下(示例):
import cv2
import numpy as np
import matplotlib.pyplot as plt
#载入原图,转化为灰度图像,并通过cv2.resize()等比调整图像大小
img_original=cv2.imread(r'E:\py\python3.7\test2\test14yuzhi\cell.png',0)
img_original=cv2.resize(img_original,(0,0),fx=0.3,fy=0.3)
#初始化阈值,定义全局变量imgs
thresh=130
imgs=0
#创建滑动条回调函数,参数thresh为滑动条对应位置的数值
def threshold_segmentation(thresh):
#采用5种阈值类型(thresholding type)分割图像
retval1,img_binary=cv2.threshold(img_original,thresh,255,cv2.THRESH_BINARY)
retval2,img_binary_invertion=cv2.threshold(img_original,thresh,255,cv2.THRESH_BINARY_INV)
retval3,img_trunc=cv2.threshold(img_original,thresh,255,cv2.THRESH_TRUNC)
retval4,img_tozero=cv2.threshold(img_original,thresh,255,cv2.THRESH_TOZERO)
retval5,img_tozero_inversion=cv2.threshold(img_original,thresh,255,cv2.THRESH_TOZERO_INV)
#由于cv2.imshow()显示的是多维数组(ndarray),因此我们通过np.hstack(数组水平拼接)
#和np.vstack(竖直拼接)拼接数组,达到同时显示多幅图的目的
img1=np.hstack([img_original,img_binary,img_binary_invertion])
img2=np.hstack([img_trunc,img_tozero,img_tozero_inversion])
global imgs
imgs=np.vstack([img1,img2])
#新建窗口
cv2.namedWindow('Images')
#新建滑动条,初始位置为130
cv2.createTrackbar('threshold value','Images',130,255,threshold_segmentation)
#第一次调用函数
threshold_segmentation(thresh)
#显示图像
while(1):
cv2.imshow('Images',imgs)
if cv2.waitKey(1)==ord('q'):
break
cv2.destroyAllWindows()
三、自适应阈值分割
1.效果图
2.源码
代码如下(示例):
import cv2
import matplotlib.pyplot as plt
#载入原图
img_original=cv2.imread(r'E:\py\python3.7\test2\test14yuzhi\cell.png',0)
#全局阈值分割
retval,img_global=cv2.threshold(img_original,130,255,cv2.THRESH_BINARY)
#自适应阈值分割
img_ada_mean=cv2.adaptiveThreshold(img_original,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,15,3)
img_ada_gaussian=cv2.adaptiveThreshold(img_original,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,15,3)
imgs=[img_original,img_global,img_ada_mean,img_ada_gaussian]
titles=['Original Image','Global Thresholding(130)','Adaptive Mean','Adaptive Guassian',]
#显示图片
for i in range(4):
plt.subplot(2,2,i+1)
plt.imshow(imgs[i],'gray')
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.show()
3.GaussianBlur()函数去噪
代码如下(示例):
import cv2
import matplotlib.pyplot as plt
#载入原图
img_original=cv2.imread(r'E:\py\python3.7\test2\test14yuzhi\cell.png',0)
#高斯滤波
img_blur=cv2.GaussianBlur(img_original,(13,13),13) #根据情况修改参数
#自适应阈值分割
img_thresh=cv2.adaptiveThreshold(img_original,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,15,3)
img_thresh_blur=cv2.adaptiveThreshold(img_blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,15,3)
#显示图像
imgs=[img_thresh,img_thresh_blur]
titles=['img_thresh','img_thresh_blur']
for i in range(2):
plt.subplot(1,2,i+1)
plt.imshow(imgs[i],'gray')
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.show()
四、参数解释
1.cv2.threshold(src, thresh, maxval, type)
参数:
src:输入的图像
thresh:图像分割所用的阈值(threshold value)
maxval:当阈值类型(thresholding type)采用cv2.THRESH_BINARY和cv2.THRESH_BINARY_INV时像素点被赋予的新值
type:介绍6种类型:
cv2.THRESH_BINARY(当图像某点像素值大于thresh(阈值)时赋予maxval,反之为0。注:最常用)
cv2.THRESH_BINARY_INV(当图像某点像素值小于thresh时赋予maxval,反之为0)
cv2.THRESH_TRUNC(当图像某点像素值大于thresh时赋予thresh,反之不变。注:虽然maxval没用了,但是调用函数不能省略)
cv2.THRESH_TOZERO(当图像某点像素值小于thresh时赋予0,反之不变。注:同上)
cv2.THRESH_TOZERO_INV(当图像某点像素值大于thresh时赋予0,反之不变。注:同上)
cv2.THRESH_OTSU(该方法自动寻找最优阈值,并返回给retval,见下文)
返回值:
retval:设定的thresh值,或者是通过cv2.THRESH_OTSU算出的最优阈值
dst:阈值分割后的图像
来源:https://blog.csdn.net/qq_45365214/article/details/124750672
标签:Python,OpenCV,阈值,分割
0
投稿
猜你喜欢
Django中的静态文件管理过程解析
2021-01-21 03:09:14
一千行的MySQL学习笔记汇总
2024-01-27 22:04:42
PyCharm 安装与使用配置教程(windows,mac通用)
2023-08-21 01:15:39
利用Python如何实时检测自身内存占用
2023-01-11 03:12:02
Python BeautifulSoup基本用法详解(通过标签及class定位元素)
2022-11-06 10:50:49
可用于监控 mysql Master Slave 状态的python代码
2023-01-05 07:39:30
oracle日志操作模式(归档模式和非归档模式的利与弊)
2024-01-24 18:01:36
永恒之蓝实战教程之Mac通过Metasploit攻击Server2008的详细过程
2022-08-01 05:41:26
在ASP.NET 2.0中操作数据之三十:格式化DataList和Repeater的数据
2023-07-22 20:15:21
opencv-python图像增强解读
2022-10-10 04:29:16
MySQL学习笔记5:修改表(alter table)
2024-01-23 23:01:16
Python实现TOPSIS分析法的示例代码
2021-05-09 19:32:47
Dreamweaver4探谜系列(2)
2010-05-13 12:08:00
Python脚本导出为exe程序的方法
2022-08-22 21:33:05
面试官问订单ID是如何生成的?难道不是MySQL自增主键
2024-01-24 00:36:24
Python pandas轴旋转stack和unstack的使用说明
2021-08-10 21:18:34
MySQL使用IF语句及用case语句对条件并结果进行判断
2024-01-14 14:30:27
Python数组变形的几种实现方法
2021-08-20 09:30:47
Python对两个有序列表进行合并和排序的例子
2022-06-07 00:11:37
Python可视化神器pyecharts绘制地理图表
2023-11-28 21:26:22