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

Meghana Dinesh
Meghana Dinesh am 22 Aug. 2015
Bearbeitet: Meghana Dinesh am 24 Aug. 2015
I also tried using bwlabeln. But how can I remove (i.e. make = 0) those pixels which repeat between 20 to 70 times?
Walter Roberson
Walter Roberson am 24 Aug. 2015
What is size(numPixels) ?
Meghana Dinesh
Meghana Dinesh am 26 Aug. 2015
Bearbeitet: Meghana Dinesh am 26 Aug. 2015
As an example:
c is a 4X6X3 logical matrix.
c(:,:,1) =
0 0 0 0 0 1
1 0 0 1 0 0
0 1 1 1 0 0
0 0 0 0 0 0
c(:,:,2) =
0 0 0 0 0 1
1 0 0 1 0 0
0 1 1 1 0 0
0 0 0 0 0 0
c(:,:,3) =
0 0 0 0 0 1
1 0 0 1 0 0
0 1 1 1 0 0
0 0 0 0 0 0
CC = bwconncomp(c);
numPixels = cellfun(@numel,CC.PixelIdxList);
numPixels has a value [15,3]
and
size(numPixels) = 1 2 ;
Ultimately, my size of c will be around 3000 X 3000 X 3. In this, I want to remove the points (i.e., make their values = NaN) which have less than say 100 number of connected points.
With those sizes there is no reason why you should get the reported error message for idx = numPixels < 70 . If you had instead received an error message about being out of memory then that would be more understandable. "Maximum variable size allowed by the program is exceeded" is an error message that you would get on 32 bit MATLAB systems if you attempted to create an array that was larger than 2 gigabytes, and is not the error message given if you fill up your available memory.
If you do not need the actual number of pixels, just to know that the number is in the right range, then recall that you can use
inrange = @(x, a, b) a <= x & x <= b;
numPixelsInRange = cellfun( @(C) inrange(numel(C), 20, 70), CC.PixelIdxList);
this should save a small amount of memory at the expense of time.
Meghana Dinesh
Meghana Dinesh am 27 Aug. 2015
This is what I am doing:
I am first converting my point cloud (in this example, I have created a small 4 X 6 point cloud) into a logical matrix. (all NaNs = 0 while all other finite values = 1).
Here is a screenshot.
I don't seem to face any memory issues as of now.
Image Analyst
Image Analyst am 27 Aug. 2015
numPixels<10 produces a binary variable, true or false. It does not produce two numbers. There are not two numbers to try to stick into biggest, and idx like you're trying to do.
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

0 Stimmen

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

1 Stimme

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 28 Aug. 2015
Bearbeitet: Meghana Dinesh am 28 Aug. 2015
I am still getting the same error.
unwantedpixels = vertcat(unwantedCC.PixelIdxList(idx));
Meghana Dinesh
Meghana Dinesh am 28 Aug. 2015
Bearbeitet: Meghana Dinesh am 28 Aug. 2015
The line
unwantedCC = CC(idx);
gives me the error mentioned above.
So when I use:
unwantedpixels = vertcat(unwantedCC.PixelIdxList(idx));
I get unwantedCC as undefined.
Walter Roberson
Walter Roberson am 28 Aug. 2015
Bearbeitet: Walter Roberson am 28 Aug. 2015
Sorry, it is the early hours of the morning here.
Leave out the unwantedCC definition. Use
unwantedpixels = vertcat(CC.PixelIdxList{idx});
: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.

Community Treasure Hunt

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

Start Hunting!

Translated by