clc;
close all;
clearvars;
workspace;
format long g;
format compact;
fontSize = 16;
fullFileName = fullfile(pwd, 'annular_orig.jpg');
[folder, baseFileName, ext] = fileparts(fullFileName);
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage);
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
[wheelMask,maskedRGBImage] = createMaskForWholeWheel(rgbImage);
[blueMask,maskedRGBImage] = createMaskForBlueCenter(rgbImage);
wheelMask = imfill(wheelMask, 'holes');
wheelMask = bwareafilt(wheelMask, 1);
blueMask = imfill(blueMask, 'holes');
blueMask = bwareafilt(blueMask, 1);
subplot(2, 2, 2);
imshow(wheelMask);
axis on;
title('Total Wheel Mask', 'FontSize', fontSize, 'Interpreter', 'None');
subplot(2, 2, 3);
imshow(blueMask);
axis on;
title('Blue Center Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
ringMask = wheelMask & ~blueMask;
subplot(2, 2, 4);
imshow(ringMask);
axis on;
title('Ring Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
boundaries = bwboundaries(ringMask);
figure;
imshow(rgbImage, []);
axis on;
caption = sprintf('Color Image with Boundary');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
hold on;
numberOfBoundaries = size(boundaries, 1);
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'r', 'LineWidth', 3);
end
hold off;
propsWheel = regionprops(wheelMask, 'EquivDiameter');
wheelDiameter = propsWheel.EquivDiameter
propsBlue = regionprops(blueMask, 'EquivDiameter');
blueDiameter = propsBlue.EquivDiameter
ringArea = sum(ringMask(:));
message = sprintf('Area of ring = %d pixels.\nAverage Diameter of Entire Wheel = %.1f pixels.\nAverage Diameter of Blue Part = %.1f pixels.\nAverage Thickness of Edge = %.1f pixels.', ...
ringArea, wheelDiameter, blueDiameter, wheelDiameter-blueDiameter);
text(20, 150, message, 'FontSize', fontSize, 'Color', 'y', 'FontWeight', 'bold', 'VerticalAlignment', 'top');
function [BW,maskedRGBImage] = createMaskForWholeWheel(RGB)
I = rgb2hsv(RGB);
channel1Min = 0.363;
channel1Max = 0.855;
channel2Min = 0.000;
channel2Max = 1.000;
channel3Min = 0.000;
channel3Max = 1.000;
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
maskedRGBImage = RGB;
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
function [BW,maskedRGBImage] = createMaskForBlueCenter(RGB)
I = rgb2hsv(RGB);
channel1Min = 0.569;
channel1Max = 0.671;
channel2Min = 0.229;
channel2Max = 1.000;
channel3Min = 0.251;
channel3Max = 1.000;
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
maskedRGBImage = RGB;
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
Best Answer