定制网站哪家好,网站建设教程答允苏州久远网络,wordpress 功能模块,网站更新中【youcans 的 OpenCV 例程200篇】176.图像分割之均值漂移算法 【youcans 的 OpenCV 例程200篇】177.图像分割之 GraphCuts 图割法 【youcans 的 OpenCV 例程200篇】178.图像分割之 GrabCut 图割法#xff08;框选前景#xff09; 【youcans 的 OpenCV 例程200篇】179.图像分割… 【youcans 的 OpenCV 例程200篇】176.图像分割之均值漂移算法 【youcans 的 OpenCV 例程200篇】177.图像分割之 GraphCuts 图割法 【youcans 的 OpenCV 例程200篇】178.图像分割之 GrabCut 图割法框选前景 【youcans 的 OpenCV 例程200篇】179.图像分割之 GrabCut 图割法掩模图像 更多内容请见 【OpenCV 例程200篇 总目录-202206更新】 【youcans 的 OpenCV 例程200篇】177.图像分割之 GraphCuts 图割法 6. 图像分割之图割法
基于图论的图像分割技术的基本思想是将图像映射为带权的无向图把像素视为节点两个节点之间的边的权重对应于两个像素之间相似性的度量割的容量就对应于能量函数使用最大流最小割算法对图进行切割得到的最小割就对应于最优图像分割。
6.1 图割分割算法 Graph cuts
将图像表示为无向图后基于网络流优化技术求解最小图割以获得图像分割结果。
一个图或网络的割表示一个切面或切线将图或网络分为分别包含源点和汇点的两个子集该切线或切面与网络相交的楞或边的集合称为图像的割。最小割Minimum cut是指边的容量权重之和最小的割。
图论中的最大流最小割定理指出从源点到汇点的最大流等于最小割因此最小割问题等价于最大流问题。求解最大流问题有很多高效的多项式时间算法 可以应用于图像分割。
包含源点和汇点的两个子集对应于图像中的两个分区 边的权重对应于图像中两个像素之间的相似性。
对于给定的全无向图 G(V,E)G(V,E)G(V,E)图的切割将节点 V 分为两个子集 A、BA∪BVA \cup B VA∪BVA∩BϕA \cap B \phiA∩Bϕ。
连接边的权重 w(i,j)w(i,j)w(i,j) 是节点 i,ji, ji,j 之间相似性的函数 w(i,j)1/(∣I(ni)−I(nj)∣η)w(i,j)1 / \big( |I(n_i) - I(n_j)| \eta \big) w(i,j)1/(∣I(ni)−I(nj)∣η)
最小割定义为割集的边的最小总权重
cut(A,B)∑u∈A,v∈Bw(u,v)cut(A,B) \sum_{u \in A, v \in B} w(u,v) cut(A,B)u∈A,v∈B∑w(u,v)
一种改进方法使用归一化切割Ncut的测度定义
Ncut(A,B)cut(A,B)assoc(A,V)cut(A,B)assoc(B,V)Nassoc(A,B)assoc(A,A)assoc(A,V)assoc(B,B)assoc(B,V)assoc(A,V)∑u∈A,z∈Vw(u,z)assoc(B,V)∑v∈B,z∈Vw(v,z)\begin{aligned} Ncut(A,B) \frac{cut(A,B)}{assoc(A,V)} \frac{cut(A,B)}{assoc(B,V)} \\ Nassoc(A,B) \frac{assoc(A,A)}{assoc(A,V)} \frac{assoc(B,B)}{assoc(B,V)} \\ assoc(A,V) \sum_{u \in A, z \in V} w(u,z) \\ assoc(B,V) \sum_{v \in B, z \in V} w(v,z) \end{aligned} Ncut(A,B)Nassoc(A,B)assoc(A,V)assoc(B,V)assoc(A,V)cut(A,B)assoc(B,V)cut(A,B)assoc(A,V)assoc(A,A)assoc(B,V)assoc(B,B)u∈A,z∈V∑w(u,z)v∈B,z∈V∑w(v,z)
另一种常用的方法使用基于区域分布直方图和边界形状的能量函数 E(L)
E(L)αR(L)B(L)E(L) \alpha R(L) B(L) E(L)αR(L)B(L)
其中R(L) 是区域分布项regional termB(L) 是边界项boundary termE(L) 是能量函数。图割的目标是最小化能量函数。
图割分割算法Graph Cut运用最小割最大流算法进行图像的分割将图像分割为前景和背景。
GraphCut 算法需要用户在前景和背景处各画几笔作为输入由此建立各个像素点与前景背景相似度的赋权图并通过求解最小割进行图像的前景和背景分割。 例程 11.34 图割分割算法 Graph Cuts
# 11.34 GraphCuts 交互式图割分割算法 说明(1) 用鼠标左键标记前景鼠标右键标记背景(2) 可以重复标记不断优化(3) 按 Esc 键退出完成分割。
# Copyright 2022 Youcans, XUPT
# Crated2021-12-27import cv2
import numpy as np
from matplotlib import pyplot as pltdrawing False
mode Falseclass GraphCutXupt:def __init__(self, t_img):self.img t_imgself.img_raw img.copy()self.img_width img.shape[0]self.img_height img.shape[1]self.scale_size 640 * self.img_width // self.img_heightif self.img_width 640:self.img cv2.resize(self.img, (640, self.scale_size), interpolationcv2.INTER_AREA)self.img_show self.img.copy()self.img_gc self.img.copy()self.img_gc cv2.GaussianBlur(self.img_gc, (3, 3), 0)self.lb_up Falseself.rb_up Falseself.lb_down Falseself.rb_down Falseself.mask np.full(self.img.shape[:2], 2, dtypenp.uint8)self.firt_choose True# 鼠标的回调函数
def mouse_event2(event, x, y, flags, param):global drawing, last_point, start_point# 左键按下开始画图if event cv2.EVENT_LBUTTONDOWN:drawing Truelast_point (x, y)start_point last_pointparam.lb_down Trueprint(mouse lb down)elif event cv2.EVENT_RBUTTONDOWN:drawing Truelast_point (x, y)start_point last_pointparam.rb_down Trueprint(mouse rb down)# 鼠标移动画图elif event cv2.EVENT_MOUSEMOVE:if drawing:if param.lb_down:cv2.line(param.img_show, last_point, (x, y), (0, 0, 255), 2, -1)cv2.rectangle(param.mask, last_point, (x, y), 1, -1, 4)else:cv2.line(param.img_show, last_point, (x, y), (255, 0, 0), 2, -1)cv2.rectangle(param.mask, last_point, (x, y), 0, -1, 4)last_point (x, y)# 左键释放结束画图elif event cv2.EVENT_LBUTTONUP:drawing Falseparam.lb_up Trueparam.lb_down Falsecv2.line(param.img_show, last_point, (x, y), (0, 0, 255), 2, -1)if param.firt_choose:param.firt_choose Falsecv2.rectangle(param.mask, last_point, (x, y), 1, -1, 4)print(mouse lb up)elif event cv2.EVENT_RBUTTONUP:drawing Falseparam.rb_up Trueparam.rb_down Falsecv2.line(param.img_show, last_point, (x, y), (255, 0, 0), 2, -1)if param.firt_choose:param.firt_choose Falseparam.mask np.full(param.img.shape[:2], 3, dtypenp.uint8)cv2.rectangle(param.mask, last_point, (x, y), 0, -1, 4)print(mouse rb up)if __name__ __main__:img cv2.imread(../images/imgDeer.png, flags1) # 读取彩色图像(Youcans)g_img GraphCutXupt(img)cv2.namedWindow(image)# 定义鼠标的回调函数cv2.setMouseCallback(image, mouse_event2, g_img)while (True):cv2.imshow(image, g_img.img_show)if g_img.lb_up or g_img.rb_up:g_img.lb_up Falseg_img.rb_up FalsebgdModel np.zeros((1, 65), np.float64)fgdModel np.zeros((1, 65), np.float64)rect (1, 1, g_img.img.shape[1], g_img.img.shape[0])print(g_img.mask)mask g_img.maskg_img.img_gc g_img.img.copy()cv2.grabCut(g_img.img_gc, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_MASK)mask2 np.where((mask 2) | (mask 0), 0, 1).astype(uint8) # 0和2做背景g_img.img_gc g_img.img_gc * mask2[:, :, np.newaxis] # 使用蒙板来获取前景区域cv2.imshow(youcans, g_img.img_gc)# 按下ESC键退出if cv2.waitKey(20) 27:breakplt.figure(figsize(10, 7))plt.subplot(221), plt.axis(off), plt.title(xupt)plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) # 显示 img(RGB)plt.subplot(222), plt.axis(off), plt.title(mask)plt.imshow(mask, gray)plt.subplot(223), plt.axis(off), plt.title(mask2)plt.imshow(mask2, gray)plt.subplot(224), plt.axis(off), plt.title(Grab Cut)plt.imshow(cv2.cvtColor(g_img.img_gc, cv2.COLOR_BGR2RGB))plt.tight_layout()plt.show()本节完 版权声明
OpenCV 例程200篇 总目录-202205更新 youcansxupt 原创作品转载必须标注原文链接(https://blog.csdn.net/youcans/article/details/124723416)
Copyright 2022 youcans, XUPT Crated2022-5-10 欢迎关注 『youcans 的 OpenCV 例程 200 篇』 系列持续更新中 欢迎关注 『youcans 的 OpenCV学习课』 系列持续更新中 【youcans 的 OpenCV 例程200篇】176.图像分割之均值漂移算法 【youcans 的 OpenCV 例程200篇】177.图像分割之 GraphCuts 图割法 【youcans 的 OpenCV 例程200篇】178.图像分割之 GrabCut 图割法框选前景 【youcans 的 OpenCV 例程200篇】179.图像分割之 GrabCut 图割法掩模图像 更多内容请见 【OpenCV 例程200篇 总目录-202206更新】