doubt regarding bwconncomp example.

2 Ansichten (letzte 30 Tage)
Meghana Dinesh
Meghana Dinesh am 21 Aug. 2015
Kommentiert: Image Analyst am 29 Aug. 2015
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 Kommentare
Walter Roberson
Walter Roberson am 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 am 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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by