MATLAB: Measuring Diameter of Calibration Pin

image processingImage Processing Toolboxmachine vision

I want to measure the diameter of the pin but getting a bad edge detection image. Can someone help me to further improve it?
untitled.png
My edge detection image:
Edge.png
My coding:
%% Read and Binarize
load image.mat
I4 = imgaussfilt(I3);
I4 = I4 <120;
%%
I5 = edge(I4,'Sobel');
imshow(I5)

Best Answer

Why are you doing edge detection???? I see no need for that. If the edges are about where you put them, then try thresholding and getting the area and divide by the width:
mask = grayImage < someValue;
mask = imfill(mask, 'holes');
diameter = nnz(mask) / size(mask, 2); % height = area / width. Answer is in pixels.
It's trickier if you want the inner, instead of outer, dark thing.