选择图像中最大的对象 [英] Select largest object in an image
问题描述
我试图找到图像中最大的对象,并删除图像中小于它的任何其他对象.
I am trying to find the the largest object in an image and remove any other objects in the image that are smaller than it.
这就是我所拥有的,但我无法让它工作.
This is what I have but I cannot get it to work.
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 光照片,例如 这些.我正在尝试从图像中删除除乳房区域之外的所有对象.
I am working with digital mammograms such as these. I am trying to remove all objects from the image except for the breast region.
推荐答案
使用 bwconncomp
因为它返回单独单元格中区域的坐标索引,其中每个单元的大小很容易辨别:
Use bwconncomp
instead since it returns the coordinate indexes for region in a separate cell, where the size of each is easily discerned:
>> 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
字段是一个元胞数组,其中包含每个区域的坐标索引.每个数组的长度就是每个区域的大小:
The PixelIdxList
field is a cell array with the indexes of coordinates for each region. The length of each array is the size of each region:
>> numPixels = cellfun(@numel,CC.PixelIdxList)
numPixels =
1 2
>> [biggestSize,idx] = max(numPixels)
biggestSize =
2
idx =
2
然后你可以很容易地用这个组件制作一个新的图像:
Then you can easily make a new image with just this component:
BW2 = false(size(BW));
BW2(CC.PixelIdxList{idx}) = true;
<小时>
编辑:根据评论,可以使用 regionprops
使用BoundingBox"选项:
EDIT: From the comments, the need to crop the output image so that the region comes to the edges can be addressed with regionprops
using the 'BoundingBox' option:
s = regionprops(BW2, 'BoundingBox');
它给你一个矩形 s.BoundingBox
,你可以用它来裁剪 BW3 = imcrop(BW2,s.BoundingBox);
.
which gives you a rectangle s.BoundingBox
which you can use to crop with BW3 = imcrop(BW2,s.BoundingBox);
.
这篇关于选择图像中最大的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!