How To Detect Face in Image Processing Using MATLAB?
Last Updated :
05 Oct, 2022
MATLAB is a programming platform that is mainly used by engineers and scientists to analyze and design systems. Image processing is a process to perform some operations on an image to get an enhanced image or to extract some useful information from it. Each picture is stored as an array and each pixel is an element of the array.
Face Detection:
Face detection also called facial detection is an artificial intelligence (AI) based computer technology used to find and identify human faces in digital images. Face Detection is widely used in projects and software. it can be called a subset of image processing. MATLAB provides the interface to practice and learn image processing skills in an effective manner.
Example 1:
Matlab
% MATLAB program for GeeksforGeeks
% logo detection
X=imread("monochrome1.png");
Y=imread("GeeksforGeeks.svg.png");
subplot(2,1,1)
imshow(X);
subplot(2,1,2)
imshow(Y);
Output:
Explanation:
- HERE "imread" is the command to MATLAB to take input of image address on which we are working. "imread" takes image address which can be taken from the image which is present in MATLAB drive.
- "subplot" is the command we used to add multiple images in a single frame. subplot(2,1,1) works as (row=2, column=1, position=1) .
- This will work as a matrix formation of 2x1 matrix 2 rows, 1 column, and the third number (2,1,1) shows the position of the image and as result, the image is considered to be an element of the matrix.
- The "imshow" command is used to get the desired result. the interface will give you desired output on compiling it. "imshow" is the fundamental image display function in MATLAB, optimizing figure, axes, and image object property settings for image display.
image processing is not limited to a certain scope, it has many uses and features such as changing image color, extracting color, making filters, etc. Any image comprises RGB values which can be extracted from the image to make it different in appearance.
filters are like matrix as same as the size of our image suppose " image1 is matrix A" and "filter is matrix B" these both matrix gets multiplied and gives a new filtered image [A x B = C].
Example 2:
Matlab
% MATLAB code for changing image color
% gray %changing image color to gray
X=imread("monochrome1.png");
Y=imread("GeeksforGeeks.svg.png");
subplot(2,2,1)
imshow(X);
subplot(2,2,2)
z=im2gray(Y);
imshow(z);
subplot(2,2,3)
imshow(Y);
subplot(2,2,4)
G=Y(:,:,2);
imshow(G);
Output:
Explanation:
- Adding images, subplot them in the same frame adding image address will remain same in all cases.
- Here new terms are "im2gray" (image to gray ) it changes the color image in gray color. im2gray(RGB) converts the true color image RGB to the grayscale intensity image I. Grayscale inputs to im2gray are returned unchanged. For any device binary scale image is easy and more compatible to use in their functioning, This is one of the advantages.
- "G=Y(:,:,2)" stands for green color and the code shows how we can extract the green color components from an image.
Now, Facial detection has now become an important feature in today’s technological development there are many advantages of face detection (image processing).
Example 3:
Matlab
% MATLAB code for face detection
Detector=vision.CascadeObjectDetector('EyePairBig');
Detector.MinSize=[11 45];
Detector.MergeThreshold=16;
DDetector=vision.CascadeObjectDetector('Mouth');
DDetector.MinSize=[15 25];
DDetector.MergeThreshold=16;
EDetector=vision.CascadeObjectDetector('Nose');
EDetector.MinSize=[15 18];
EDetector.MergeThreshold=16;
CDetector=vision.CascadeObjectDetector('EyePairSmall');
CDetector.MinSize=[5 22];
CDetector.MergeThreshold=16;
% Read an images
I=imread("face.jpg");
J=imread("face.jpg");
K=imread("face.jpg");
L=imread("face.jpg");
bbox=step(Detector,I);
box=step(DDetector,J);
cbox=step(EDetector,K);
dbox=step(CDetector,L);
% Functions for face fecefeatures detection
FaceFeature=insertObjectAnnotation(I,'rectangle',bbox,'Detected');
FaceFeature2=insertObjectAnnotation(J,'rectangle',box,'Detected');
FaceFeature3=insertObjectAnnotation(K,'rectangle',cbox,'Detected');
FaceFeature4=insertObjectAnnotation(L,'rectangle',dbox,'Detected');
figure;
% Plot
subplot(2,2,1);
% For showing facefeatures
imshow(FaceFeature);
title('EYES detected');
subplot(2,2,2);
imshow(FaceFeature2);
title('MOUTH detected');
subplot(2,2,3);
imshow(FaceFeature3);
title('NOSE detected');
subplot(2,2,4);
imshow(FaceFeature4);
title('EYE SMALL detected');
Output:
Explanation:
- It follows viola jones's algorithm and vision.CascadeObjectDetector is act as a function or command. all 4 features can be easily detected from this code like mouth, nose, etc. as four features will display at the same time eventually code length increases but the concept for all 4 phases is the same. This detector is capable of detecting a variety of objects, including faces and a person's upper body.
- MinSize Size of the smallest object to detect Specify the size of the smallest object to detect, in pixels as a two-element vector, [height width]. Use this property to reduce computation time when the minimum object size is known. Here Minsize [11 45] height =11 and width = 45 prior to processing the image. Detector.MinSize=[X Y]; denotes coordinates as we get desired output from these.
- insertObjectAnnotation is again used as a command that we want the image in the desired box. A subplot has the same role to add multiple images in same frame. Insert annotation in image or video stream. This function inserts labels and corresponding circles or rectangles into an image or video. insertObjectAnnotation(I,'rectangle',box,'Detected'); shows image 'I' we want it in rectangular box as it detects.
- MergeThreshold Threshold for merging detections Specify a threshold value as a scalar integer. This property defines the minimum number of colocated detections needed to declare a final detection. MergeThreshold 16 scalar int here is "16" .
- Box all other just used as variable store the command to detect image. In last phase of code we just written the basic steps of subplot and imshow to get the desired result. Subplot will work same to gather images position wise. title we add in last phase determines title one wants to add with the output image or result.
Similar Reads
Introduction to Object Detection Using Image Processing Object detection is a crucial task in computer vision that involves identifying and locating objects within an image or video. This task is fundamental for various applications, including autonomous driving, video surveillance, and medical imaging. This article delves into the techniques and methodo
7 min read
Digital Image Processing Algorithms using MATLAB Like it is said, "One picture is worth more than ten thousand words "A digital image is composed of thousands and thousands of pixels. An image could also be defined as a two-dimensional function, f(x, y), where x and y are spatial (plane) coordinates and therefore the amplitude of f at any pair of
8 min read
How to Detect Shapes in Images in Python using OpenCV? OpenCV is an open source library used mainly for processing images and videos to identify shapes, objects, text etc. It is mostly used with python. In this article we are going to see how to detect shapes in image. For this we need cv2.findContours() function of OpenCV, and also we are going to use
3 min read
MATLAB - Image Edge Detection using Sobel Operator from Scratch Sobel Operator: It is a discrete differentiation gradient-based operator. It computes the gradient approximation of image intensity function for image edge detection. At the pixels of an image, the Sobel operator produces either the normal to a vector or the corresponding gradient vector. It uses tw
3 min read
How to Blur Faces in Images using OpenCV in Python? Prerequisite: OpenCV OpenCV is a huge open-source library for computer vision, machine learning, and image processing. Â It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a highly optim
2 min read