【发布时间】:2017-05-16 16:43:57
【问题描述】:
我正在使用 MATLAB 创建一个带有“对象”/“区域”的结构,可以在标记的图像中看到。我正在使用regionprops 提取属性BoundingBox 和Area。因为它不幸(而且对我来说令人惊讶)无法提取 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;
我的问题是:有没有合并区域的边界、边界框和区域属性的好方法?我在监督什么吗?还有其他方法可以生成这些属性吗?
【问题讨论】: