clc;
close all;
imtool close all;
clear;
workspace;
format long g;
format compact;
fontSize = 13;
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
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 = 'D:\Temporary stuff';
baseFileName = 'moogseed5.jpg';
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));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorBands] = size(rgbImage);
subplot(2, 3, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
[rows, columns, numberOfColorBands] = size(rgbImage);
if numberOfColorBands > 1
grayImage = rgbImage(:, :, 2);
end
subplot(2, 3, 2);
imshow(grayImage, []);
title('Green Channel Image', 'FontSize', fontSize);
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 3, 3);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of Green Channel', 'FontSize', fontSize);
xlim([0 grayLevels(end)]);
binaryImage = grayImage > 55;
subplot(2, 3, 4);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
binaryImage = imclearborder(binaryImage);
binaryImage = imfill(binaryImage, 'holes');
binaryImage = bwareaopen(binaryImage, 500);
subplot(2, 3, 5);
imshow(binaryImage, []);
title('Cleaned Binary Image', 'FontSize', fontSize);
maskedRgbImage = bsxfun(@times, rgbImage, cast(binaryImage, class(rgbImage)));
subplot(2, 3, 6);
imshow(maskedRgbImage, []);
title('Masked RGB Image', 'FontSize', fontSize);
Best Answer