hasIPT = license('test', 'image_toolbox');
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
baseFileName = 'DE10-1200-500-30_00071.png';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileName = baseFileName;
if ~exist(fullFileName, 'file')
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
grayImage = imread(fullFileName);
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
grayImage = grayImage(:,:,2);
imshow(imadjust(grayImage));
title('Original Image', 'FontSize', fontSize);
hFig.WindowState = 'maximized'
hFig.Name = 'Demo by Image Analyst'
grayImage = CircleMaskImage(grayImage);
[pixelCount, grayLevels] = imhist(grayImage);
bar(grayLevels, pixelCount);
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
binaryImage = (grayImage >= lowThreshold) & (grayImage <= highThreshold);
title('Initial Binary Image', 'FontSize', fontSize);
se = strel('disk', 3, 0);
binaryImage = imclose(binaryImage, se);
binaryImage = imfill(binaryImage, 'holes');
binaryImage = bwareafilt(binaryImage, numberToExtract);
title('Biggest Blob, Final Mask', 'FontSize', fontSize);
rightEdge = nan(1, rows);
thisRow = binaryImage(row, :);
leftIndex = find(thisRow, 1, 'first');
leftEdge(row) = leftIndex;
rightEdge(row) = find(thisRow, 1, 'last');
widths(row) = rightEdge(row) - leftEdge(row);
topRow = find(~isnan(widths), 1, 'first')
[maxWidth, bottomRow] = max(widths)
plot(1:rows, widths, 'b-', 'LineWidth', 2);
title('Widths as a function of line number', 'FontSize', fontSize);
yline(topRow, 'Color', 'r', 'LineWidth', 2)
yline(bottomRow, 'Color', 'r', 'LineWidth', 2)
xline(topRow, 'Color', 'r', 'LineWidth', 2)
xline(bottomRow, 'Color', 'r', 'LineWidth', 2)
leftCoefficients = polyfit(x, y, 1);
leftAngle = atand(leftCoefficients(1))
plot(x, y, 'b-', 'LineWidth', 2);
rightCoefficients = polyfit(x, y, 1);
rightAngle = atand(rightCoefficients(1))
plot(x, y, 'r-', 'LineWidth', 2);
coneAngle = abs(leftAngle) + abs(rightAngle);
yLeftFit = polyval(leftCoefficients, x);
plot(x, yLeftFit, 'b-', 'LineWidth', 2);
yRightFit = polyval(rightCoefficients, x);
plot(x, yRightFit, 'r-', 'LineWidth', 2);
legend('left', 'right', 'Location', 'east');
caption = sprintf('Cone Angle = %.1f degrees', coneAngle);
title(caption, 'FontSize', fontSize);
message = sprintf('Done with demo.\nThe cone angle is %.1f degrees.', coneAngle);
uiwait(helpdlg(message));
function grayImage = CircleMaskImage(grayImage)
[imageSizeY, imageSizeX, numberOfColorChannels] = size(grayImage);
[columnsInImage, rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
centerX = imageSizeX / 2;
centerY = imageSizeY / 2;
viscircles([centerX, centerY], radius, 'Color', 'r');
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
grayImage(~circlePixels) = 65535;