clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 18;
fprintf('Beginning to run %s.m ...\n', mfilename);
rgbImage = imread('lena.tif');
imwrite(rgbImage, 'LenaColor.png');
[rows, columns, numberOfColorChannels] = size(rgbImage)
subplot(2, 3, 1);
imshow(rgbImage);
title('Original Image', 'FontSize', fontSize);
FaceDetector = vision.CascadeObjectDetector();
BBOX = step(FaceDetector, rgbImage)
B = insertObjectAnnotation(rgbImage, 'rectangle', BBOX, 'Face');
subplot(2, 3, 2);
imshow(B);
title('Detected Face', 'FontSize', fontSize);
hold on;
rectangle('Position', BBOX, 'EdgeColor', 'b', 'LineWidth', 2);
n = size (BBOX,1);
fprintf('Number of detected faces = %d.\n', n);
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
blockSize = [16, 16];
outputMagnificationRatio1 = 1;
meanFilterFunction1 = @(theBlockStructure) mean2(theBlockStructure.data(:));
blockyImageR = blockproc(redChannel, blockSize, meanFilterFunction1);
blockyImageG = blockproc(greenChannel, blockSize, meanFilterFunction1);
blockyImageB = blockproc(blueChannel, blockSize, meanFilterFunction1);
rgbBlockyImage = cat(3, blockyImageR, blockyImageG, blockyImageB);
rgbBlockyImage = cast(rgbBlockyImage, 'like', rgbImage);
[blockRows, blockColumns, numberOfColorChannels2] = size(rgbBlockyImage)
subplot(2, 3, 3);
imshow(rgbBlockyImage, []);
axis('on', 'image');
caption = sprintf('Block mean image with block size = %d\nOutput image size = %d rows by %d columns', ...
blockSize(1), blockRows, blockColumns);
title(caption, 'FontSize', fontSize);
rgbPixelatedImage = imresize(rgbBlockyImage, [rows, columns], 'nearest');
subplot(2, 3, 4);
imshow(rgbPixelatedImage);
axis('on', 'image');
title('Resized, Pixelated Image', 'FontSize', fontSize);
hold on;
rectangle('Position', BBOX, 'EdgeColor', 'b', 'LineWidth', 2);
row1 = BBOX(2);
row2 = BBOX(2) + BBOX(4);
col1 = BBOX(1);
col2 = BBOX(1) + BBOX(3);
rgbMaskedImage = rgbImage;
rgbMaskedImage(row1:row2, col1:col2, :) = rgbPixelatedImage(row1:row2, col1:col2, :);
subplot(2, 3, 5);
imshow(rgbMaskedImage, []);
axis('on', 'image');
title('Final Masked, Pixelated Image', 'FontSize', fontSize);
g = gcf;
g.WindowState = 'maximized';
g.Name = 'Masked, Pixelated Image';
windowSize = 41;
kernel = ones(windowSize) / windowSize ^ 2;
rgbBlurredImage = imfilter(rgbImage, kernel);
hFig2 = figure;
subplot(1, 2, 1);
imshow(rgbBlurredImage);
axis('on', 'image');
title('Resized, Blurred Image', 'FontSize', fontSize);
hold on;
rectangle('Position', BBOX, 'EdgeColor', 'b', 'LineWidth', 2, 'LineWidth', 2);
row1 = BBOX(2);
row2 = BBOX(2) + BBOX(4);
col1 = BBOX(1);
col2 = BBOX(1) + BBOX(3);
rgbMaskedImage = rgbImage;
rgbMaskedImage(row1:row2, col1:col2, :) = rgbBlurredImage(row1:row2, col1:col2, :);
subplot(1, 2, 2);
imshow(rgbMaskedImage, []);
axis('on', 'image');
title('Final Masked, Blurred Image', 'FontSize', fontSize);
hFig2.Units = 'normalized';
hFig2.Position = [0.3, 0.3, 0.4, 0.4];
hFig2.Name = 'Masked, Blurred Image'