Pixeling only detected face

5 vues (au cours des 30 derniers jours)
Martin Schlachta
Martin Schlachta le 10 Avr 2020
Commenté : Image Analyst le 13 Mai 2021
% Read image
A = imread('lena512c.bmp');
%Get FaceDetector object
FaceDetector = vision.CascadeObjectDetector();
%Use FaceDetector
BBOX = step(FaceDetector, A);
%Annotation of faces
B = insertObjectAnnotation(A,'rectangle', BBOX, 'Face');
imshow(B),title('Detected Faces');
%Display number of faces
n = size (BBOX,1);
str_n = num2str(n);
str = strcat ('Number of detected faces are = ',str_n);
disp(str);
How can i pixelate just face which was detected,not whole image please?
Detector Works fine.

Réponse acceptée

Image Analyst
Image Analyst le 10 Avr 2020
See my demo below. It creates masked images, both by pixelation and blurring.
Color demo image and m-file script are attached.
It's well commented so I think it's self explanatory.
% Locate the face in a color image using the vision.CascadeObjectDetector of the Computer Vision Toolbox
% and mask it in 2 ways: by blurring and pixelation.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
fprintf('Beginning to run %s.m ...\n', mfilename);
% Read image from the drive.
rgbImage = imread('lena.tif');
imwrite(rgbImage, 'LenaColor.png');
[rows, columns, numberOfColorChannels] = size(rgbImage)
subplot(2, 3, 1);
imshow(rgbImage);
title('Original Image', 'FontSize', fontSize);
% Get FaceDetector object. Requires the Computer Vision Toolbox.
FaceDetector = vision.CascadeObjectDetector();
% Use FaceDetector
BBOX = step(FaceDetector, rgbImage)
% Annotation of faces by putting boxes over them.
B = insertObjectAnnotation(rgbImage, 'rectangle', BBOX, 'Face');
subplot(2, 3, 2);
imshow(B);
title('Detected Face', 'FontSize', fontSize);
% Display box over it.
hold on;
rectangle('Position', BBOX, 'EdgeColor', 'b', 'LineWidth', 2);
% Display the number of detected faces.
n = size (BBOX,1);
fprintf('Number of detected faces = %d.\n', n);
%------------------------------------------------------------------------------------------------------------
% OPTION 1: Create an image where the image is pixelated within the face box area:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
blockSize = [16, 16]; % 16 pixel by 16 pixel window that jumps along in steps of 16.
% Block process the image to replace every pixel in the
% 16 pixel by 16 pixel block by the mean of the pixels in the block.
% The image is 512 pixels across which will give 512/16 = 32 blocks.
% The image is 480 pixels tall which will give 480/16 = 30 blocks.
outputMagnificationRatio1 = 1;
meanFilterFunction1 = @(theBlockStructure) mean2(theBlockStructure.data(:));
% Next process the image and each 64x64 block is an array of 64 x 64 pixels.
% So it will be the same size as the original image.
% Now,here we actually to the actual filtering.
blockyImageR = blockproc(redChannel, blockSize, meanFilterFunction1);
blockyImageG = blockproc(greenChannel, blockSize, meanFilterFunction1);
blockyImageB = blockproc(blueChannel, blockSize, meanFilterFunction1);
% Recombine separate color channels into a single, true color RGB image.
rgbBlockyImage = cat(3, blockyImageR, blockyImageG, blockyImageB);
% rgbBlockyImage is a double image (because we took the mean) and would display as white unless we cast to uint8, so let's do that.
rgbBlockyImage = cast(rgbBlockyImage, 'like', rgbImage);
[blockRows, blockColumns, numberOfColorChannels2] = size(rgbBlockyImage)
% Display the block mean image.
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);
% Create a pixelated image
% Resize the image to be the same size as the original.
rgbPixelatedImage = imresize(rgbBlockyImage, [rows, columns], 'nearest'); % Use 'nearest' to pixelate.
% Display the blurry image.
subplot(2, 3, 4);
imshow(rgbPixelatedImage);
axis('on', 'image');
title('Resized, Pixelated Image', 'FontSize', fontSize);
% Display box over it.
hold on;
rectangle('Position', BBOX, 'EdgeColor', 'b', 'LineWidth', 2);
% Replace the pixels in the original image with the pixelated image.
row1 = BBOX(2);
row2 = BBOX(2) + BBOX(4);
col1 = BBOX(1);
col2 = BBOX(1) + BBOX(3);
rgbMaskedImage = rgbImage; % Initialize with a copy of the original image.
% Replace only within the box.
rgbMaskedImage(row1:row2, col1:col2, :) = rgbPixelatedImage(row1:row2, col1:col2, :);
% Display the masked, pixelated image.
subplot(2, 3, 5);
imshow(rgbMaskedImage, []);
axis('on', 'image');
title('Final Masked, Pixelated Image', 'FontSize', fontSize);
% Maximize the figure window.
g = gcf;
g.WindowState = 'maximized';
g.Name = 'Masked, Pixelated Image';
%------------------------------------------------------------------------------------------------------------
% OPTION 2: Create an image where the image is blurred within the face box area:
% Create a blurry image
% Resize the image to be the same size as the original.
windowSize = 41;
kernel = ones(windowSize) / windowSize ^ 2;
rgbBlurredImage = imfilter(rgbImage, kernel); % Requires the Image Processing Toolbox.
% Display the blurry image.
hFig2 = figure;
subplot(1, 2, 1);
imshow(rgbBlurredImage);
axis('on', 'image');
title('Resized, Blurred Image', 'FontSize', fontSize);
% Display box over it.
hold on;
rectangle('Position', BBOX, 'EdgeColor', 'b', 'LineWidth', 2, 'LineWidth', 2);
% Replace the pixels in the original image with the blurred image.
row1 = BBOX(2);
row2 = BBOX(2) + BBOX(4);
col1 = BBOX(1);
col2 = BBOX(1) + BBOX(3);
rgbMaskedImage = rgbImage; % Initialize with a copy of the original image.
% Replace only within the box.
rgbMaskedImage(row1:row2, col1:col2, :) = rgbBlurredImage(row1:row2, col1:col2, :);
% Display the masked, pixelated image.
subplot(1, 2, 2);
imshow(rgbMaskedImage, []);
axis('on', 'image');
title('Final Masked, Blurred Image', 'FontSize', fontSize);
% Enlarge the figure window.
hFig2.Units = 'normalized';
hFig2.Position = [0.3, 0.3, 0.4, 0.4];
hFig2.Name = 'Masked, Blurred Image'
  1 commentaire
Image Analyst
Image Analyst le 10 Avr 2020
See the lines of code that call imshow(). Well imshow() is a function that displays an image. If you don't want to display an image, don't call imshow(). Just comment out each section of the code that calls subplot(), imshow(), title(), etc. that you don't want.

Connectez-vous pour commenter.

Plus de réponses (1)

Ameer Hamza
Ameer Hamza le 10 Avr 2020
Modifié(e) : Ameer Hamza le 10 Avr 2020
try this
% Read image
A = imread('lena_std.tif');
%Get FaceDetector object
FaceDetector = vision.CascadeObjectDetector();
%Use FaceDetector
BBOX = step(FaceDetector, A);
%Annotation of faces
B = insertObjectAnnotation(A,'rectangle', BBOX, 'Face');
imshow(B),title('Detected Faces');
%Display number of faces
n = size (BBOX,1);
str_n = num2str(n);
str = strcat ('Number of detected faces are = ',str_n);
disp(str);
x = BBOX(1);
y = BBOX(2);
w = BBOX(3);
h = BBOX(4);
face = A(y:y+h, x:x+w, :);
sz = size(face, [1 2]);
face = imresize(imresize(face, sz/20), sz, 'Method', 'nearest');
A(y:y+h, x:x+w, :) = face;
imshow(A)
  8 commentaires
erik macejko
erik macejko le 12 Mai 2021
Thanks, and Can I ask one more thing? What is this part of code doing?
face = A(y:y+h, x:x+w, :); %% this
sz = size(face, [1 2]); %% this
face = imresize(imresize(face, sz/20), sz, 'Method', 'nearest');
A(y:y+h, x:x+w, :) = face; %% and this
Thanks for your answers
Image Analyst
Image Analyst le 13 Mai 2021
face = A(y:y+h, x:x+w, :); %% this crops a certain lateral ROI out of a color image.
sz = size(face, [1 2]); %% this returns the number of rows in the cropped face array as sz(1) and the number of columns as sz(2)
% Shrinks by factor of 20, then expand back to original size with replication
% to make it have a blocky appearance.
face = imresize(imresize(face, sz/20), sz, 'Method', 'nearest');
A(y:y+h, x:x+w, :) = face; %% and this puts the block image back into the original image at the original location.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Computer Vision with Simulink dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by