Draw rectangular contour in thresholded image

조회 수: 1 (최근 30일)
Nachiket Patki
Nachiket Patki 2018년 3월 20일
댓글: Nachiket Patki 2018년 4월 4일
Hello, I have a thresholded image of a bottle, where the liquid color is being masked and thresholded.
Now I want to find the height of that liquid present inside the bottle. What I am thinking is I will find the rectangular contour covering the liquid inside the bottle...and the height of the rectangle will give me a height of liquid. So how to draw react contour is the first question or is there any better way of finding height of liquid present?
Thank you

답변 (1개)

Image Analyst
Image Analyst 2018년 3월 20일
What you are saying is this:
binaryImage = bwareafilt(binaryImage, 1); % Extract largest blob only.
props = regionprops(binaryImage, 'BoundingBox');
height = props.BoundingBox(4)
This way is susceptible to little outliers, like the two you have on the top. A better way may be to either scan the blob vertically getting the height at every column and then taking the average
heights = zeros(rows, 1);
for col = 1 : columns
thisColumn = binaryImage(:, col);
if max(thisColumn) > 0 % If there are white pixels in the column
topRow = find(thisColumn, 1, 'first');
bottomRow = find(thisColumn, 1, 'last');
heights(col) = bottomRow - topRow + 1;
end
end
averageHeight = mean(heights(heights>0))
Or, get the average vertical profile and threshold it at some level
verticalProfile = sum(binaryImage, 2);
averageHeight = sum(verticalProfile > someLevel)
All 3 methods will/could give different heights. It just depends on how you want to define height. I mean, look at the bottom of your bottle - it's not flat so you don't have a perfect rectangle aligned with the edges of the image, so there are different ways to define height.
  댓글 수: 7
Image Analyst
Image Analyst 2018년 3월 25일
편집: Image Analyst 2018년 3월 25일
If you're going to write out four strings, you'll need four %s in the format specifier string.
n = handles.edit1.String;
r = handles.edit2.String;
b = handles.edit3.String;
y = handles.edit4.String;
outputFileID = fopen(fullOutputFileName, 'wt');
fprintf(outputFileID, '%s %s %s %s %s\n', n, r, b, y);
fclose(outputFileID);
Nachiket Patki
Nachiket Patki 2018년 4월 4일
Ohh I just missed that... Again thank you very much, sir. Your answers have always helped me.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Sequence and Numeric Feature Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by