Draw rectangular contour in thresholded image

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

Answers (1)

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 Comments

Thank you very much sir. Sir I have another task where I have a text file which contains information in three rows like this...
Name1 5 EC 256 ACPCE
Name2 5 EC 253 ACPCE
Name2 15 EC 255 ACPCE
these three line. If X contdition occurs I want to access first line(access and store string in variable), if Y occurs second and if none then third, how can do this?
Try this:
% Open the file.
fileID = fopen(fullFileName, 'rt');
if fileID ~= -1
% If file could be opened...
% Skip the proper number of lines.
if X
linesToSkip = 0;
elseif Y
linesToSkip = 1;
else
linesToSkip = 2;
end
for k = 1 : linesToSkip
% Read and throw away lines of the file.
fprintf('Skipping Line #%d: %s\n', linesToSkip+1, textLine);
% Read the next line.
textLine = fgetl(fileID);
end
fprintf('Reading Line #%d: %s\n', linesToSkip+1, textLine);
% Read the next line of the file.
textLine = fgetl(fileID);
% All done reading all lines, so close the file.
fclose(fileID);
else
errorMessage = sprintf('Error opening %s', fullFileName);
uiwait(errordlg(errorMessage));
end
Sir, can you tell me what is that 'textline'? It's giving me an error saying textline is undefined
Try this:
% Open the file.
fileID = fopen(fullFileName, 'rt');
if fileID ~= -1
% If file could be opened...
% Skip the proper number of lines.
if X
linesToSkip = 0;
elseif Y
linesToSkip = 1;
else
linesToSkip = 2;
end
for k = 1 : linesToSkip
% Read and throw away lines of the file.
fprintf('Skipping Line #%d: %s\n', linesToSkip+1, textLine);
% Read the next line.
textLine = fgetl(fileID);
end
% Read the next line of the file.
textLine = fgetl(fileID);
fprintf('Read Line #%d: %s\n', linesToSkip+1, textLine);
% All done reading all lines, so close the file.
fclose(fileID);
else
errorMessage = sprintf('Error opening %s', fullFileName);
uiwait(errordlg(errorMessage));
end
Thank you very much sir. This was one part of the project. The second part is I want to save info into text file (in the same format above) which will be supplied from the GUI. In simple words I want to do exactly the reverse of this what we are trying to do now. The same info has to be entered into text via inputs from GUI. So I use...
n=(get(handles.edit1,'string'));
r=(get(handles.edit2,'string'));
b=(get(handles.edit3,'string'));
y=(get(handles.edit4,'string'));
fprintf(fileID,'%s %s %s %s\n',n,r,b,y);
It's not working I am getting an error... where am I going wrong?
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);
Ohh I just missed that... Again thank you very much, sir. Your answers have always helped me.

Sign in to comment.

Categories

Asked:

on 20 Mar 2018

Commented:

on 4 Apr 2018

Community Treasure Hunt

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

Start Hunting!