doubt regarding bwconncomp example.
이전 댓글 표시
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
Meghana Dinesh
2015년 8월 22일
편집: Meghana Dinesh
2015년 8월 24일
Walter Roberson
2015년 8월 24일
What is size(numPixels) ?
Meghana Dinesh
2015년 8월 26일
편집: Meghana Dinesh
2015년 8월 26일
Walter Roberson
2015년 8월 26일
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
2015년 8월 27일
Image Analyst
2015년 8월 27일
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.
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
2015년 8월 28일
채택된 답변
추가 답변 (1개)
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
2015년 8월 28일
편집: Meghana Dinesh
2015년 8월 28일
Walter Roberson
2015년 8월 28일
unwantedpixels = vertcat(unwantedCC.PixelIdxList(idx));
Meghana Dinesh
2015년 8월 28일
편집: Meghana Dinesh
2015년 8월 28일
Walter Roberson
2015년 8월 28일
편집: Walter Roberson
2015년 8월 28일
Sorry, it is the early hours of the morning here.
Leave out the unwantedCC definition. Use
unwantedpixels = vertcat(CC.PixelIdxList{idx});
Meghana Dinesh
2015년 8월 29일
Walter Roberson
2015년 8월 29일
unwantedpixels = cell2mat(CC.PixelIdxList(idx));
카테고리
도움말 센터 및 File Exchange에서 Region and Image Properties에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

