我试图找到图像中最大的对象并删除图像中小于它的任何其他对象。
这就是我所拥有的,但我无法让它工作。
l=bwlabel(BW);
%the area of all objects in the image is calculated
stat = regionprops(l,'Area','PixelIdxList');
[maxValue,index] = max([stat.Area]);
%remove any connected areas smaller than the biggest object
BW2=bwareaopen(BW,[maxValue,index],8);
subplot(5, 5, 4);
imshow(BW2, []);
我正在处理数字乳房 X 线照片,例如 these .我正在尝试从图像中删除除乳房区域以外的所有对象。
最佳答案
使用bwconncomp
相反,因为它在单独的单元格中返回区域的坐标索引,其中每个单元格的大小很容易辨别:
>> BW = [1 0 0; 0 0 0; 0 1 1]; % two regions
>> CC = bwconncomp(BW)
CC =
Connectivity: 8
ImageSize: [3 3]
NumObjects: 2
PixelIdxList: {[1] [2x1 double]}
PixelIdxList
字段是一个元胞数组,其中包含每个区域的坐标索引。每个数组的长度就是每个区域的大小:
>> numPixels = cellfun(@numel,CC.PixelIdxList)
numPixels =
1 2
>> [biggestSize,idx] = max(numPixels)
biggestSize =
2
idx =
2
然后你可以很容易地用这个组件制作一个新图像:
BW2 = false(size(BW));
BW2(CC.PixelIdxList{idx}) = true;
编辑:根据评论,裁剪输出图像以使区域到达边缘的需要可以用 regionprops
解决。使用“边界框”选项:
s = regionprops(BW2, 'BoundingBox');
这会为您提供一个矩形 s.BoundingBox
,您可以使用 BW3 = imcrop(BW2,s.BoundingBox);
进行裁剪。
关于matlab - 选择图像中最大的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22514668/