Find consecutive numbers below threshold using bwlabel

8 ビュー (過去 30 日間)
Konvictus177
Konvictus177 2022 年 6 月 1 日
コメント済み: Konvictus177 2022 年 6 月 3 日
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.

採用された回答

Steve Eddins
Steve Eddins 2022 年 6 月 2 日
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
Region #1 is 14 elements long and starts at element #11
  1 件のコメント
Konvictus177
Konvictus177 2022 年 6 月 3 日
Wow, exactly what I was looking for. Love the solution so far.
I will test it a bit more and come back in case of any issues.
Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with Image Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by