How can I find distance in a binarized image

5 views (last 30 days)
Hello all, I have binarized image and I want to calculate the distance in the image (marked in attached binarized image). I am attaching original and binarized pictures for reference. Thanks in advance.
  6 Comments
Rohit Thokala
Rohit Thokala on 3 Jan 2022
Hey @Turlough Hughes, I have attached origina and processed image now.

Sign in to comment.

Accepted Answer

Turlough Hughes
Turlough Hughes on 3 Jan 2022
Edited: Turlough Hughes on 3 Jan 2022
I originally answered this for the case of the binary image you represented in png format (this is not technically a binary image, but I know what you mean and we can work with it).
After seeing the original image (subsequently uploaded) I don't have enough information to know how to threshold the cloud around the jet impingment. One person may select a threshold that results in completely different boundaries (width and height) to the next person. It would be up to you to assess the definition of what you are trying to measure and to define what these boundaries really mean. Given all of that, I will not try to threshold your image, but rather address the original question, which is how to get the height and width (in pixels) from the binary image.
So, starting with a binary image, B, you can calculate the width and height as follows:
B2 = bwareafilt(B,1);
% Horizontal pixel width:
widthIndex = any(B2);
horizontalWidth = find(widthIndex,1,'last')-find(widthIndex,1,'first');
% Vertical pixel height
B3 = imerode(B2,strel('line',10,0));
verticalPixelHeight = find(any(B2,2),1,'last') - find(any(B3,2),1,'first');
The following is a demo for the above code:
First load and prepare the binary image:
I = imread('https://uk.mathworks.com/matlabcentral/answers/uploaded_files/851560/sample.png');
% converting the png to a Binary image which is roughly what you have in
% matlab.
B = imbinarize(rgb2gray(I));
figure('Visible',false), subplot(3,1,1)
imshow(B,'Border','tight')
title('Step 1')
You can start by selecting the largest component using the bwareafilt function. This removes all the smaller particles surrounding the largest component (incidentally this removes your annotations as well):
B2 = bwareafilt(B,1); % select the largest component with bwareafilt
subplot(3,1,2)
imshow(B2),
title('Step 2')
You can then take the horizontal width of the largest component as follows:
widthIndex = any(B2);
horizontalPixelWidth = find(widthIndex,1,'last')-find(widthIndex,1,'first')
horizontalPixelWidth = 451
In determining the vertical height of the component, it seems you want to remove the jets. You can do this using imerode, it's not perfect but it will give an approximate result:
% Vertical pixel height
% You can remove the jets by eroding the image with a horizontal line as
% the structing element. This will allow us to obtain the upper bound that
% you showed in sample.png, the lower bound can be obtained from the
% original binary image.
B3 = imerode(B2,strel('line',10,0));
subplot(3,1,3)
imshow(B3)
title('Step 3')
set(gcf,'Visible',true)
% The vertical height would then be the lowermost true pixel in B2 minus
% the uppermost true pixel in B3
verticalPixelHeight = find(any(B2,2),1,'last') - find(any(B3,2),1,'first')
verticalPixelHeight = 88
Finally:
figure(), imshow(B)
title('Result')
hold on
rectangle('Position', ...
[find(widthIndex,1,'first'),find(any(B3,2),1,'first'),...
horizontalPixelWidth,verticalPixelHeight],...
'EdgeColor','r','LineWidth',2 )
  2 Comments
Turlough Hughes
Turlough Hughes on 4 Jan 2022
I didn't actually show any area measurements, I showed width and height (in pixels). The bounding box just demonstrates how the values obtained relate to the image.
You seem to be looking for the width along which the cloud is in contact with the surface (atleast as it appears in the threshold). This is very different to getting the width and height of a single component, I suggest opening a new question.

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by