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.
0 个评论
采纳的回答
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)
3 个评论
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!