doubt regarding bwconncomp example.

1 次查看(过去 30 天)
This is a small doubt.....I'm stuck in the second example given for bwconncomp by Mathworks. This example removes the group of pixels having largest pixels connected (in an 8 connected neighbourhood):
BW = imread('text.png');
imshow(BW);
CC = bwconncomp(BW);
numPixels = cellfun(@numel,CC.PixelIdxList);
[biggest,idx] = max(numPixels);
BW(CC.PixelIdxList{idx}) = 0;
figure, imshow(BW);
I want to change the code to erase groups of pixels which have between 20 to 70 connected pixels (in their 8 connected neighbourhood). How can I do this?
If I do:
idx = numPixels < 70;
I get an error saying : " Maximum variable size allowed by the program is exceeded."
  8 个评论
Walter Roberson
Walter Roberson 2015-8-28
It looks like you are trying to find a maximum, but it is not clear what you are trying to find a maximum of.
idx = 20 <= numPixels & numPixels <= 70;
valididx = find(idx);
validsizes = numPixels(idx);
if isempty(valididx)
lastvalid = [];
else
lastvalid = valididx(end);
end
[sizelargest, idxlargest] = max(validsizes);
ccidxlargest = valididx(idxlargest);
lastvalid would be for the case where you want to find the index (into CC) of the last cluster that is in the size range -- the maximum being taken over the indices
ccidxlargest would be for the case whee you want to find the index (into CC) of the largest cluster that is left after you isolate down to that range of sizes -- the maximum being taken over the remaining sizes.
Meghana Dinesh
Meghana Dinesh 2015-8-28
Maybe my approach is wrong.
This is my goal: I have a pointCloud (a 3D matrix including Inf, NaN and finite values). I want to remove the groups of all those points ("remove" i.e., make them = NaN) which have lesser than N (say, 100) points connected to it (in a 26-connectivity neighbourhood).
In order to do this, I am performing the steps mentioned above (converting all finite values to logical and applying bwconncomp).
Are my steps right?

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2015-8-22
Use bwareafilt instead.
  2 个评论
Image Analyst
Image Analyst 2015-8-29
If you want to " want to remove the groups of all those points ("remove" i.e., make them = NaN) which have lesser than N (say, 100) points connected to it (in a 26-connectivity neighbourhood)." then the function you want to use is definitely 100% bwareaopen(). It's meant for exactly that. No need to get PixelIdxList at all.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2015-8-28
N = 100;
a = imread('text.png');
c = a;
c(~isfinite(c)) = 0;
BW = logical(c);
CC = bwconncomp(BW);
numPixels = cellfun(@numel,CC.PixelIdxList);
idx = numPixels < N;
unwantedCC = CC(idx);
unwantedpixels = vertcat(unwantedCC.PixelIdxList);
b = a;
b(unwantedpixels) = NaN;
  6 个评论
Meghana Dinesh
Meghana Dinesh 2015-8-29
:D Nevermind! Anyway, thanks for your help so far.
I am still not able to execute since the variable unwantedpixels is a cell. So the line:
b(unwantedpixels) = NaN;
gives an error saying: Function 'subsindex' is not defined for values of class 'cell'.
Walter Roberson
Walter Roberson 2015-8-29
unwantedpixels = cell2mat(CC.PixelIdxList(idx));

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by