How to detect certain pixel value in a stick.

1 Ansicht (letzte 30 Tage)
Chathurika Sandamali
Chathurika Sandamali am 19 Feb. 2021
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.

Akzeptierte Antwort

Rik
Rik am 19 Feb. 2021
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 Kommentare
Rik
Rik am 22 Feb. 2021
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 am 22 Feb. 2021
Ok Thank you @Rik.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Images finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by