clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 15;
baseFileName = '1.png';
folder = pwd
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
rgbImage = imread(fullFileName);
[imageRows, imageColumns, numberOfColorChannels] = size(rgbImage);
subplot(1, 3, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, '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')
drawnow;
hp = impixelinfo();
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
yellowMask = redChannel > 150 & greenChannel > 150 & blueChannel < 150;
yellowMask = imfill(yellowMask, 'holes');
subplot(1, 3, 2);
imshow(yellowMask);
grid on;
axis on;
title('Yellow Mask Image', 'FontSize', fontSize);
props = regionprops(yellowMask, 'BoundingBox');
croppedImage = imcrop(rgbImage, props.BoundingBox);
subplot(1, 3, 3);
imshow(croppedImage);
axis on;
caption = sprintf('Final, Cropped Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
Best Answer