How can I find distance in a binarized image

2 次查看(过去 30 天)
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 个评论
Turlough Hughes
Turlough Hughes 2022-1-3
My answer (as well as the question title) don't make sense anymore. Can you keep the png of the binarized image (as well as the original).

请先登录,再进行评论。

采纳的回答

Turlough Hughes
Turlough Hughes 2022-1-3
编辑:Turlough Hughes 2022-1-3
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 个评论
Rohit Thokala
Rohit Thokala 2022-1-4
Hi @Turlough Hughes thank you for your help. what can I do if I want to measure some distance instead of area and automate the process to other images. imdistline command works for individual images on output window but I want to know are there any other options available?
Turlough Hughes
Turlough Hughes 2022-1-4
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.

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by