How to detect certain pixel value in a stick.

1 次查看(过去 30 天)
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by