图像分割之Snake主动轮廓模型(Matlab代码)

article/2025/10/16 8:01:51

示例演示

        如果在中文搜索的话,一般会找到《数字图像处理-图像分割:Snake主动轮廓模型 Matlab代码及运行结果》。里面有句代码,千万别用,否则出不来效果。(别问我怎么知道的)

% 转化为双精度型
%I = im2double(I);

         当然英文搜索的话,会在Matlab官网上找到《Snakes: Active Contour Models》,提供更好的代码,里面不同图片的算法参数都调好了。核心代码展示。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% interate.m implements the core of the snakes (active contours) algorithm.
% It is called by the snk.m file which is the GUI frontend. If you do not
% want to deal with GUI, look at this file and the included comments which
% explain how active contours work.
%
% To run this code with GUI
%   1. Type guide on the matlab prompt.
%   2. Click on "Go to Existing GUI"
%   3. Select the snk.fig file in the same directory as this file
%   4. Click the green arrow at the top to launch the GUI
%
%   Once the GUI has been launched, you can use snakes by
%   1. Click on "New Image" and load an input image. Samples image are
%   provided.
%   2. Set the smoothing parameter "sigma" or leave it at its default value
%   and click "Filter". This will smooth the image.
%   3. As soon as you click "Filter", cross hairs would appear and using
%   them and left click of you mouse you can pick initial contour location
%   on the image. A red circle would appead everywhere you click and in
%   most cases you should click all the way around the object you want to
%   segement. The last point must be picked using a right-click in order to
%   stop matlab for asking for more points.
%   4. Set the various snake parameters (relative weights of energy terms
%   in the snake objective function) or leave them with their default value
%   and click "Iterate" button. The snake would appear and move as it
%   converges to its low energy state.
%
% Copyright (c) Ritwik Kumar, Harvard University 2010
%               www.seas.harvard.edu/~rkkumar
%
% This code implements 揝nakes: Active Contour Models�by Kass, Witkin and
% Terzopolous incorporating Eline, Eedge and Eterm energy factors. See the
% included report and the paper to learn more.
%
% If you find this useful, also look at Radon-Like Features based
% segmentation in  the following paper:
% Ritwik Kumar, Amelio V. Reina & Hanspeter Pfister, 揜adon-Like Features 
% and their Application to Connectomics� IEEE Computer Society Workshop %
% on Mathematical Methods in Biomedical Image Analysis (MMBIA) 2010
% http://seas.harvard.edu/~rkkumar
% Its code is also available on MATLAB Central
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%function [smth] = interate(image, xs, ys, alpha, beta, gamma, kappa, wl, we, wt, iterations);
% image: This is the image data
% xs, ys: The initial snake coordinates
% alpha: Controls tension
% beta: Controls rigidity
% gamma: Step size
% kappa: Controls enegry term
% wl, we, wt: Weights for line, edge and terminal enegy components
% iterations: No. of iteration for which snake is to be moved%parameters
N = iterations; 
smth = image;% Calculating size of image
[row col] = size(image);%Computing external forceseline = smth; %eline is simply the image intensities[grady,gradx] = gradient(smth);
eedge = -1 * sqrt ((gradx .* gradx + grady .* grady)); %eedge is measured by gradient in the image%masks for taking various derivatives
m1 = [-1 1];
m2 = [-1;1];
m3 = [1 -2 1];
m4 = [1;-2;1];
m5 = [1 -1;-1 1];cx = conv2(smth,m1,'same');
cy = conv2(smth,m2,'same');
cxx = conv2(smth,m3,'same');
cyy = conv2(smth,m4,'same');
cxy = conv2(smth,m5,'same');for i = 1:rowfor j= 1:col% eterm as deined in Kass et al Snakes papereterm(i,j) = (cyy(i,j)*cx(i,j)*cx(i,j) -2 *cxy(i,j)*cx(i,j)*cy(i,j) + cxx(i,j)*cy(i,j)*cy(i,j))/((1+cx(i,j)*cx(i,j) + cy(i,j)*cy(i,j))^1.5);end
end% imview(eterm);
% imview(abs(eedge));
eext = (wl*eline + we*eedge -wt * eterm); %eext as a weighted sum of eline, eedge and eterm[fx, fy] = gradient(eext); %computing the gradient%initializing the snake
xs=xs';
ys=ys';
[m n] = size(xs);
[mm nn] = size(fx);%populating the penta diagonal matrix
A = zeros(m,m);
b = [(2*alpha + 6 *beta) -(alpha + 4*beta) beta];
brow = zeros(1,m);
brow(1,1:3) = brow(1,1:3) + b;
brow(1,m-1:m) = brow(1,m-1:m) + [beta -(alpha + 4*beta)]; % populating a template row
for i=1:mA(i,:) = brow;brow = circshift(brow',1)'; % Template row being rotated to egenrate different rows in pentadiagonal matrix
end[L U] = lu(A + gamma .* eye(m,m));
Ainv = inv(U) * inv(L); % Computing Ainv using LU factorization%moving the snake in each iteration
for i=1:N;ssx = gamma*xs - kappa*interp2(fx,xs,ys);ssy = gamma*ys - kappa*interp2(fy,xs,ys);%calculating the new position of snakexs = Ainv * ssx;ys = Ainv * ssy;%Displaying the snake in its new positionimshow(image,[]); hold on;plot([xs; xs(1)], [ys; ys(1)], 'r-');hold off;pause(0.001)    
end;

不同图片运行结果
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述


http://chatgpt.dhexx.cn/article/nV6PykCx.shtml

相关文章

Snake活动轮廓模型Matlab实现

1. Snake模型 人为地在图像感兴趣的区域(ROI)上给出初始轮廓曲线,最小化一个能量函数,使轮廓曲线在图像中运动(变形),最终逼近该区域的边界。 设v(s)[x(s),y(s)]为活动轮廓线,s∈[0,…

snake主动轮廓模型

模型:一条可变形的参数曲线及相应的能量函数,以最小化能量函数为目标,控制参数曲线变形,具有最小能量的闭合曲线即是目标轮廓。 snake模型调和了上层知识和底层图像特征矛盾。 上层知识指物体形状。表示内部力。 底层图像特征是局…

Snake活动轮廓模型

1. Snake模型 人为地在图像感兴趣的区域(ROI)上给出初始轮廓曲线,最小化一个能量函数,使轮廓曲线在图像中运动(变形),最终逼近该区域的边界。 设v(s)[x(s),y(s)]为活动轮廓线,s∈[0,…

基于边缘的主动轮廓模型——从零到一用python实现snake

从零到一实现snake算法 1、Snake算法原理2、基于曲线演化的实现方法2.1演化方程推导2.2离散化过程2.3 代码实现 3、基于水平集的实现方法4、讨论与分析源码地址[snake](https://github.com/woshimami/snake) 1、Snake算法原理 Kass等人1最早于1988年提出了主动轮廓模型&#x…

主动轮廓模型snake

原理概述 snake模型将图像分割问题转换为求解能量泛函最小值的问题。主要思路是构造能量函数进行迭代后,轮廓曲线由初始位置逐渐向使能量函数最小(局部极小)的图像边缘逼近,最终分割出目标。 曲线理论 假设一条光滑封闭曲线 C …

腾讯电脑管家,vs安装文件报成木马,还能信吗?

今天在公司安装vs2013,安装过程中腾讯公司的产品“电脑管家”提示有新版本,没有犹豫的点了升级,完成后直接在管家主界面上点了“全面体检”按钮,这一点不要紧,报告有一个木马,看紧看一下“详情”&#xff0…

计算机windows8黑屏怎么办,Win8电脑开机黑屏只有鼠标光标怎么解决

有些win8系统用户在开机的时候,遇到了黑屏的情况, 整个屏幕上面只有一个闪烁的鼠标光标,导致无法进入到系统桌面,遇到这样的情况该怎么解决呢?现在给大家分享一下Win8电脑开机黑屏只有鼠标光标的具体解决方法吧。 Win8…

解决ValueError: Cannot run multiple SparkContexts at once; existing SparkContext

一、问题描述 创建sparkcontext和SparkSession,连接spark集群时报错,如题ValueError: Cannot run multiple SparkContexts at once; existing SparkContext。 from pyspark.sql import SparkSession from pyspark.sql import functions as F from pysp…

1、Qt线程(二):继承QThread,重写run

一、功能说明 1、通过继承QThread,重写run的方式实现多线程 2、点击“开始”按钮启动子线程,同时通过信号槽的方式给子线程发送“开始”字符串; 3、子线程每隔1秒向主线程发送累加数; 4、点击"停止"按钮&#xff0c…

诡异的RunOnce病毒启动项和神奇的URL Protocol

整理磁盘发现之前有个有趣的流氓招数忘记分享了,每次看到新鲜的东东都感慨黑暗势力的层出不穷的招数,比某些安全厂商是不是自相残杀好多了.电脑日常使用过程中我们经常输入开头为http ftp,点击诸如ed2k的链接,每个链接的背后都会执行相应的功能.如http 通过iexplore.exe,ed2k通…

如何创建水晶报表

开发工具与关键技术:VS与MVC 作者:刘华叶 撰写时间:2019年4月29日 MVC是软件工程中的一种软件架构模式,涉及到的知识点也是相当广泛,而我们在做项目的过程中,总会遇到要制作一些报表,水晶报表就…

java 水晶报表教程_水晶报表 (Crystal Reports 2008)的配置

概要: Crystal Reports(水晶报表)是一款商务智能(BI)软件,主要用于设计及产生报表。水晶报表是业内最专业、功能最强的报表系统,它除了强大的报表功能外。最大的优势是实现了与绝大多数流行开发工具的集成和接口。在VS.Net平台做过报表开发的…

水晶报表教程:手把手教你制作基本报表

ASP.NET水晶报表的学习 这篇文章教你如何在.Net Web应用中使用水晶报表,也可以让你在学习过程中少走一些弯路。为了得到最好的效果,读者最好需要有一些基础的Asp.Net访问数据库的知识以及使用VS.Net的开发经验。 简介 水晶报表可以由很多的方法得到&a…

水晶报表基本使用方法

开发工具与关键技术:VS/MVC 作者:何桂朋 撰写时间:2019年4月22日 Crystal Reports(水晶报表)是一款商务智能(BI)软件,主要用于设计及产生报表。水晶报表是业内最专业、功能最强的报…

php try catch 不处理,如何解决php try catch不起作用的问题

如何解决php try catch不起作用的问题 发布时间:2020-09-22 09:59:14 来源:亿速云 阅读:103 作者:小新 这篇文章主要介绍了如何解决php try catch不起作用的问题,具有一定借鉴价值,需要的朋友可以参考下。希…

try catch执行过程分析

本篇文章带大家聊聊try catch的执行过程,有时候在开发的过程中,try代码里如果出现异常,catch后的步骤还会继续执行吗?以及finally的使用。 下面来分析一下几种使用场景: 场景一: try代码块中出现异常后&a…

面试官问我 ,try catch 应该在for循环里面还是外面?

前言 有个老哥昨天被面试官欺负了,但是是被这个问题(标题)欺负的? 其实是个比较基础的问题,只要有了解过,叙述是非常简单OK的。 只要有初学者觉得有疑惑,那么我相信不止是他一个。 所以&#…

try catch异常捕获

这次我们介绍try catch异常捕获。下面是try catch的语法。 thy catch 的作用就是异常捕获,在一些会报错的地方时的时候才会用到。 例如这个代码,当我们在控制器时输入的不是数字而是其他的文字或者其他符号呢?所以这里程序就会报错&#xff0…

try catch 的作用

不加try catch package com.web; public class Test25 {public static void main(String[] args) {int i 1 / 0;System.out.println("i " i);System.out.println("aaaa");//直接不运行了} }加try catch package com.web;public class Test25 {public …

kotlin try catch使用方法

5 kotlin之 try catch kotlin的try catch比Java更简洁面料采用,使用更方便 val value "10a"var out: Int? null//错误处理try {out Integer.parseInt(value)} catch (e: NumberFormatException) {println("NumberFormatException")println(e.message)…