【问题标题】:Can I use bwboundaries and regionprops together on a labelmap?我可以在 labelmap 上同时使用 bwboundaries 和 regionprops 吗?
【发布时间】:2017-05-16 16:43:57
【问题描述】:

我正在使用 MATLAB 创建一个带有“对象”/“区域”的结构,可以在标记的图像中看到。我正在使用regionprops 提取属性BoundingBoxArea。因为它不幸(而且对我来说令人惊讶)无法提取 boundary (描绘该区域的像素列表),我正在使用bwboundary 创建那些)。不过这会带来一个问题:

obj = regionprops(obj_labelmap,'BoundingBox','Area','PixelIdxList'); 给出一个具有 N 个区域的 Nx1 结构,其中第 n 个区域对应于标签 n(例如,obj(1) 对应于标有 1 的区域)。

obj_boundaries = bwboundaries(obj_labelmap, 'noholes'); 我希望以同样的方式工作。它几乎可以,但仅适用于大约 80% 的标签。它任意混合了一些标签,所以obj_boundaries(1) 可能对应于标有5(或任何其他)的区域。

问题如下图所示,其中一个标签是 regionprops(边界框)索引,另一个标签是 obj_boundaries(边界)索引。该图是使用我上传的标签地图上的此代码生成的here

% get obj regions and boundaries from labelmap
obj_all = regionprops(obj_labelmap,'BoundingBox','Area','PixelIdxList');
obj_boundaries = bwboundaries(obj_labelmap, 'noholes');

imshow(obj_labelmap ~= 0); hold on;     % background image
colors=['b' 'g' 'r' 'c' 'm' 'y'];
for k=1:length(obj_boundaries),

    boundary = obj_boundaries{k};       % get boundary
    cidx = mod(k,length(colors))+1;     % set color
    plot(boundary(:,2), boundary(:,1),...
        colors(cidx),'LineWidth',1);

    % plot text for boundary
    % randomize text position for better visibility
    rndRow = ceil(length(boundary)/(mod(rand*k,7)+1));
    col = boundary(rndRow,2); row = boundary(rndRow,1);
    h = text(col+1, row-1, [num2str(k)]);% '|' num2str(obj_labelmap(row,col))]);
    set(h,'Color',colors(cidx),'FontSize',10,'FontWeight','bold');

    % plot bounding boxes
    bb = obj_all(k).BoundingBox;
    rectangle('Position', bb,...
        'EdgeColor', [0.7 0.7 0.7], 'LineWidth', 1)

    % plot text
    col = bb(1) ; row = bb(2);
    h = text(col+3, row+3, num2str(k));
    set(h,'Color',colors(cidx),'FontSize',10,'FontWeight','bold');
end

当试图将边界合并到结构中时,我必须为每个边界提取标签值并找到正确的边界或存储相应的(理想排序的)idx 来获取它。但这在计算上非常昂贵。

    % the order in which regionprops objects are stored corresponds to the labels 
    obj_all_idx = zeros(numel(obj_all), 1);
    for k=1:length(obj_all)
        pixelList = obj_all(k).PixelIdxList;
        obj_all_idx(k) = obj_labelmap(pixelList(1));
    end; clear k;

    % the order in which obj_boundaries are stored does NOT correspond to the labels 
    obj_boundaries_idx = zeros(numel(obj_boundaries), 1);
    for k=1:length(obj_boundaries)
        boundary = obj_boundaries{k};
        col = boundary(1,2); row = boundary(1,1);
        obj_boundaries_idx(k) = obj_labelmap(row,col);
    end; clear k;

我的问题是:有没有合并区域的边界、边界框和区域属性的好方法?我在监督什么吗?还有其他方法可以生成这些属性吗?

【问题讨论】:

    标签: matlab image-processing


    【解决方案1】:

    您可以直接在regionprops 输出上应用bwboundaries,这样就不会混淆。在'Image' 属性上这样做是最直接的,但也可以使用'PixelIdList'

    % generate binary image
    bw = imread('coins.png') > 100;
    % get region props
    props = regionprops(bw,{'Area','BoundingBox','Image','PixelIdxList'});
    % split props to cells
    C = struct2cell(props).';
    regions = C(:,3);
    bbox = C(:,2);
    % extract boundaries from 'Image' property
    B = cellfun(@(obj)bwboundaries(obj, 'noholes'),regions,'UniformOutput',0);
    % add bounding box offset to boundary coordinates
    B = cellfun(@(b,box) bsxfun(@plus,b{1},box([2 1]) - 0.5),B,bbox,'UniformOutput',0);
    % assign boundaries cell to props struct
    props(end).BWBoundary = [];
    [props(:).BWBoundary] = deal(B{:});
    % plot
    imshow(bw);
    hold on;
    for ii = 1:numel(B)
        plot(B{ii}(:,2),B{ii}(:,1),'LineWidth',3)
        text(mean(B{ii}(:,2)),mean(B{ii}(:,1)),num2str(ii));
    end
    

    【讨论】:

      猜你喜欢
      • 2019-07-11
      • 1970-01-01
      • 2011-07-21
      • 2015-05-19
      • 2019-05-21
      • 2021-09-05
      • 2014-04-08
      • 2020-06-23
      • 1970-01-01
      相关资源
      最近更新 更多