选择图像中的最大对象 [英] 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屋!