Advertisement

matlab图像分割(以草莓为例),区域生长,区域分割

阅读量:

该文本介绍了数字图像处理中的几种主要分割方法及其应用场景: 1. 区域生长(Region Growing):通过设定种子点并结合阈值条件对图像进行迭代扩展以实现目标区域的分离。 2. 分裂合并(Split Merge):基于四叉树分解的算法对图像进行层次化划分,并根据预设条件动态调整块大小以获得更精确的分割结果。 3. 分水岭分割:利用数学形态学中的距离变换和分水岭算法对图像进行全局优化分割,在复杂背景中表现较为稳定但可能不如其他方法精确。 这些方法广泛应用于医学成像、遥感影像分析等领域中对复杂场景的自动目标识别与分离任务中。

分割效果略显粗糙,在视觉上显得不够精细。原始代码源自冈萨雷斯所著的《数字图像处理》一书。获取电子版教材需通过后台操作进行查询。

分割效果略显粗糙,在视觉上显得不够精细。原始代码源自冈萨雷斯所著的《数字图像处理》一书。获取电子版教材需通过后台操作进行查询。

该区域动态扩展以实现整体增长

RegionGrow函数需要创建一个新的script文件。

复制代码
 function [g, NR, SI, TI] = regiongrow(f, S, T)

    
  
    
 %REGIONGROW Perform segmentation by region growing.
    
  
    
 % [G, NR, SI, TI] = REGIONGROW(F, SR, T). S can be an array (the
    
  
    
 % same size as F) with a 1 at the coordinates of every seed point
    
  
    
 % and 0s elsewhere. S can also be a single seed value. Similarly,
    
  
    
 % T can be an array (the same size as F) containing a threshold
    
  
    
 % value for each pixel in F. T can also be a scalar, in which
    
  
    
 % case it becomes a global threshold.
    
  
    
 %
    
  
    
 % On the output, G is the result of region growing, with each
    
  
    
 % region labeled by a different integer, NR is the number of
    
  
    
 % regions, SI is the final seed image used by the algorithm, and TI
    
  
    
 % is the image consisting of the pixels in F that satisfied the
    
  
    
 % threshold test.
    
  
    
 % Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
    
  
    
 % Digital Image Processing Using MATLAB, Prentice-Hall, 2004
    
  
    
 % $Revision: 1.4 $ $Date: 2003/10/26 22:35:37 $
    
  
    
 f = double(f);
    
  
    
 % If S is a scalar, obtain the seed image.
    
 f=im2gray(f);
    
  
    
 if numel(S) == 1
    
  
    
 SI = f == S;
    
  
    
 S1 = S;
    
  
    
 else
    
  
    
 % S is an array. Eliminate duplicate, connected seed locations
    
  
    
 % to reduce the number of loop executions in the following
    
  
    
 % sections of code.
    
  
    
 SI = bwmorph(S, 'shrink', Inf);
    
  
    
 J = find(SI);
    
  
    
 S1 = f(J); % Array of seed values.
    
  
    
 end
    
  
    
 TI = false(size(f));
    
  
    
 for K = 1:length(S1)
    
  
    
 seedvalue = S1(K);
    
  
    
 S = abs(f - seedvalue) <= T;
    
  
    
 TI = TI | S;
    
  
    
 end
    
  
    
 % Use function imreconstruct with SI as the marker image to
    
  
    
 % obtain the regions corresponding to each seed in S. Function
    
  
    
 % bwlabel assigns a different integer to each connected region.
    
  
    
 [g, NR] = bwlabel(imreconstruct(SI, TI));

主函数用于转移至另一份脚本中:

复制代码
 clc

    
 clear
    
 f = imread('E:\数字图像\第二次作业\草莓.jpg');
    
 subplot(221),imshow(f);
    
 title('原始图像');
    
 %函数regiongrow返回的NR为是不同区域的数目,参数SI是一副含有种子点的图像
    
 %TI是包含在经过连通前通过阈值测试的像素
    
 f=im2gray(f);
    
 [g,NR,SI,TI]=regiongrow(f,100,55);%种子的像素值为100,55为阈值
    
 subplot(222),imshow(SI);
    
 title('种子点的图像');
    
 subplot(223),imshow(TI);
    
 title('所有通过阈值测试的像素');
    
 subplot(224),imshow(g);
    
 title('对种子点进行8连通分析后的结果');

在资源管理中进行区域划分是一项至关重要的操作

该splitmerge功能模块已成功创建新脚本。

复制代码
 function g=splitmerge(f,mindim,fun)%f是待分割的原图,mindim是定义分解中所允许的最小的块,必须是2的正整数次幂

    
 f= rgb2gray(f); % 如果 f 是 RGB 彩色图像
    
  Q=2^nextpow2(max(size(f)));
    
  [M,N]=size(f);
    
  padsize = max([Q-M, Q-N], 0);
    
  f=padarray(f,padsize,'post');%:填充图像或填充数组。f是输入图像,输出是填充后的图像,先将图像填充到2的幂次以使后面的分解可行
    
  %然后是填充的行数和列数,post:表示在每一维的最后一个元素后填充,B = padarray(A,padsize,padval,direction)
    
  %不含padval就用0填充,Q代表填充后图像的大小。
    
  S=qtdecomp(f,@split_test,mindim,fun);%S传给split_test,qtdecomp divides a square image into four
    
 % equal-sized square blocks, and then tests each block to see if it
    
 % meets some criterion of homogeneity. If a block meets the criterion,
    
 % it is not divided any further. If it does not meet the criterion,
    
 % it is subdivided again into four blocks, and the test criterion is
    
 % applied to those blocks. This process is repeated iteratively until
    
 % each block meets the criterion. The result can have blocks of several
    
 % different sizes.S是包含四叉树结构的稀疏矩阵,存储的值是块的大小及坐标,以稀疏矩阵形式存储
    
  Lmax=full(max(S(:)));%将以稀疏矩阵存储形式存储的矩阵变换成以普通矩阵(full matrix)形式存储,full,sparse只是存储形式的不同
    
  g=zeros(size(f));
    
  MARKER=zeros(size(f));
    
  
    
  for k=1:Lmax
    
      [vals,r,c]=qtgetblk(f,S,k);%vals是一个数组,包含f的四叉树分解中大小为k*k的块的值,是一个k*k*个数的矩阵,
    
      %个数是指S中有多少个这样大小的块,f是被四叉树分的原图像,r,c是对应的左上角块的坐标如2*2块,代表的是左上角开始块的坐标
    
      
    
      if ~isempty(vals)
    
      for I=1:length(r)
    
          xlow=r(I);
    
          ylow=c(I);
    
          xhigh=xlow+k-1;
    
          yhigh=ylow+k-1;
    
          region=f(xlow:xhigh,ylow:yhigh);%找到对应的区域
    
          flag=feval(fun,region);%evaluates the function handle, fhandle,using arguments x1 through xn.执行函数fun,region是参数
    
          if flag%如果返回的是1,则进行标记
    
              g(xlow:xhigh,ylow:yhigh)=1;%然后将对应的区域置1
    
              MARKER(xlow,ylow)=1;%MARKER矩阵对应的左上角坐标置1
    
          end
    
      end
    
      end
    
  end
    
  
    
  g=bwlabel(imreconstruct(MARKER,g));%imreconstruct默认2D图像8连通,这个函数就是起合的作用
    
  g=g(1:M,1:N);%返回原图像的大小
    
 end
    
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
  
    
 function v=split_test(B,mindim,fun)
    
  K=size(B,3);%B就是qtdecomp函数传过来的,代表当前size(B,3)返回的是B的层数,就是B是几维的,这里实际上就是有几个B这样大小的图像块
    
  %这句代码的意思是从qtdecomp函数传过来的B,是当前分解成的K块的m*m的图像块,K表示有多少个这样大小的图像块
    
  v(1:K)=false;
    
  for I=1:K
    
      quadregion=B(:,:,I);
    
      if size(quadregion,1)<=mindim%如果分的块的大小小于mindim就直接结束
    
      v(I)=false;
    
      continue
    
      end
    
      flag=feval(fun,quadregion);%quadregion是fun函数的参数
    
      if flag%如果flag是1,代表需要再分
    
      v(I)=true;%这里就相当于split_test是起一个调用predicate的作用,返回的就是ppredicate的值
    
      end
    
  end
    
 end
    
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%

该系统应具备支持谓词功能模块的能力,并要求开发人员编写相关代码。

复制代码
 function flag = predicate( region )

    
 sd = std2(region);
    
 m = mean2(region);
    
 flag = (sd > 10) & (m > 0) & (m < 255);
    
 end

在编程环境中,建议创建一个新项目文件作为主程序。

复制代码
 clc

    
 clear
    
 f = imread('E:\数字图像\第二次作业\草莓.jpg');
    
 subplot(231),imshow(f);
    
 title('区域分割原始图像');
    
 g32=splitmerge(f,32,@predicate);%32代表分割中允许最小的块
    
 subplot(232),imshow(g32);
    
 title('mindim为32时的分割图像');
    
 g16=splitmerge(f,16,@predicate);%32代表分割中允许最小的块
    
 subplot(233),imshow(g16);
    
 title('mindim为16时的分割图像');
    
 g8=splitmerge(f,8,@predicate);%8代表分割中允许最小的块
    
 subplot(234),imshow(g8);
    
 title('mindim为8时的分割图像');
    
 g4=splitmerge(f,4,@predicate);%4代表分割中允许最小的块
    
 subplot(235),imshow(g4);
    
 title('mindim为4时的分割图像');
    
 g2=splitmerge(f,2,@predicate);%2代表分割中允许最小的块
    
 subplot(236),imshow(g2);
    
 title('mindim为2时的分割图像');

采用分水岭分割方法后发现其效果略逊于前面两种方法,然而代码实现较为简单。

复制代码
 I=imread('E:\数字图像\第二次作业\草莓.jpg');

    
 g=im2bw(I,graythresh(I));
    
 figure(1),subplot(321),imshow(g);
    
 gc=~g;
    
 subplot(322),imshow(gc);
    
 d=bwdist(gc);
    
 figure(1),subplot(323),imshow(d);
    
 L=watershed(-d);
    
 figure(1),subplot(324),imshow(L);
    
 w=L==0;
    
 g2=g&~w;
    
 figure(1),subplot(325),imshow(g2);
    
 title('分割图');
    
 %距离变换分水岭分割

结果大概是这样的情况

基础图形

区域生长是一种复杂的空间分析技术,在生态学和城市规划等领域中具有广泛的应用价值。它通过模拟不同区域在特定条件下的发展过程,在分析土地利用变化、城市扩张模式以及生态修复潜力方面提供了科学依据。该方法能够有效识别影响区域发展的关键因素,并预测未来的发展趋势,从而为相关领域的决策者提供理论支持和实践参考。

区域划分:

该算法基于图像的空间信息与灰度信息进行图像处理。
其基本概念在于将图像划分为一系列连续且有界的区域。
由于其高效的计算能力和良好的性能,在模式识别、遥感解译等领域得到了广泛应用。

此为区分不同类别或阶段的重要分界线:

全部评论 (0)

还没有任何评论哟~