OpenCV半小时掌握基本操作之分水岭算法
作者:我是小白呀 发布时间:2023-04-10 11:24:51
标签:OpenCV,分水岭算法,python
【OpenCV】⚠️高手勿入! 半小时学会基本操作 ⚠️ 分水岭算法
概述
OpenCV 是一个跨平台的计算机视觉库, 支持多语言, 功能强大. 今天小白就带大家一起携手走进 OpenCV 的世界.
分水岭算法
分水岭算法 (Watershed Algorithm) 是一种图像区域分割算法. 在分割的过程中, 分水岭算法会把跟临近像素间的相似性作为重要的根据.
分水岭分割流程:
读取图片
转换成灰度图
二值化
距离变换
寻找种子
生成 Marker
分水岭变换
距离变换
距离变换 (Distance Transform)通过计算图像中非零像素点到最近像素的距离, 实现了像素与图像区域的距离变换.
连通域
连通域 (Connected Components) 指的是图像中具有相同像素且位置相邻的前景像素点组成的图像区域.
格式:
cv2.connectedComponents(image, labels=None, connectivity=None, ltype=None)
参数:
image: 输入图像, 必须是 uint8 二值图像
labels 图像上每一像素的标记, 用数字 1, 2, 3 表示
分水岭
算法会根据 markers 传入的轮廓作为种子, 对图像上其他的像素点根据分水岭算法规则进行判断, 并对每个像素点的区域归属进行划定. 区域之间的分界处的值被赋值为 -1.
格式:
cv2.watershed(image, markers)
参数:
image: 输入图像
markers: 种子, 包含不同区域的轮廓
代码实战
import numpy as np
import cv2
from matplotlib import pyplot as plt
def watershed(image):
"""分水岭算法"""
# 卷积核
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
# 均值迁移滤波
blur = cv2.pyrMeanShiftFiltering(image, 10, 100)
# 转换成灰度图
image_gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
# 二值化
ret1, thresh1 = cv2.threshold(image_gray, 0, 255, cv2.THRESH_OTSU)
# 开运算
open = cv2.morphologyEx(thresh1, cv2.MORPH_OPEN, kernel, iterations=2)
# 膨胀
dilate = cv2.dilate(open, kernel, iterations=3)
# 距离变换
dist = cv2.distanceTransform(dilate, cv2.DIST_L2, 3)
dist = cv2.normalize(dist, 0, 1.0, cv2.NORM_MINMAX)
print(dist.max())
# 二值化
ret2, thresh2 = cv2.threshold(dist, dist.max() * 0.6, 255, cv2.THRESH_BINARY)
thresh2 = np.uint8(thresh2)
# 分水岭计算
unknown = cv2.subtract(dilate, thresh2)
ret3, component = cv2.connectedComponents(thresh2)
print(ret3)
# 分水岭计算
markers = component + 1
markers[unknown == 255] = 0
result = cv2.watershed(image, markers=markers)
image[result == -1] = [0, 0, 255]
# 图片展示
image_show((image, blur, image_gray, thresh1, open, dilate), (dist, thresh2, unknown, component, markers, image))
return image
def image_show(graph1, graph2):
"""绘制图片"""
# 图像1
original, blur, gray, binary1, open, dilate = graph1
# 图像2
dist, binary2, unknown, component, markers, result = graph2
f, ax = plt.subplots(3, 2, figsize=(12, 16))
# 绘制子图
ax[0, 0].imshow(cv2.cvtColor(original, cv2.COLOR_BGR2RGB))
ax[0, 1].imshow(cv2.cvtColor(blur, cv2.COLOR_BGR2RGB))
ax[1, 0].imshow(gray, "gray")
ax[1, 1].imshow(binary1, "gray")
ax[2, 0].imshow(open, "gray")
ax[2, 1].imshow(dilate, "gray")
# 标题
ax[0, 0].set_title("original")
ax[0, 1].set_title("image blur")
ax[1, 0].set_title("image gray")
ax[1, 1].set_title("image binary1")
ax[2, 0].set_title("image open")
ax[2, 1].set_title("image dilate")
plt.show()
f, ax = plt.subplots(3, 2, figsize=(12, 16))
# 绘制子图
ax[0, 0].imshow(dist, "gray")
ax[0, 1].imshow(binary2, "gray")
ax[1, 0].imshow(unknown, "gray")
ax[1, 1].imshow(component, "gray")
ax[2, 0].imshow(markers, "gray")
ax[2, 1].imshow(cv2.cvtColor(result, cv2.COLOR_BGR2RGB))
# 标题
ax[0, 0].set_title("image distance")
ax[0, 1].set_title("image binary2")
ax[1, 0].set_title("image unknown")
ax[1, 1].set_title("image component")
ax[2, 0].set_title("image markers")
ax[2, 1].set_title("result")
plt.show()
if __name__ == "__main__":
# 读取图片
image = cv2.imread("coin.jpg")
# 分水岭算法
result = watershed(image)
# 保存结果
cv2.imwrite("result.jpg", result)
输出结果:
来源:https://blog.csdn.net/weixin_46274168/article/details/119399447
0
投稿
猜你喜欢
- Python int() 函数描述int() 函数用于将一个字符串或数字转换为整型。语法以下是 int() 方法的语法:class int(
- Select字句在逻辑上是SQL语句最后进行处理的最后一步,所以,以下查询会发生错误:SELECT YEAR(OrderDate) AS O
- 最近ChatGpt异常火热,访问网站经常会出现1020的错误代码,或无法访问。大概率是因为服务器压力大,网关返回的1020错误码,只能等待官
- 前言本文介绍的是DDCTF第五题,绕过未知字段名的技巧,这里拿本机来操作了下,思路很棒也很清晰,分享给大家,下面来看看详细的介绍:实现思路题
- 系统中对Razor的支持包括两部分,其中之一就是自定义RazorViewEngine一、自定义RazorViewEngine在Global.
- 用python实现FTP文件传输,包括服务器端和客户端,要求(1)客户端访问服务器端要有一个验证功能(2)可以有多个客户端访问服务器端(3)
- 今天安装Django的时候遇到了python版本冲突,找不到python路径,所以又重新安装了一个python3.6.5安装完之后,突然发现
- 效果如下:代码如下:<!DOCTYPE html><html> <head> <meta char
- javascript过滤数组重复元素的实现方法 以下是在
- Numpy通过观察Python的自有数据类型,我们可以发现Python原生并不提供多维数组的操作,那么为了处理矩阵,就需要使用第三方提供的相
- python进行矩阵运算的方法:1、矩阵相乘>>>a1=mat([1,2]); >>>a2
- 本文转自微信公众号:"算法与编程之美" Python用HBuilder创建交流社区APP 基于P
- 引言“ 这是MySQL系列笔记的第十一篇,文章内容均为本人通过实践及查阅资料相关整理所得,可用作新手入门指南,
- 1 发送文本信息'''加密发送文本邮件'''def sendEmail(from_addr,
- MySQL字符集出错的解决方法:错误案例: Illegal mix of collations (gbk_chinese_ci,I
- 1.环境设置1.1gradio安装需要安装 gradio,安装办法就是 pip install gradio2.ffmpeg安装再次需要加入
- format函数的调用format函数可以被直接调用或在print函数中以占位符实现格式化调用。在**2.6**版本前,占位符仍和其他语言一
- 本来想控制鼠标自动移动防止公司电脑自动休眠的策略,然而,实现了并没什么卵用,还是会休眠。但还是分享出来吧。win10的系统。首先要安装几个第
- IDEA 插件安装 步骤IDEA里面,选择打开 File --> Settings --> Plugins在Plugins里面,
- 在上一篇Python接口自动化测试系列文章:Python接口自动化之浅析requests模块post请求,介绍了post源码,data、js