Calculation of level pixel heights
1 回表示 (過去 30 日間)
古いコメントを表示
clc;clear all;
%% 1. Read picture
I = imread('orange.jpg');
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 件のコメント
Walter Roberson
2022 年 8 月 1 日
I believe that they are indicating that they are having trouble detecting level of transparent fluids but are ok with orange fluid
回答 (1 件)
Varun
2023 年 11 月 28 日
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!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Image Segmentation and Analysis についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!