matlab实现图割算法中的最大流最小割Max-flow/min-cut问题(一)

本篇主要介绍matlab实现Max-flow/min-cut的方法,介绍一种只实现了Max-flow/min-cut的工具箱Bk_matlab。


一:最大流最小割的由来

  了解这个问题之前先说说这个问题的由来吧。最大流最小割最开始从图论的相关概念中引用过来,讲述一个带有起点与终点并且具有边权值的网络图中,如何进行边的切割,把这个网络图分成独立的两个部分(或者多个部分),使得这个切割中被切割的边的权值之和最小。比如现在有一个网络图如下:

那么要把这个图分割成两部分,如上的虚线就是一种切割方式,这个时候可以看到这种切割下消耗的边的权值为3+4=7吧,当然,切割的方式很多种,不同的切割方式自然对应不同的切割边权值,而最大流最小割就是找到一种切割方式使得切割的边的权值之和最小。

这么一看似乎也看不出它的实际意义所在,最大流最小割应该说是应用还挺好的,一个重要应用就在于对图像的分割上面。

先了解一下图像的分割,随便对于一副图像,很容易把图像分成若干部分的,常见的图像分割算法有Kmeans,FCM,马尔科夫,等等很多。我们知道,图像简单来说就是矩阵,对于只是灰度图像,那么就是一般的二维矩阵,矩阵中值的大小就是该点的灰度值,那么图像分割问题就是寻找到图像的边界,而边界肯定在两个像素值差别很大的邻域间,如果把两个邻域间的像素值差定义为该邻域的边权值的话,分割问题就转化为如何切割这些边的问题了,这样模型就和上述的对应上了,有点,有边,有边的权值,那么就可以运用上述理论解决图的分割问题了。

在形象一点如下所示:


那么这就是一个对图像的分割。

理论介绍的不够好,贴一些其他关于最大流最小割的介绍性文章:

http://wenku.baidu.com/view/54323c030722192e4536f64f.html?re=view

http://blog.sina.com.cn/s/blog_638502960100g39x.html

最大流/最小割算法总结


二:关于图割理论

在正式使用之前先介绍下关于图割理论用来分割图像的方法。还是先贴些比较好的别人写的文章:

图像分割之(二)Graph Cut(图割)

这里面理论化介绍的都是直观上的对图像的操作,而在实际变成实现的时候是要事先转化到一维或者直接调用相应的函数才能对二维的图像进行操作。我们先介绍对一维的图割操作,后面再介绍二维是如何转化到一维的。

最小费用最大流是一个经典的网络流问题,可以使用MATLAB来解决。MATLAB有许多网络流算法实现,其包括最小费用最大流算法。以下是一个简单的MATLAB代码,可以用来解决最小费用最大流问题: ```matlab function [f, cost] = min_cost_max_flow(C, s, t, demand) % C: cost matrix, C(i,j) is the cost of sending one unit of flow from i to j % s: source node % t: sink node % demand: demand vector, demand(i) is the demand at node i % f: flow matrix, f(i,j) is the flow from i to j % cost: the minimum cost of sending the required demand n = size(C, 1); % number of nodes f = zeros(n); % initialize flow matrix while true % find shortest path from s to t using the Bellman-Ford algorithm [dist, prev] = bellman_ford(C, s); if dist(t) == inf % if there is no path from s to t, terminate break end % initialize residual capacity matrix res_cap = zeros(n); for i = 1:n for j = 1:n res_cap(i,j) = C(i,j) - f(i,j); end end % find the bottleneck capacity bottleneck = demand(t); node = t; while node ~= s bottleneck = min(bottleneck, res_cap(prev(node), node)); node = prev(node); end % update flow matrix and demand vector node = t; while node ~= s f(prev(node), node) = f(prev(node), node) + bottleneck; f(node, prev(node)) = f(node, prev(node)) - bottleneck; demand(node) = demand(node) - bottleneck; demand(prev(node)) = demand(prev(node)) + bottleneck; node = prev(node); end end % calculate minimum cost cost = 0; for i = 1:n for j = 1:n cost = cost + f(i,j) * C(i,j); end end end function [dist, prev] = bellman_ford(C, s) % C: cost matrix, C(i,j) is the cost of sending one unit of flow from i to j % s: source node % dist: distance vector, dist(i) is the shortest distance from s to i % prev: predecessor vector, prev(i) is the node that precedes i on the shortest path from s to i n = size(C, 1); % number of nodes dist = inf(1, n); % initialize distance vector prev = zeros(1, n); % initialize predecessor vector dist(s) = 0; % distance from source to source is zero for k = 1:n-1 % iterate n-1 times for i = 1:n for j = 1:n if C(i,j) ~= 0 && dist(i) + C(i,j) < dist(j) dist(j) = dist(i) + C(i,j); prev(j) = i; end end end end end ``` 这个代码实现了最小费用最大流算法,其使用了Bellman-Ford算法来寻找最短路径。你可以根据自己的需求来调整代码,并在MATLAB运行它。
评论 29
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值