doubt regarding bwconncomp example.

5 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 28 Aug 2015
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 on 28 Aug 2015
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?

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 22 Aug 2015
Use bwareafilt instead.
  2 Comments
Meghana Dinesh
Meghana Dinesh on 24 Aug 2015
Edited: Meghana Dinesh on 24 Aug 2015
My data is in 3D.
Image Analyst
Image Analyst on 29 Aug 2015
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.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 28 Aug 2015
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 Comments
Meghana Dinesh
Meghana Dinesh on 29 Aug 2015
: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 on 29 Aug 2015
unwantedpixels = cell2mat(CC.PixelIdxList(idx));

Sign in to comment.

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!