提取边界内的图像区域

2024-01-26

我必须使用 2D CT 图像做一个项目,并使用 Matlab(仅)对其中的肝脏和肿瘤进行分割。最初我必须单独分割肝脏区域。我使用区域生长进行肝脏分割。它获取种子点作为输入。

输出是具有肝脏区域边界的图像。现在我需要仅由边界包围的区域。

我的程序有一个主程序和一个regionGrowing.m函数。因为我是新用户,所以不允许发布图片。如果你确实需要图片我会发邮件给你。请帮助我。

  % mainreg.m

  IR=imread('nfliver5.jpg');
  figure, imshow(IR), hold all
  poly = regionGrowing(IR,[],15,1200); % click somewhere inside the liver
  plot(poly(:,1), poly(:,2), 'LineWidth', 2, 'Color', [1 1 1])

%regionGrowing.m

function [P, J] = regionGrowing(cIM, initPos, thresVal, maxDist, tfMean, tfFillHoles, tfSimplify)
% REGIONGROWING Region growing algorithm for 2D/3D grayscale images
%
% Syntax:
%   P = regionGrowing();
%   P = regionGrowing(cIM);
%   P = regionGrowing(cIM, initPos)
%   P = regionGrowing(..., thresVal, maxDist, tfMean, tfFillHoles, tfSimpl)
%   [P, J] = regionGrowing(...);
%
% Inputs:
%          cIM: 2D/3D grayscale matrix                      {current image}
%      initPos: Coordinates for initial seed position     {ginput position}
%     thresVal: Absolute threshold level to be included     {5% of max-min}
%      maxDist: Maximum distance to the initial position in [px]      {Inf}
%       tfMean: Updates the initial value to the region mean (slow) {false}
%  tfFillHoles: Fills enclosed holes in the binary mask              {true}
%   tfSimplify: Reduces the number of vertices {true, if dpsimplify exists}
%
% Outputs:
%   P: VxN array (with V number of vertices, N number of dimensions)
%      P is the enclosing polygon for all associated pixel/voxel
%   J: Binary mask (with the same size as the input image) indicating
%      1 (true) for associated pixel/voxel and 0 (false) for outside
%   
% Examples:
%   % 2D Example
%   load example
%   figure, imshow(cIM, [0 1500]), hold all
%   poly = regionGrowing(cIM, [], 300); % click somewhere inside the lungs
%   plot(poly(:,1), poly(:,2), 'LineWidth', 2)
%   
%   % 3D Example
%   load mri
%   poly = regionGrowing(squeeze(D), [66,55,13], 60, Inf, [], true, false);
%   plot3(poly(:,1), poly(:,2), poly(:,3), 'x', 'LineWidth', 2)
%
% Requirements:
%   TheMathWorks Image Processing Toolbox for bwboundaries() and axes2pix()
%   Optional: Line Simplification by Wolfgang Schwanghart to reduce the 
%             number of polygon vertices (see the MATLAB FileExchange)
%
% Remarks:
%   The queue is not preallocated and the region mean computation is slow.
%   I haven't implemented a preallocation nor a queue counter yet for the
%   sake of clarity, however this would be of course more efficient.
%
% Author:
%   Daniel Kellner, 2011, braggpeaks{}googlemail.com
%   History: v1.00: 2011/08/14


% error checking on input arguments
if nargin > 7
    error('Wrong number of input arguments!')
end

if ~exist('cIM', 'var')
    himage = findobj('Type', 'image');
    if isempty(himage) || length(himage) > 1
        error('Please define one of the current images!')
    end

    cIM = get(himage, 'CData');
end

if ~exist('initPos', 'var') || isempty(initPos)
    himage = findobj('Type', 'image');
    if isempty(himage)
        himage = imshow(cIM, []);
    end

    % graphical user input for the initial position
    p = ginput(1);

    % get the pixel position concerning to the current axes coordinates
    initPos(1) = round(axes2pix(size(cIM, 2), get(himage, 'XData'), p(2)));
    initPos(2) = round(axes2pix(size(cIM, 1), get(himage, 'YData'), p(1)));
end

if ~exist('thresVal', 'var') || isempty(thresVal)
    thresVal = double((max(cIM(:)) - min(cIM(:)))) * 0.05;
end

if ~exist('maxDist', 'var') || isempty(maxDist)
    maxDist = Inf;
end

if ~exist('tfMean', 'var') || isempty(tfMean)
    tfMean = false;
end

if ~exist('tfFillHoles', 'var')
    tfFillHoles = true;
end

if isequal(ndims(cIM), 2)
    initPos(3) = 1;
elseif isequal(ndims(cIM),1) || ndims(cIM) > 3
    error('There are only 2D images and 3D image sets allowed!')
end

[nRow, nCol, nSli] = size(cIM);

if initPos(1) < 1 || initPos(2) < 1 ||...
   initPos(1) > nRow || initPos(2) > nCol
    error('Initial position out of bounds, please try again!')
end

if thresVal < 0 || maxDist < 0
    error('Threshold and maximum distance values must be positive!')
end

if ~isempty(which('dpsimplify.m'))
    if ~exist('tfSimplify', 'var')
        tfSimplify = true;
    end
    simplifyTolerance = 1;
else
    tfSimplify = false;
end


% initial pixel value
regVal = double(cIM(initPos(1), initPos(2), initPos(3)));

% text output with initial parameters
disp(['RegionGrowing Opening: Initial position (' num2str(initPos(1))...
      '|' num2str(initPos(2)) '|' num2str(initPos(3)) ') with '...
      num2str(regVal) ' as initial pixel value!'])

% preallocate array
J = false(nRow, nCol, nSli);

% add the initial pixel to the queue
queue = [initPos(1), initPos(2), initPos(3)];


%%% START OF REGION GROWING ALGORITHM
while size(queue, 1)
  % the first queue position determines the new values
  xv = queue(1,1);
  yv = queue(1,2);
  zv = queue(1,3);

  % .. and delete the first queue position
  queue(1,:) = [];

  % check the neighbors for the current position
  for i = -1:1
    for j = -1:1
      for k = -1:1

        if xv+i > 0  &&  xv+i <= nRow &&...          % within the x-bounds?
           yv+j > 0  &&  yv+j <= nCol &&...          % within the y-bounds?          
           zv+k > 0  &&  zv+k <= nSli &&...          % within the z-bounds?
           any([i, j, k])       &&...      % i/j/k of (0/0/0) is redundant!
           ~J(xv+i, yv+j, zv+k) &&...          % pixelposition already set?
           sqrt( (xv+i-initPos(1))^2 +...
                 (yv+j-initPos(2))^2 +...
                 (zv+k-initPos(3))^2 ) < maxDist &&...   % within distance?
           cIM(xv+i, yv+j, zv+k) <= (regVal + thresVal) &&...% within range
           cIM(xv+i, yv+j, zv+k) >= (regVal - thresVal) % of the threshold?

           % current pixel is true, if all properties are fullfilled
           J(xv+i, yv+j, zv+k) = true; 

           % add the current pixel to the computation queue (recursive)
           queue(end+1,:) = [xv+i, yv+j, zv+k];

           if tfMean
               regVal = mean(mean(cIM(J > 0))); % --> slow!
           end

        end        
      end
    end  
  end
end
%%% END OF REGION GROWING ALGORITHM


% loop through each slice, fill holes and extract the polygon vertices
P = [];
for cSli = 1:nSli
    if ~any(J(:,:,cSli))
        continue
    end

    % use bwboundaries() to extract the enclosing polygon
    if tfFillHoles
        % fill the holes inside the mask
        J(:,:,cSli) = imfill(J(:,:,cSli), 'holes');    
        B = bwboundaries(J(:,:,cSli), 8, 'noholes');
    else
        B = bwboundaries(J(:,:,cSli));
    end

    newVertices = [B{1}(:,2), B{1}(:,1)];

    % simplify the polygon via Line Simplification
    if tfSimplify
        newVertices = dpsimplify(newVertices, simplifyTolerance);        
    end

    % number of new vertices to be added
    nNew = size(newVertices, 1);

    % append the new vertices to the existing polygon matrix
    if isequal(nSli, 1) % 2D
        P(end+1:end+nNew, :) = newVertices;
    else                % 3D
        P(end+1:end+nNew, :) = [newVertices, repmat(cSli, nNew, 1)];
    end
end

% text output with final number of vertices
disp(['RegionGrowing Ending: Found ' num2str(length(find(J)))...
      ' pixels within the threshold range (' num2str(size(P, 1))...
      ' polygon vertices)!'])

如果我理解正确的话,您有一个肾脏边界的二值图像,现在需要将边界内部设置为 1 秒。为此,您可以使用imfill() http://www.mathworks.com/help/toolbox/images/ref/imfill.html功能与“孔”设置打开。

BW2 = imfill(BW,'holes');

编辑:看看代码,它似乎已经满足了您的要求。

% Outputs:
%   J: Binary mask (with the same size as the input image) indicating
%      1 (true) for associated pixel/voxel and 0 (false) for outside

所以你只需要得到第二个输出:

  IR=imread('nfliver5.jpg');
  figure, imshow(IR), hold all
  [poly im] = regionGrowing(IR,[],15,1200); % click somewhere inside the liver
  imshow(im,[])

Now im是带有分割区域的二值图像。

EDIT2:

一旦你有了二值图像im,您可以简单地使用逐元素乘法来删除分割区域之外的原始图像的所有部分。

SEG = IR.*im;
imshow(SEG,[]) 

EDIT3:

对于 3D 图像,您需要手动指定坐标,而不是使用鼠标。这是因为鼠标只给我们 2 个坐标(x 和 y),而您需要 3 个坐标(x、y 和 z)。因此,只需通过查看图像找到所需的坐标,然后选择合适的 z 坐标即可。

%Example coordinates, 
coordinates = [100 100 5] 
poly = regionGrowing(squeeze(IR), coordinates, 60, Inf, [], true, false);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

提取边界内的图像区域 的相关文章

  • Matlab PARFOR 循环可以通过编程方式打开/关闭吗?

    有一个关于 MATLAB 中 parfor 的简单问题 我想在程序中设置一个标志 以便在 parfor 和常规 for 循环之间进行更改 基本上 我需要此功能 以便我的代码的某些部分可以在 调试 模式下更新图形 然后当关闭该标志时 使用 p
  • 使用 K 均值聚类 OpenCV 进行交通标志分割

    I used K Means Clustering to perform segmentation on this traffic sign as shown below 这些是我的代码 读取图像并模糊 img cv imread 000
  • Matlab Mex文件编译

    我正在尝试编译一个 mex 文件以在 matlab 中使用套接字连接 问题是它总是说我没有安装sdk或编译器 但我已经安装了 Visual Studio 2010 Express Visual Studio 2012 Express Vis
  • 扩展 MATLAB 函数名称的最大长度

    我编写了一个 MATLAB 程序 可以动态创建自定义 MATLAB 函数 并使用以下命令在其他 MATLAB 实例中启动它们unix命令 我使用这个程序来自动化 fMRI 神经影像分析 使用 SPM8 for MATLAB 一切正常 但是
  • 如何在 Matlab 中使用谷歌翻译?

    我正在编写一个程序 使用 Matlab 列出电影字幕文件中的所有唯一单词 现在我有一个独特的单词列表 我想将其翻译成我的语言并在观看电影之前了解其含义 有谁知道如何在 Matlab 中使用 Google Translate 以便完成我的脚本
  • 在 matlab 中求 3d 峰的体积

    现在我有一个带有峰值的 3D 散点图 我需要找到其体积 我的数据来自图像 因此 x 和 y 值表示 xy 平面上的像素位置 z 值是每个像素的像素值 这是我的散点图 scatter3 x y z 20 z filled 我试图找到数据峰值的
  • 在 MATLAB 中重命名文件

    我正在尝试以编程方式重命名工作目录中的文件a temp txt to b hello txt 您建议如何这样做 MATLAB中有一个简单的文件重命名函数吗 我认为您正在寻找 MOVEFILE
  • Matlab 和 Python 中的优化算法(dog-leg trust-region)

    我正在尝试使用 Matlab 和 Python 中的狗腿信赖域算法求解一组非线性方程 在Matlab中有fsolve https www mathworks com help optim ug fsolve html其中此算法是默认算法 而
  • 如何将二进制值列表转换为int32类型?

    我在 MATLAB 工作区中有一个小端格式的二进制数列表 我想将它们转换为 int32 a是由 0 和 1 组成的双向量 如下所示 a 0 0 0 1 1 0 0 1 1 1 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1 1
  • 我需要转义该 MATLAB 字符串中的字符吗?

    我想在 MATLAB 中调用以下 bash 命令 grep Up to test linux vision1 1 log awk print 7 I use system 在MATLAB中 但结果有错误 gt gt status strin
  • matlab中类库的全局变量

    我有一些matlab声明的类 我如何声明所有类中都可见的常量 例如 这些常量可以是在所有类的方法中使用的物理常量 首先想到的是使用全局变量 还有更好的办法吗 最好在单独的文件中声明这些常量 包含常量的类是执行此操作的一种很好的干净方法 请参
  • 理解高斯混合模型的概念

    我试图通过阅读在线资源来理解 GMM 我已经使用 K 均值实现了聚类 并且正在了解 GMM 与 K 均值的比较 以下是我的理解 如有错误请指出 GMM 类似于 KNN 在这两种情况下都实现了聚类 但在 GMM 中 每个簇都有自己独立的均值和
  • MATLAB - 冲浪图数据结构

    我用两种不同的方法进行了计算 对于这些计算 我改变了 2 个参数 x 和 y 最后 我计算了每种变体的两种方法之间的 误差 现在我想根据结果创建 3D 曲面图 x gt on x axis y gt on y axis Error gt o
  • 图像处理 - 使用 opencv 进行服装分割

    我正在使用 opencv 进行服装特征识别 第一步 我需要通过从图像中移除脸部和手来分割 T 恤 任何建议表示赞赏 我建议采用以下方法 Use 阿德里安 罗斯布鲁克的用于检测皮肤的皮肤检测算法 谢谢罗莎 格隆奇以获得他的评论 在方差图上使用
  • 给定协方差矩阵,在Matlab中生成高斯随机变量

    Given a M x M期望的协方差 R 以及所需数量的样本向量 N计算一个N x M高斯随机向量 X在普通 MATLAB 中 即不能使用r mvnrnd MU SIGMA cases 不太确定如何解决这个问题 通常你需要一个协方差并且意
  • 正确使用 fft2 和 fftshift 进行着色形状

    我正在尝试从 Trucco Verri 文本 3d 计算机视觉入门技术 中看到的着色算法重新创建经典形状 但我很难理解 matlab 中的 fft 函数 本质上 我需要使用可积性约束来获取图像的深度 Z 我不确定在这种情况下何时使用 fft
  • Matlab Builder JA - 将 Matlab 编译成 Java jar - 免费版本?

    请记住 我对 Matlab 一无所知 Matlab Builder JA 允许开发人员构建 Matlab 应用程序并将其导出到 Java jar 中 太棒了 我只需要生成一个 jar 然后就可以从其他 java 代码中使用它 有谁知道单罐包
  • 检测分段常数信号中的阶跃

    我有一个分段恒定信号 如下所示 我想检测步骤转换的位置 标记为红色 我目前的做法 使用移动平均滤波器平滑信号 http www mathworks com help signal examples signal smoothing html
  • 如何知道Matlab中系统命令执行过程中经过的时间?

    我有一个运行系统脚本的 Matlab 代码 该脚本可能会因命令运行而停止 我想知道是否有一种方法可以让程序知道它是否花费了很长时间并执行其他操作 这是代码 tic status cmdout system iperfcmd The prog
  • 将 Matlab 的 datenum 格式转换为 Python

    我刚刚开始从 Matlab 迁移到 Python 2 7 在读取 mat 文件时遇到一些问题 时间信息以 Matlab 的日期数字格式存储 对于那些不熟悉它的人 日期序列号将日历日期表示为自固定基准日期以来已经过去的天数 在 MATLAB

随机推荐

  • Javascript 合并具有嵌套属性的对象

    让我们看一下下面的例子 var ref fullName rules type string minLength 4 maxLength 64 description Full name of a user var user fullNam
  • 如何将大文件导入到 PostgreSQL 中?

    我在一个新项目中 现在必须使用现有的 PostgreSQL 数据库 该应用程序是使用 CakePHP 构建的 我在导入时面临的问题是数据库的文件大小约为 4 8 GB 而我对 PostgreSQL 完全陌生 我曾研究过 MySql 和 No
  • 数据结构填充

    C 中的数据结构填充是什么以及如何检查填充字节的字节数 class a public int x int y int z 处理器要求某些类型的数据具有特定的对齐方式 例如 处理器可能需要int位于 4 字节边界上 因此 例如 一个int可以
  • LinkedBlockingQueue 的 Java 性能问题

    这是我在 stackoverflow 上的第一篇文章 我希望有人能帮助我 我的 Java 6 性能大幅下降LinkedBlockingQueue 在第一个线程中 我生成一些对象并将其推入队列 在第二个线程中 我将这些对象拉出来 当take
  • 如何在 Nunit 中调用 WPF Dispatcher?

    我想测试一个使用数据字段值呈现文本块的应用程序 渲染完成后 我想获得实际宽度和实际高度 一切正常 当我尝试测试该应用程序时 问题首先出现 我无法从测试项目调用调度程序 以下是代码 this Loaded s e gt TextBlock t
  • Xcode 6.1 - 如何卸载命令行工具?

    我通过发出安装了 Xcode 命令行工具xcode select install 现在我想卸载它 不卸载Xcode 我试过了 sudo Developer Library uninstall devtools mode all 但后来我得到
  • 模块依赖 - Android Studio

    我的项目 M1 M2 下有 2 个模块 Project M1 M2 早些时候 我将 M1 作为我的主要应用程序 然而 需要新的应用程序 M2 与 M1 共享许多通用内容 因此 我创建了一个新模块 M2 并以 M1 作为依赖项 为了实现这一点
  • 使用带有子存储库和子模块的深层 Git 存储库-存储库更改权限?

    我决定默认限制存储库树中的可见范围 以便为精美的东西创建公共文件夹 现在因为有很多子存储库 我得到了一个痛苦的提交或一些聪明的想法 我不确定最好的方法 我考虑过使用 find 遍历存储库 然后对每个存储库进行一致的虚拟提交 例如 defau
  • 如何将网络摄像头转为 rtsp

    我有一个产品 可以在输入 rtsp url 后分析视频 我想使用网络摄像头通过网络摄像头 rtsp 流式传输并提供我的产品 我怎样才能做到这一点 这取决于您使用的网络摄像头 大多数支持 RTSP 但许多不发布访问流的接口 因为它们设计为与网
  • 了解 Spark 创建的分区数量

    读取 csv 时 pyspark sql 将创建多少个分区 我对此的理解是 number of partitions math ceil file size spark conf get spark sql files maxPartiti
  • 如果行中的其他单元格与值匹配,则将单元格从一个工作表复制到另一个工作表

    我想要的是将单元格从一张纸复制到另一张纸 前提是同一行 不同列 中的另一个单元格在 Google 表格中具有特定值 理想情况下 我希望这是实时的 如果我在第一张表中添加一行并且条件匹配 第二张表也会更新 这是一个例子 Sheet one C
  • 无法从 docker 容器内部访问 datadog 代理

    我在 Amazon linux ec2 上安装了 dd agent 如果我直接在主机上运行 python 脚本 我使用名为 dogstatsd python 的 SDK 则所有指标都可以发送到 datadog 我登录到 datadoghq
  • 在 onStart() 之后直接调用 Fragment onStop() - 为什么?

    我的应用程序遇到了一个奇怪的问题 A 有一个包含片段的片段活动 该片段启动 AsyncTask onCreate 并取消 AsyncTask onStop 我的问题出现是因为 尽管我的 Fragment 保持运行并且没有被遮挡 但 onSt
  • 使用 Spring IoC 设置枚举值

    有没有办法在构造时通过 Spring IoC 设置此类枚举值 我想做的是在类加载时注入硬编码在下面的代码片段中的值 public enum Car NANO Very Cheap India MERCEDES Expensive Germa
  • 现有类型是否可以扩展以与 Seq.sum 等一起使用?

    最近一直在处理很多时间跨度 并且需要获得总和和平均值 但是 TimeSpan 既没有定义运算符 get Zero 也没有定义 DivideByInt 因此 Seq sum 和 Seq average 不能直接与此类型一起使用 以下情况无法编
  • PEP572 中的海象运算符示例

    中给出的示例之一PEP572 https www python org dev peps pep 0572 is Reuse a value that s expensive to compute y f x y 2 y 3 目前在 pyt
  • 变换后的子项在 Safari 中被父项背景剪裁,与 z-index 无关。为什么?

    我有这个加载器 在所有内容中都渲染得很好 但 Safari 除外 pageLoader min height 100vh min width 100vw background color white z index 2 position f
  • 更新 /.pub-cache 中的 flutter 依赖项

    我在文件夹里删除了 pub cache hosted pub dartlang org this plugin 更新里面的依赖的命令是什么pubsec yaml 我相信是的 颤振包获取 下的文件夹 pub cache仍然不是最新的 注意 有
  • 当视图模型没有域模型那么多的字段时,如何忽略/保留 MVC 中的值?

    我有一个网站 我正在使用 FluentNhibernate 和 Asp net MVC 我有一个编辑视图 允许用户编辑该记录 对象 的 10 个属性中的 8 个 当您提交表单和模型绑定时 两个不可编辑的字段将作为空字符串或默认日期时间值返回
  • 提取边界内的图像区域

    我必须使用 2D CT 图像做一个项目 并使用 Matlab 仅 对其中的肝脏和肿瘤进行分割 最初我必须单独分割肝脏区域 我使用区域生长进行肝脏分割 它获取种子点作为输入 输出是具有肝脏区域边界的图像 现在我需要仅由边界包围的区域 我的程序