How to detect certain pixel value in a stick.

1 回表示 (過去 30 日間)
Chathurika Sandamali
Chathurika Sandamali 2021 年 2 月 19 日
I want to measure the diameter of cinnamon sticks. I already did edge detection. I want to get one pixel value from upper line and second pixel value from lower line and then substract and get the diameter.
I am troubling when get upper and lower lines pixel values.It should be auto detecting when upload an image.
Thank you.

採用された回答

Rik
Rik 2021 年 2 月 19 日
Something like this will do it:
A=imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/524729/image.png');
%convert image back to logical mask
L=im2double(im2gray(A))>0.5;
width=zeros(size(L,2),1);
for n=1:size(L,2)
tmp=L(:,n);
tmp=diff([find(tmp,1,'first') find(tmp,1,'last')]);
tmp=sort([0 tmp]);tmp=tmp(end);%catch empty case
width(n)=tmp;
end
width=median(width);
disp(width)
15
  3 件のコメント
Rik
Rik 2021 年 2 月 22 日
If it works, please consider marking my answer as accepted answer.
The easiest way to repeat a process in Matlab is to use a loop. You can easily put my code in a function:
function width=WidthOfContour(L)
width=zeros(size(L,2),1);
for n=1:size(L,2)
tmp=L(:,n);
tmp=diff([find(tmp,1,'first') find(tmp,1,'last')]);
tmp=sort([0 tmp]);tmp=tmp(end);%catch empty case
width(n)=tmp;
end
width=median(width);
end
Chathurika Sandamali
Chathurika Sandamali 2021 年 2 月 22 日
Ok Thank you @Rik.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by