How to adjust 'measured distance' on a binary image

4 views (last 30 days)
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

Accepted Answer

Image Analyst
Image Analyst on 7 Jan 2022
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);

More Answers (1)

yanqi liu
yanqi liu on 7 Jan 2022
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 )

Community Treasure Hunt

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

Start Hunting!

Translated by