clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 20;
hasLicenseForToolbox = license('test', 'image_toolbox');
if ~hasLicenseForToolbox
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');
if strcmpi(reply, 'No')
return;
end
end
folder = pwd;
baseFileName = '001bin.jpg';
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileNameOnSearchPath = baseFileName;
if ~exist(fullFileNameOnSearchPath, 'file')
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
grayImage = rgb2gray(grayImage);
end
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
binaryImage = grayImage < 128;
binaryImage = imfill(binaryImage, 'holes');
leafArea = sum(binaryImage(:))
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image, leaf mask', 'FontSize', fontSize, 'Interpreter', 'None');
defectImage = grayImage > 128;
defectImage = imclearborder(defectImage);
subplot(2, 2, 3);
imshow(defectImage, []);
title('Defects', 'FontSize', fontSize, 'Interpreter', 'None');
defectArea = sum(defectImage(:))
message = sprintf('The leaf area = %d pixels.\nThe defect Area = %d pixels = %.1f%%',...
leafArea, defectArea, defectArea/leafArea*100);
uiwait(msgbox(message));