MATLAB: I tried to find inner and outer diameter of an circle using this code but it didn’t give me outputs images like below I’ve attached

image processing

Here is the code % Read your image and binarize it I = imread('C:\Users\Rangika Mark\Desktop\WP_20171207_11_47_46_Selfie.jpg'); Igray = rgb2gray(I); BW = imbinarize(Igray); % Measure the outer radius BWout = ~BW; BWout = imfill(BWout,'holes'); statOuter = regionprops(BWout,{'EquivDiameter','Centroid'}); outerRadius = statOuter.EquivDiameter/2; % Measure the inner radius BWin = imclearborder(BW); BWin = imopen(BWin, strel('disk',5)); % Remove noise statInner = regionprops(BWin,{'EquivDiameter','Centroid'}) innerRadius = statInner.EquivDiameter/2; % Show the result figure imshow(I) hold on viscircles(statOuter.Centroid, outerRadius,'Color','r') viscircles(statInner.Centroid, innerRadius,'Color','r')

Best Answer

  • In this case, you should pick up the region, that has maximum diameter, from the output of regionprops function. Based on your original code, I have slightly modified to do that.
    I = imread('8122bCXHqPL._SY463_.jpg');
    Igray = rgb2gray(I);
    BW = imbinarize(Igray);
    % Measure the outer radius
    BWout = ~BW;
    BWout = imfill(BWout,'holes');
    statOuter = struct2table(regionprops(BWout,{'MajorAxisLength','Centroid'}));
    [~,idxOuter] = max(statOuter.MajorAxisLength);
    outerRadius = statOuter.MajorAxisLength(idxOuter)/2;
    % Measure the inner radius
    BWin = imclearborder(BW);
    BWin = imopen(BWin, strel('disk',5));
    statInner = struct2table(regionprops(BWin,{'MajorAxisLength','Centroid'}));
    [~,idxInner] = max(statInner.MajorAxisLength);
    innerRadius = statInner.MajorAxisLength(idxInner)/2;
    % Show the result
    figure
    imshow(I)
    hold on
    viscircles(statOuter.Centroid(idxOuter,:), outerRadius,'Color','r')
    viscircles(statInner.Centroid(idxInner,:), innerRadius,'Color','r')