use*_*871 6 matlab image-processing morphological-analysis
我试图找到图像中最大的对象,并删除图像中小于它的任何其他对象.
这就是我所拥有的,但我无法让它发挥作用.
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, []);
Run Code Online (Sandbox Code Playgroud)
我正在使用这些数字乳房X线照片.我试图从乳房区域除去图像中的所有对象.
请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]}
Run Code Online (Sandbox Code Playgroud)
该PixelIdxList字段是一个单元格数组,其中包含每个区域的坐标索引.每个数组的长度是每个区域的大小:
>> numPixels = cellfun(@numel,CC.PixelIdxList)
numPixels =
1 2
>> [biggestSize,idx] = max(numPixels)
biggestSize =
2
idx =
2
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用此组件轻松创建新图像:
BW2 = false(size(BW));
BW2(CC.PixelIdxList{idx}) = true;
Run Code Online (Sandbox Code Playgroud)
编辑:从评论中,需要裁剪输出图像,以便区域到达边缘可以regionprops使用'BoundingBox'选项来解决:
s = regionprops(BW2, 'BoundingBox');
Run Code Online (Sandbox Code Playgroud)
它为您提供了一个s.BoundingBox可以用来裁剪的矩形BW3 = imcrop(BW2,s.BoundingBox);.
如果您想继续这种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)
Run Code Online (Sandbox Code Playgroud)
产量
