Find consecutive numbers below threshold using bwlabel
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Konvictus177
il 1 Giu 2022
Commentato: Konvictus177
il 3 Giu 2022
Hi,
I want to find all consecutive values and their region in a vector that are below a certain threshold.
I am able to do this using the following code. In this example I find 5 regions below the set threshold which is correct.
y = [1 2 1 2 3 4 5 4 3 2 0.4 0.3 0.4 0.4 0.6 0.4 0.5 0.4 0.6 0.5 0.3 0.5 0.6 0.2 1 2 3 4 1 2 3 2 3 2 5 4]
plot(y)
lowThreshold = 0.5
% label points below threshold as 1
[L, count] = bwlabel(y < lowThreshold); % label waves below threshold
% Get lengths of each region
props = regionprops(L, 'Area', 'PixelList');
regionLengths = [props.Area];
for k = 1 : count
fprintf('Region #%d is %d elements long and starts at element #%d\n',...
k, regionLengths(k), props(k).PixelList(1,1));
end
Now I want to modify this code to count the 5 regions as 1 connected big region although I exceed my threshold by a tiny amount. How can I do this? Take first index of region 1 and last index of region 5 and count as 1 large region. For example:
Thanks.
0 Commenti
Risposta accettata
Steve Eddins
il 2 Giu 2022
You could a hysteresis thresholding technique. First, threshold with a strict threshold, like your 0.5. Then, threshold with a slightly looser threshold, perhaps 0.1. Next, allow the strict threshold result to grow into adjacent regions that are included in the looser threshold result. The Image Processing Toolbox function imreconstruct can be used to do this. Here's a modified version of your code:
y = [1 2 1 2 3 4 5 4 3 2 0.4 0.3 0.4 0.4 0.6 0.4 0.5 0.4 ...
0.6 0.5 0.3 0.5 0.6 0.2 1 2 3 4 1 2 3 2 3 2 5 4];
plot(y)
lowThreshold = 0.5;
medThreshold = 0.7;
mask1 = y < lowThreshold;
mask2 = y < medThreshold;
combined_mask = imreconstruct(mask1,mask2);
% Get lengths of each region.
% Note that regionprops can take a binary mask directly as input.
props = regionprops(combined_mask, 'Area', 'PixelList');
regionLengths = [props.Area];
for k = 1 : numel(props)
fprintf('Region #%d is %d elements long and starts at element #%d\n',...
k, regionLengths(k), props(k).PixelList(1,1));
hold on
[r1,r2] = bounds(props(k).PixelList(:,1));
plot([r1 r2],[0.5 0.5],'Color','r','Linewidth',3)
hold off
end
Più risposte (0)
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!