质量声明:原创文章,内容质量问题请评论吐槽。如对您产生干扰,可私信删除。
主要参考:Active Contour Model — skimage v0.16.dev0 docs - scikit-image


文章目录

  • skimage实现
    • 函数声明
    • 代码示例
    • 结果显示
  • Numpy实现
    • 代码示例
    • 结果显示

skimage实现

函数声明

Active contours by fitting snakes to features of images. Supports single and multichannel 2D images. Snakes can be periodic (for segmentation) or have fixed and/or free ends. The output snake has the same length as the input boundary. As the number of points is constant, make sure that the initial snake has enough points to capture the details of the final contour.

active_contour(image, snake, alpha=0.01, beta=0.1, w_line=0, w_edge=1, gamma=0.01,bc='periodic', max_px_move=1.0, max_iterations=2500, convergence=0.1)Parameters----------image : (N, M) or (N, M, 3) ndarrayInput image.snake : (N, 2) ndarrayInitial snake coordinates. For periodic boundary conditions, endpointsmust not be duplicated.alpha : float, optionalSnake length shape parameter. Higher values makes snake contractfaster.beta : float, optionalSnake smoothness shape parameter. Higher values makes snake smoother.w_line : float, optionalControls attraction to brightness. Use negative values to attract towarddark regions.w_edge : float, optionalControls attraction to edges. Use negative values to repel snake fromedges.gamma : float, optionalExplicit time stepping parameter.bc : {'periodic', 'free', 'fixed'}, optionalBoundary conditions for worm. 'periodic' attaches the two ends of thesnake, 'fixed' holds the end-points in place, and 'free' allows freemovement of the ends. 'fixed' and 'free' can be combined by parsing'fixed-free', 'free-fixed'. Parsing 'fixed-fixed' or 'free-free'yields same behaviour as 'fixed' and 'free', respectively.max_px_move : float, optionalMaximum pixel distance to move per iteration.max_iterations : int, optionalMaximum iterations to optimize snake shape.convergence: float, optionalConvergence criteria.Returns-------snake : (N, 2) ndarrayOptimised snake, same shape as input parameter.References----------.. [1]  Kass, M.; Witkin, A.; Terzopoulos, D. "Snakes: Active contourmodels". International Journal of Computer Vision 1 (4): 321(1988). DOI:`10.1007/BF00133570`

代码示例

import numpy as np
from matplotlib import pyplot as plt
from skimage.color import rgb2gray
from skimage import data
from skimage.filters import gaussian
from skimage.segmentation import active_contourimg = data.astronaut() # 读入图像
img = rgb2gray(img) # 灰度化# 圆的参数方程:(220, 100) r=100
t = np.linspace(0, 2*np.pi, 400) # 参数t, [0,2π]
x = 220 + 100*np.cos(t)
y = 100 + 100*np.sin(t)# 构造初始Snake
init = np.array([x, y]).T # shape=(400, 2)# Snake模型迭代输出
snake = active_contour(gaussian(img,3), snake=init, alpha=0.1, beta=1, gamma=0.01, w_line=0, w_edge=10)# 绘图显示
plt.figure(figsize=(5, 5))
plt.imshow(img, cmap="gray")
plt.plot(init[:, 0], init[:, 1], '--r', lw=3)
plt.plot(snake[:, 0], snake[:, 1], '-b', lw=3)
plt.xticks([]), plt.yticks([]), plt.axis("off")
plt.show()

结果显示


Numpy实现

代码示例

import cv2 as cv
import numpy as np
from matplotlib import pyplot as pltdef getGaussianPE(src):"""描述:计算负高斯势能(Negative Gaussian Potential Energy, NGPE)输入:单通道灰度图src输出:无符号的浮点型单通道,取值0.0 ~ 255.0"""imblur = cv.GaussianBlur(src, ksize=(5, 5), sigmaX=3)dx = cv.Sobel(imblur, cv.CV_16S, 1, 0)  # X方向上取一阶导数,16位有符号数,卷积核3x3dy = cv.Sobel(imblur, cv.CV_16S, 0, 1)E = dx**2 + dy**2return Edef getDiagCycleMat(alpha, beta, n):"""计算5对角循环矩阵"""a = 2 * alpha + 6 * betab = -(alpha + 4 * beta)c = betadiag_mat_a = a * np.eye(n)diag_mat_b = b * np.roll(np.eye(n), 1, 0) + b * np.roll(np.eye(n), -1, 0)diag_mat_c = c * np.roll(np.eye(n), 2, 0) + c * np.roll(np.eye(n), -2, 0)return diag_mat_a + diag_mat_b + diag_mat_cdef getCircleContour(centre=(0, 0), radius=(1, 1), N=200):"""以参数方程的形式,获取n个离散点围成的圆形/椭圆形轮廓输入:中心centre=(x0, y0), 半轴长radius=(a, b), 离散点数N输出:由离散点坐标(x, y)组成的2xN矩阵"""t = np.linspace(0, 2 * np.pi, N)x = centre[0] + radius[0] * np.cos(t)y = centre[1] + radius[1] * np.sin(t)return np.array([x, y])def getRectContour(pt1=(0, 0), pt2=(50, 50)):"""根据左上、右下两个顶点来计算矩形初始轮廓坐标由于Snake模型适用于光滑曲线,故这里用不到该函数"""pt1, pt2 = np.array(pt1), np.array(pt2)r1, c1, r2, c2 = pt1[0], pt1[1], pt2[0], pt2[1]a, b = r2 - r1, c2 - c1length = (a + b) * 2 + 1x = np.ones((length), np.float)x[:b] = r1x[b:a + b] = np.arange(r1, r2)x[a + b:a + b + b] = r2x[a + b + b:] = np.arange(r2, r1 - 1, -1)y = np.ones((length), np.float)y[:b] = np.arange(c1, c2)y[b:a + b] = c2y[a + b:a + b + b] = np.arange(c2, c1, -1)y[a + b + b:] = c1return np.array([x, y])def snake(img, snake, alpha=0.5, beta=0.1, gamma=0.1, max_iter=2500, convergence=0.01):"""根据Snake模型的隐式格式进行迭代输入:弹力系数alpha,刚性系数beta,迭代步长gamma,最大迭代次数max_iter,收敛阈值convergence输出:由收敛轮廓坐标(x, y)组成的2xN矩阵, 历次迭代误差list"""x, y, errs = snake[0].copy(), snake[1].copy(), []n = len(x)# 计算5对角循环矩阵A,及其相关逆阵A = getDiagCycleMat(alpha, beta, n)inv = np.linalg.inv(A + gamma * np.eye(n))# 初始化y_max, x_max = img.shapemax_px_move = 1.0# 计算负高斯势能矩阵,及其梯度E_ext = -getGaussianPE(img)fx = cv.Sobel(E_ext, cv.CV_16S, 1, 0)fy = cv.Sobel(E_ext, cv.CV_16S, 0, 1)T = np.max([abs(fx), abs(fy)])fx, fy = fx / T, fy / Tfor g in range(max_iter):x_pre, y_pre = x.copy(), y.copy()i, j = np.uint8(y), np.uint8(x)try:xn = inv @ (gamma * x + fx[i, j])yn = inv @ (gamma * y + fy[i, j])except Exception as e:print("索引超出范围")# 判断收敛x, y = xn, ynerr = np.mean(0.5 * np.abs(x_pre - x) + 0.5 * np.abs(y_pre - y))errs.append(err)if err < convergence:print(f"Snake迭代{g}次后,趋于收敛。\t err = {err:.3f}")breakreturn x, y, errsdef main():src = cv.imread("circle.jpg", 0)img = cv.GaussianBlur(src, (3, 3), 5)# 构造初始轮廓线init = getCircleContour((140, 95), (110, 80), N=200)# Snake Modelx, y, errs = snake(img, snake=init, alpha=0.1, beta=1, gamma=0.1)plt.figure() # 绘制轮廓图plt.imshow(img, cmap="gray")plt.plot(init[0], init[1], '--r', lw=1)plt.plot(x, y, 'g', lw=1)plt.xticks([]), plt.yticks([]), plt.axis("off")plt.figure() # 绘制收敛趋势图plt.plot(range(len(errs)), errs)plt.show()if __name__ == '__main__':main()

结果显示

Snake迭代760次后,趋于收敛。     err = 0.010

主动轮廓模型:Snake模型的python实现相关推荐

  1. Active Contour Models 主动轮廓模型(snake模型)

    主动轮廓模型主要用于解决图像中目标物体的分割操作.理论上是可以解决二维乃至多维的情况,不过最初的模型是在二维图像上建立的. 主动轮廓模型(Active Contour Model),又被称为Snake ...

  2. 图像分割之主动轮廓线模型Snake

    基本概念 1987年由 Kass 等人提出的主动轮廓模型即蛇模型(snake 模型).活动轮廓模型可以用在图像分割和理解中,也适用于分析动态图像或三维图像.Snake定义为最小的能量样条曲线.下面重点 ...

  3. 8.2 Python图像处理之图像典型分割-主动轮廓

    8.2 Python图像处理之图像典型分割-主动轮廓 文章目录 8.2 Python图像处理之图像典型分割-主动轮廓 1 算法原理 2 代码 3 效果 1 算法原理 主动轮廓模型,将图像分割问题转换为 ...

  4. 离散化-利用计算机求解y=x,基于边缘的主动轮廓模型——从零到一用python实现snake...

    从零到一实现snake算法 1.Snake算法原理 Kass等人最早于1988年提出了主动轮廓模型,该方法通过构造能量函数,从而将图像分割转化为求解能量泛函极值的问题,其核心思想在于,作者认为和之前的 ...

  5. OpenCV学习笔记(Python)———— 主动轮廓模型

    本文包含主动轮廓模型代码以及实例分割代码 原图: 效果图: 主动轮廓模型: morphsnakes.py # -*- coding: utf-8 -*- """ ==== ...

  6. 水平集方法引入主动轮廓模型算法中的两种方法

    水平集方法引入主动轮廓模型算法中的两种方法 1.传统的基于主动轮廓模型和水平集理论的方法 2.变分水平集方法 在讲解水平集理论在主动轮廓模型中的应用前,我们先用流程图说明一下常见的处理主动轮廓模型的流 ...

  7. Active Contour Models 主动轮廓模型

    参考博客: https://www.mathworks.com/matlabcentral/fileexchange/19567-active-contour-segmentation 数字图像处理- ...

  8. 《Matlab图像处理》part1 Snakes:Active Contour Models 主动轮廓模型

    <Matlab图像处理>part1 Snakes:Active Contour Models 主动轮廓模型 参考博客: 数字图像处理-图像分割:Snake主动轮廓模型 Matlab代码及运 ...

  9. 主动轮廓模型 matlab,主动轮廓模型的功能.ppt

    主动轮廓模型的功能 Amelioration d'images ultrasonores via alignement de vasculature - Julien Jomier - UNC - 2 ...

  10. 基于带有信息熵和联合矢量的LBF主动轮廓模型的PET-CT成像中对静脉血管肺结节分割 (笔记四)

    -----------------------------------------------------------------SUV 标准吸收值-------------------------- ...

最新文章

  1. java atomicfloat_Java:有没有AtomicFloat或AtomicDouble?
  2. 绝了!Dataway让 SpringBoot 变得更强大!
  3. 算法工程师怎样提升业务理解能力?
  4. 用计算机对话的小品,爆笑小品剧本台词《作弊记》
  5. Python 字符编码 b
  6. Sublime Text 3中配置编译和运行Java的两个方法
  7. 陷阱:在 WebApp 中谨防 Singleton 错误
  8. mysql可视化创建外键说明_关于使用可视化图形工具navicat for mysql来创建外键的步骤...
  9. Hbase 的Java API 操作
  10. 多开 android模拟器,安卓模拟器如何多开窗口保证游戏不封号
  11. 免费在线的数据库建模工具,云版PownerDesigner
  12. 细数历史上那些有名的程序媛
  13. 谷歌浏览器部分iframe页面无法打开,跨域问题
  14. python基础入门练习(字符串练习)
  15. google不能用解决方法
  16. 汽车钥匙改装成手机蓝牙无钥匙进入一键启动 ,汽车无钥匙进入为何不能集成到手机?蓝牙无钥匙进入一键启动解决方案
  17. 淘宝密码加密方式分析,及python实现
  18. margin 重叠问题的理解
  19. 邮政社招笔试题库_2016年中国邮政储蓄银行招聘考试笔试题库内容试卷历年真题...
  20. android如何添加透明图片按钮,如何拥有透明的ImageButton:Android

热门文章

  1. java png转svg工具_关于图像:将TIFF或PNG或JPEG转换为SVG的Java API
  2. oracle卸载步骤图解,Oracle详细卸载步骤
  3. php获取客户端和服务器ip,PHP获取客户端和服务器IP地址
  4. UCOS操作系统——任务管理(二)
  5. java开发正则表达式
  6. 文本编辑器android,Editor简单通用文本编辑器
  7. Java自定义动态数组
  8. 【spring系列】spring注解解析原理
  9. 大一入门oj题目——《三天以后》
  10. 绘图工具java软件开发模板_仅适合程序员的uml绘图工具-plantuml