Calculation of level pixel heights

3 Ansichten (letzte 30 Tage)
Runyu Liu
Runyu Liu am 1 Aug. 2022
Beantwortet: Varun am 28 Nov. 2023
clc;clear all;
%% 1. Read picture
I = imread('orange.jpg');
Error using imread>get_full_filename
File "orange.jpg" does not exist.

Error in imread (line 371)
fullname = get_full_filename(filename);
figure();
imshow(I);
%% 2. Image binarization
thresh = graythresh(I); %threshold
thresh = thresh+0.035;
I2 = im2bw(I,thresh);
I2 = imcomplement(I2);
figure();
imshow(I2);
%% 3. Finding the Maximum Connected Domain
img_reg = regionprops(I2, 'area', 'boundingbox');
areas = [img_reg.Area];
rects = cat(1,img_reg.BoundingBox);
[~, max_id] = max(areas);
max_rect = rects(max_id, :);
% show the largest connected region
figure(),
imshow(I2);
rectangle('position', max_rect, 'EdgeColor', 'r');
%
D = max_rect(3);
H = max_rect(4);
%% boundary
TH_D1 = D*0.9;
for i = floor(max_rect(2)):(floor(max_rect(2))+ H-1)
num = 0;
for j = floor(max_rect(1)):(floor(max_rect(1))+ D-1)
if I2(i,j) == 1
num = num + 1;
end
end
if num > TH_D1
line_1 = i;
break
end
end
TH_D2 = D*0.9;
for i = (floor(max_rect(2))+ H-1):-1:floor(max_rect(2))
num = 0;
for j = floor(max_rect(1)):(floor(max_rect(1))+ D-1)
if I2(i,j) == 1
num = num + 1;
end
end
if num > TH_D2
line_2 = i;
break
end
end
I'm trying to measure the pixel height of transparent liquids and I've designed a measurement method based on the maximum connected area which is only capable of measuring the level of coloured liquids.
  5 Kommentare
Walter Roberson
Walter Roberson am 1 Aug. 2022
I believe that they are indicating that they are having trouble detecting level of transparent fluids but are ok with orange fluid
Runyu Liu
Runyu Liu am 1 Aug. 2022
My tutor suggested that I use edge detection to measure liquid levels, but I could not think of a suitable method based on edge detection. This method works well for measuring coloured liquids, but not so well for identifying clear levels in labelled test tubes.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Varun
Varun am 28 Nov. 2023
Hi Runyu,
I understand that you are facing issues with your code that calculates the height of the coloured as well as transparent liquid. You have already provided a good approach to calculate this using MATLAB functions like “imread” to read the image, “graythresh” for image binarization, followed by finding the maximum connected area using “regionprop”.
Here are the precautions which you can take to achieve your goal:
  • Resolve the following error:
File “orange.jpg” does not exist.
You have the file with name “orange.png” i.e., “.png” extension but you are giving input as “orange.jpg”. Hence it is not able to find this file and throws this error.
  • I executed your code on my machine for these both coloured and transparent image, your idea to find the height using maximum connected area is working. It is able to detect both coloured as well as transparent liquid. However, in case of transparent liquid it will give wrong answer because there is a paper label on the test-tube which is making the “regionprop” function to calculate the unintended area due this paper label as well.To resolve this issue, make sure that you take a clear pic/image of the test-tube at such an angle that there is no obscuration of this paper label.
Please refer to the following documentations to learn more:
Hope this helps!

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by