How to adjust 'measured distance' on a binary image

7 次查看(过去 30 天)
I have code for radius and height measurements for spray images. This code is overestimating the spray cloud height (i.e., rectangle height). I want to measure spray cloud height with out imcoming spray influence. I am attaching my code and reference image here. the height I want to measure is shown in the below picture with arrows. Thanks in advance

采纳的回答

Image Analyst
Image Analyst 2022-1-7
You can sum the white pixels horizontally and then threshold it at some percentage of the max width
verticalProfile = sum(mask, 2);
maxWidth = max(verticalProfile);
percentage = 0.5; % Adjust to take more or less of the blob.
topLine = find(verticalProfile > percentage * maxWidth, 1, 'first')
bottomLine = find(verticalProfile > percentage * maxWidth, 1, 'last')
% Draw lines there
yline(topLine, 'Color', 'r', 'LineWidth', 2);
yline(bottomLine, 'Color', 'r', 'LineWidth', 2);

更多回答(1 个)

yanqi liu
yanqi liu 2022-1-7
clc; clear all; close all;
B = imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/853535/binary_image.jpg');
B = im2bw(B);
figure; subplot(3,1,1)
imshow(B,'Border','tight')
title('Step 1')
B2 = bwareafilt(B,1); % select the largest component with bwareafilt
subplot(3,1,2)
imshow(B2),
title('Step 2')
widthIndex = any(B2);
horizontalPixelWidth = find(widthIndex,1,'last')-find(widthIndex,1,'first');
B3 = imerode(B2,strel('line',10,0));
% use sum for every row
cs = sum(B3, 2);
mcs = max(cs);
inds = find(cs>mcs*0.3);
subplot(3,1,3)
imshow(B3)
title('Step 3')
set(gcf,'Visible',true)
verticalPixelHeight = find(any(B2,2),1,'last') - inds(1);
figure; imshow(B)
title('Result')
hold on
rectangle('Position', ...
[find(widthIndex,1,'first'), inds(1),...
horizontalPixelWidth,verticalPixelHeight],...
'EdgeColor','r','LineWidth',2 )

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by