我正在使用MATLAB创建一个带有“对象”/“区域”的结构,可以在带标签的图像中看到。我正在使用regionprops
提取属性 BoundingBox 和区域。因为不幸(令我惊讶的是)它无法提取边界(描绘该区域的像素列表)我正在创建那些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(BoundingBox)索引,另一个标签是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;
我的问题是:有没有一种很好的方法来合并区域的边界,BoundingBox和Area属性?我是否在监督某些事情?还有另一种方法来生成这些属性吗?
答案 0 :(得分:1)
您可以直接在bwboundaries
输出上应用regionprops
,这样就不会产生混淆。在'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