6

我试图找到图像中最大的对象并删除图像中小于它的任何其他对象。

这就是我所拥有的,但我无法让它工作。

 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 线照片。我正在尝试从图像中删除除乳房区域之外的所有对象。

4

2 回答 2

7

改为使用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使用“BoundingBox”选项来裁剪输出图像以使区域到达边缘:

s  = regionprops(BW2, 'BoundingBox');

它为您提供了一个矩形s.BoundingBox,您可以使用它来裁剪BW3 = imcrop(BW2,s.BoundingBox);.

于 2014-03-19T18:33:20.447 回答
5

如果您想继续使用该bwlabel方法,您可以使用这个 -

代码

BW = im2bw(imread('coins.png')); %%// Coins photo from MATLAB Library

[L, num] = bwlabel(BW, 8);
count_pixels_per_obj = sum(bsxfun(@eq,L(:),1:num));
[~,ind] = max(count_pixels_per_obj);
biggest_blob = (L==ind);

%%// Display the images
figure,
subplot(211),imshow(BW)
subplot(212),imshow(biggest_blob)

输出

在此处输入图像描述

于 2014-03-19T18:42:14.600 回答