% 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.

 채택된 답변

Image Analyst
Image Analyst 2020년 4월 10일

1 개 추천

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

Image Analyst
Image Analyst 2020년 4월 10일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 4월 10일
편집: Ameer Hamza 2020년 4월 10일

2 개 추천

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

Martin Schlachta
Martin Schlachta 2020년 4월 10일
Yeah, thats fine thanks but at the other picture/image not working as it should, it has problem with focus as you could see. Is any fix for that?
Ameer Hamza
Ameer Hamza 2020년 4월 10일
I think this is the issue with the face detection algorithm, not the pixelating. The face detection did not correctly identify the bounding box. Can you see if the bounding box covers the whole face? If bounding box is correct, can you share the original image so that i can test?
I found a mistake in my code, which will misplace the pixelation region. Please try the updated code. Specifically, I changed these two lines
face = A(y:y+h, x:x+w, :); %<--- this
sz = size(face, [1 2]);
face = imresize(imresize(face, sz/20), sz, 'Method', 'nearest');
A(y:y+h, x:x+w, :) = face; %<--- this
Martin Schlachta
Martin Schlachta 2020년 4월 10일
Thanks!
erik macejko
erik macejko 2021년 5월 8일
Can I use this code for more than one face? If no whats the solution for recognise and censure more faces in one picture?
Image Analyst
Image Analyst 2021년 5월 8일
Yes, go ahead and try it.
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
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기

질문:

2020년 4월 10일

댓글:

2021년 5월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by