【问题标题】:connected component analysis in MATLABMATLAB中的连通分量分析
【发布时间】:2015-11-05 01:23:02
【问题描述】:

我想在灰度图像上应用连通分量分析,并考虑灰度超过阈值的像素。然后,我想删除那些长度小于阈值的连接组件。请帮我?我在MATLAB中写了以下代码,效率高吗?
提前谢谢你。

%im = input image;
% alpha1 = 0.0001;
% alpha2 = 0.0001;
% [row col] = size(im);
% 
% 
% thr1 = mean(mean(im))-alpha1*std(std(im));
% BW = zeros(size(im));
% 
% for rr = 1:row
%     for cc = 1:col
%         if im(rr,cc)>thr2
%             BW(rr,cc) = 1;
%         else
%             BW(rr,cc) = 0;
%         end
%     end
% end
% 
% CC = bwconncomp(BW);
% area_in_pixels = cellfun(@length,CC.PixelIdxList);
% thr2 = mean(area_in_pixels)-alpha2*std(area_in_pixels);
% idx = find(area_in_pixels <= thr3);
% for  kk = 1:length(idx)
% aaa = idx(kk);
% BW(CC.PixelIdxList{aaa})=0;
% end

【问题讨论】:

  • 您可以将您的第一个双 for 循环替换为一行:BW = im &gt; thr2;
  • 谢谢。但这将我的数据类型从双重更改为逻辑
  • @bahar 然后就做BW = double(im &gt; thr2);
  • @rayryeng:非常感谢。

标签: matlab connected-components


【解决方案1】:

您可以尝试使用 regionprops 来提取图像中的所有对象。使用下面的代码,您可以获得小于阈值的所有对象的位置,您可以操作或执行之后需要执行的操作... 相比之下,您可以遍历不同的对象并提取灰度,如果它低于阈值,则对其进行操作。

    % Threshold for the size in pixels that you want
    threshold = 100; 

    % read your image    
    rawimage = imread('yourimage.jpg');

    % create a 2D field by summing 
    im = sum(rawimage,3);

    % label all objects that have 8 neighbours    
    IMAGE_labeled = bwlabel(im,8); 

    % get the properties of all elements
    shapedata=regionprops (IMAGE_labeled,'all'); 

    % get those elements that are smaller in size (area) than the threshold
    index = find(cell2mat({shapedata(:).Area})<=threshold); 

    % make a contourplot of im
    figure
    contourf(im)
    hold on

    % creation of colormap with the size of all identified objects below the thres
    mycolormap = jet(size(index,2));

    % loop over all small objects, extraction of their position in the original file, plotting circles with different colors at the position of each small object

   imap = 1;
   mean_of_red = zeros(length(index),1);
   for i = index
      plot (shapedata(i).PixelList(:,1),shapedata(i).PixelList(:,2),'o','MarkerFaceColor',mycolormap(imap,:))
      mean_of_red(i) = mean(mean(im(shapedata(i).PixelList(:,1),shapedata(i).PixelList(:,1),1)));
      imap=imap+1; 
   end

【讨论】:

  • 我认为您的index=... 有错误,应该类似于index=find([shapedata.Area] &lt; threshold)
  • @gregswiss:对,谢谢,已更正。我有一个只应该返回一个对象的早期版本......
  • @horseshoe:非常感谢。这很有帮助。只是,我不明白最后一个循环到底做了什么?找到索引后,我想在 rawimage 中删除与这些索引相关的连接组件。如何将这些索引与它们在 rawimage 中的连接组件相关联?
  • @bahar,我修改了代码,以便更清楚您可以做什么。 Pixellist 返回每个对象的位置,索引包含低于阈值的所有区域的每个索引。因此,如果您遍历索引,则可以对每个对象进行寻址并对其进行处理。在这里,我用不同颜色绘制了圆圈,并提取了图像红色部分的平均值...
  • @horseshoe:非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2011-09-14
  • 1970-01-01
  • 2013-05-28
  • 2011-06-01
  • 2011-07-26
  • 1970-01-01
  • 2019-09-29
  • 2011-05-23
相关资源
最近更新 更多