close all;
clear;
workspace;
format long g;
format compact;
fontSize = 15;
baseFileName = '8.jpg';
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(2, 2, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
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);
blueMask = redChannel < 100 & greenChannel < 100 & blueChannel > 100;
subplot(2, 2, 2);
imshow(blueMask);
grid on;
axis on;
title('Initial Blue Mask Image', 'FontSize', fontSize);
blueMask = imfill(blueMask, 'holes');
blueMask = bwulterode(blueMask);
subplot(2, 2, 3);
imshow(blueMask);
grid on;
axis on;
title('Final Blue Mask Image', 'FontSize', fontSize);
windowColumns = 16;
windowRows = 8;
countImage = conv2(double(blueMask), ones(windowRows, windowColumns));
subplot(2, 2, 4);
imshow(countImage);
axis on;
caption = sprintf('Count Image: count in a %d by %d moving box', windowRows, windowColumns);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
Best Answer