You are on page 1of 57

Pattern Recognition:

Introduction:

PATTERN RECOGNITION
Pattern Recognition has attracted the attention of researchers in last few decades as a machine
learning approach due to its wide spread application areas. The application area includes
medicine, communications, automations, military intelligence, data mining, bioinformatics,
document classification, speech recognition, business and many others.
Automatic (machine) recognition, description, classification, and grouping of patterns are
important problems in a variety of engineering and scientific disciplines such as biology,
psychology, medicine, marketing, computer vision, artificial intelligence, and remote sensing.
But what is a pattern?
PATTERN : Pattern is a set of objects or phenomena or concepts where the elements of the set
are similar to one another in certain ways/aspects. The Pattern are described by certain quantities,
qualities, behaviors, notable features and so on.
Example : Humans, insects, Animals, clouds etc.
Cloud patterns

Pattern + Recognition
Humans have a pattern which is different from the pattern of animals. Each individuals has a
pattern which is different from the patterns of others.
There are numerous number applications of pattern recognition techniques which are shown in
below table.

The design of a pattern recognition system essentially involves the following three aspects:
1) data acquisition and preprocessing,
2) data representation, and
3) decision making.
The four best known approaches for pattern recognition are: 1) template matching, 2) statistical
classification, 3) syntactic or structural matching, and 4) neural networks.

Basics of Pattern Recognition


Pattern Representation:
A pattern is represented by a set of d features, or attributes, viewed as a d-dimensional feature
vector.

Possible features for Pattern Recognition:


Human faces can be recognized by their facial
features i.e. eyebrows,eye, lip,nose etc.
But what computer recognize is the below image
pixel values.

So here the task is to recognize the face using different pattern recognition techniques
which will be described in the sections to follow.
What is Pattern Recognition?
Pattern Recognition is the study of how machines can observe the environment,
learn to distinguish patterns of interest from their background, and
make sound and reasonable decisions about the categories of the patterns.
Classification in PR
A class is a set of objects having some important properties in common

A feature extractor is a program that inputs the data (image) and extracts features that can be
used in classification.
A classifier is a program that inputs the feature vector and assigns it to one of a set of
designated classes or to the reject class.

ClassificationA Two-Step Process

1)
Model construction: describing a set of predetermined classes
Each tuple/sample is assumed to belong to a predefined class, as determined by the class label
attribute
The set of tuples used for model construction: training set
The model is represented as classification rules, decision trees, or mathematical formulae

2)Model usage: for classifying future or unknown objects


Estimate accuracy of the model
The known label of test sample is compared with the classified result from the model
Accuracy rate is the percentage of test set samples that are correctly classified by the model

Example(1): Model Construction

(2): Use the Model in Prediction

Pattern Recognition Technique:

The four best known approaches for pattern recognition are: 1) template matching, 2) statistical
classification, 3) syntactic or structural matching, and 4) neural networks.

A brief comparison of these techniques is given in the below table.

The detailed description of these methods are described in the below sections.
1.Template Matching:
One of the simplest and earliest approaches to pattern recognition is based on template matching.
Matching is a generic operation in pattern recognition which is used to determine the similarity
between two entities (points, curves, or shapes) of the same type.
Template Matching techniques compare portions of images against one another.
Sample image may be used to recognize similar objects in source image.

If standard deviation of the template image compared to the source image is small enough,
template matching may be used.
Templates are most often used to identify printed characters, numbers, and other small, simple
objects.
General Idea of Template Matching
create a small template image of the feature we want to detect
center the template image over pixel( i, j) in a target image
calculate the pixel differences between the template image and the pixels centered at ( i, j )
if the distance is small, the template is similar to the window centered at ( i , j)
if the distance is large, the template is dissimilar
repeat for all pixels
the minimum distance is the best match
this is known as template matching
Face Detection using Template matching:
A window slides across image and evaluate a face model at every location.Then above
procedure is followed to detect face.

Pseudocode for Template Matching


[location,d] = template(image, template)
[nx ny] = size(image)
[tx ty] = size(template)
for i = tx:(nx-tx)
for j = ty:(ny-ty)
d(i,j) = distance(template, window(i,j) % calculate local distances
end
end
location = index_of_minimum(d(i,j)) % locate minimum distance

Normalized Template-Matching
We can normalize each image patch, and the template in various ways before performing the
match
e.g., subtract the mean from both the template and the image patch

effect?
removes any additive lighting effect

Correlation
Given an image, we want to find all places in the image which contain a subimage, also called a
template.
Very useful for answering where is the x in this picture?
Method
The matching process moves the template image to all possible positions in a larger source image
and computes a numerical index that indicates how well the template matches the image in that
position.

Match is done on a pixel-by-pixel basis. The matching process moves the template image to all
possible positions in a larger source image and computes a numerical index that indicates how
well the template matches the image in that position. Match is done on a pixel-by-pixel basis.
Correlation
Just the dot product between the template and a
neighborhood in the image.
Idea: high correlation when the template matches.
Problem: always high correlation when matching with a
plain bright region
Solution: Normalize the template and each region by
subtracting eachs mean from itself before taking dot
product
Correlation is Computation Intensive

Euclidean Distance
Let I be a gray level image
and g be a gray-value template of size nxm.

In this formula (r,c) denotes the top left corner of template g.

The disadvantage of ED is that it is very sensitive even to small deformation


This brings to the end of our discussion on Template matching.
Structural Pattern Recognition
Structural pattern recognition is intuitively appealing because, in addition to classification, this
approach also provides a description of how the given pattern is constructed from the primitives.
The data is converted to a discrete structure (such as a grammar or a graph) and the techniques
are related to computer science subjects (such as parsing and graph matching).

The statistical Vs structural approaches: The goal is to discriminate between the square and the
triangle. A statistical approach extracts quantitative features which are assembled into feature
vectors for classification.
A structural approach extracts morphological features and their interrelationships,
encoding them in relational graphs;

Structural pattern recognition Describe complicated objects in terms of simple primitives and
structural relationships.
It helps in Decision-making when features are non-numeric or structural

It Describe patterns using deterministic grammars or formal languages


Structural approach to stapler Recogntion
Acquire source stapler image

Segment and find the stapler edge

Compute the boundary

Select boundary points


Boundary points at distance of 8:

Image recreated from boundary points:

Compare to a different view

Segment and acquire boundary

Image redrawn from boundary data:

Boundary points selected at a distance of 8:

Redrawn from selected boundary points:

Final step
Compare the chain code strings of the 2 sets of boundary points.

Statistical Pattern Recognition


Statistical pattern recognition has been used successfully to design a number of commercial
recognition systems. In statistical pattern recognition, a pattern is represented by a set of d
features, or attributes, viewed as a d-dimensional feature vector.
The data is reduced to vectors of numbers
and statistical techniques are used for
the tasks to be performed.

Model for statistical pattern recognition.

Features are chosen in such a way that different patterns occupy non-overlapping feature space.
It recognizes the probabilistic nature both of the information we seek to process, and of the form
in which we should express it [2]. It works well when the selected features lead to feature spaces
which cluster in a recognizable manner, i.e. there is proper interclass distance. After analyzing
the probability distribution of a pattern belonging to a class, decision boundary is determined [3],
[4]. Here patterns are projected to pre-processing operations to make them suitable for training
purposes. Features are selected upon analyzing training patterns. System learns and adapts itself
for unknown patterns as shown in Fig. 1.Test patterns are applied to check suitability of system
to recognize patterns. Feature
measurement is done while testing, then these feature values is presented to learned system and
in this way classification is performed. When conditional probability density distribution is

known, parametric classification schemes are used otherwise non parametric classification
scheme need to be used. Various decision rules are there to determine decision boundary like,
Bayes Decision Rule, Optimal Bayes Decision Rule, The Maximum Likelihood Rule, NeymanPearson rule and MAP rule. As feature spaces are partitioned, system becomes noise insensitive,
therefore in case of noisy patterns. The choice of statistical model is a good solution. Depending
upon whether the method opted is supervised or unsupervised statistical technique can be
categorized as: Discriminant Analysis and Principal Component Analysis [1].

Discriminant Analysis is a supervised technique in which we approach for dimensionality


reduction. Here linear combination of features is utilized to perform the classification operation.
For each pattern class, a discriminant function is defined which performs the classification
function [5] - [8]. There is not a well defined rule regarding the form of discriminant function
like minimum distance classifier uses one reference point for each class, and discriminant
function computes minimum distance from unknown vectors to these points, on the other hand
nearest neighbor classifier uses set of points for each class. There are various kinds of
Discriminant Analysis methods that are used based upon the application and system requirement
such as: Linear Discriminant Analysis (LDA), Null-LDA (N-LDA), Fisher Discriminant Analysis
(FDA), Two Dimensional Linear Discriminant Analysis (2D-LDA), Two Dimensional Fisher
Discriminant Analysis (2D-FDA). In LDA feature set is obtained by linear combination of
original features. Intra-class distance is minimized as well as inter-class distance is maximized to
obtain the optimum results.LDA suffers from small sample size (SSS) problem.

In FDA ratio of variance in inter-classes to variances in intra-classes defines the separation


between classes. In FDA inter-class scatter is maximized and intra-class scatter is minimized to
get the optimum results [8]. FDA approach is a combination of PCA and LDA. 2D-LDA avoids
small sample size (SSS) problem associated with 1D-LDA. Here matrices of input data are
computed to form the feature vector. Trace of interclass scatter matrix is maximized while trace
of intraclass scatter matrix is minimized to get the optimum results in 2 D-LDA. As compared to
1-D LDA; 2D-FDA provides nonsingular interclass and intra-class matrices. Chen et al. [9]
suggested that the null space spanned by eigenvectors of intra-class scatter matrices having zero
Eigen values contains highly discriminating information. An LDA method in null space of intraclass scatter matrix is N-LDA which involves solving the Eigen value problem for a very large
matrix. Principal Component Analysis (PCA) or Karhunen-Loeave expansion is a multi-element
unsupervised technique in which we approach for dimensionality reduction [10]. Using PCA,
patterns are detected in the data and these patterns determine the similarity measure [11]. In PCA
Eigen vectors with largest Eigen values are computed to form the feature space. PCA is closely

related to Factor Analysis [11]. Kernel PCA is a solution for nonlinear feature extraction [12],
[13].Other nonlinear feature extraction techniques are Multidimensional scaling (MDS) and
Kohonen feature Map [14]. Application areas of PCA include graphically unreliable patterns.
Discriminant Analysis is more efficient as compared to PCA, in terms of accuracy and time
elapsed [15].

D. Neural Network Based Model

Neural networks are the massively parallel structures composed of neuron like subunits [29].
Neural networks provide efficient result in the field of classification. Its property of changing its
weight iteratively and learning [10], [30], give it an edge over other techniques for recognition
process.

Classification: Neural Nets


Can select more complex regions
Can be more accurate
Also can overfit the data find patterns in random noise

A typical neural network


Capabilities of Multi-Layer Neural Networks
expressiveness
weaker than predicate logic
good for continuous inputs and outputs
computational efficiency
training time can be exponential in the number of inputs
depends critically on parameters like the learning rate
local minima are problematic
can be overcome by simulated annealing, at additional cost
generalization
works reasonably well for some functions (classes of problems)

When to Consider Neural Networks


Input is high-dimensional discrete or real-valued (e.g. raw sensor input)

Output is discrete or real valued


Output is a vector of values
Possibly noisy data
Form of target function is unknown
Human readability of result is unimportant
Long training, quick evaluation

Perceptron is a primitive neuron model. It is a two layer structure. If output function of


perceptron is step, then it performs classification problems, if it is linear than it perform
regression problems. The most commonly used family of neural networks for pattern
classification is the feed forward networks like MLP and RBF networks [31]. Different types of
neural networks are used depending upon the requirement of the application. Feed Forward
Back-propagation Neural Network (FFBPNN) is used to implement non-linear differentiable
functions. Increase in the learning rate in back-propagation neural network leads to decrease in
convergence time [32]. General Regression neural network (GRNN) is a highly parallel structure
in which learning is from input side to output side [33]. General Regression Neural Network
(GRNN) performs efficiently on noisy data than Back-propagation. FFBP Neural Network does
not work accurately if available data is large enough. On the other hand in GRNN, as the size of
data increases, the error approaches towards zero [33]. KohnenNetworks are mainly used for
data clustering and feature mapping [14]. Ripley [34] and Anderson et al. [35] stated the
relationship between neural networks and statistical model of pattern recognition.
The performance of the neural networks enhances upon increasing the number of hidden layers
up to a certain extent. Increased number of neurons in hidden layer also improves the
performance of the system. No. of neurons must be large enough to adequately represent the
problem domain and small enough to permit the generalization from training data. A trade-off
must be maintained between size of network and complexity resulted because of network size.
Percentage recognition accuracy of a neural network can be further enhanced if we use

'tansig'-'tansig' combination of activation functions for neurons of hidden layer and output layer
opted rather than opting for other combinations [36].

Regression Analysis: A statistical procedure used to find relationships among a set of variables
In regression analysis, there is a dependent variable, which is the one you are trying to explain,
and one or more independent variables that are related to it.
You can express the relationship as a linear equation, such as:
y = a + bx

y is the dependent variable


x is the independent variable
a is a constant
b is the slope of the line
For every increase of 1 in x, y changes by an amount equal to b
Some relationships are perfectly linear and fit this equation exactly. Your cell phone bill, for
instance, may be:
Total Charges = Base Fee + 30 (overage minutes)
If you know the base fee and the number of overage minutes, you can predict the total charges
exactly
Other relationships may not be so exact.
Weight, for instance, is to some degree a function of height, but there are variations that height
does not explain.
On average, you might have an equation like:
Weight = -222 + 5.7*Height
If you take a sample of actual heights and weights, you might see something like the graph to the
right.

Often, none of the actual observations lie on the line.


The difference between the line and any individual observation is the error.
The new equation is:
Weight = -222 + 5.7*Height + e
This equation does not mean that people who are short enough will have a negative weight.
The observations that contributed to this analysis were all for heights between 5 and 64.
You cannot, however, extrapolate the results to heights outside of those observed.
The regression results are only valid for the range of actual observations.

TYPICAL APPLICATIONS

Sorting Fish: incoming fish are sorted according to species using optical sensing (sea bass or
salmon?)

Problem Analysis:
set up a camera and take some sample images to extract features
Consider features such as length, lightness, width, number and shape of fins, position of
mouth, etc.
Sorting incoming Fish on a conveyor according to species using optical sensing

Problem Analysis
Set up a camera and take some sample images to extract features
Length
Lightness
Width
Number and shape of fins
Position of the mouth, etc
This is the set of all suggested features to explore for use in our classifier!
Preprocessing
Use a segmentation operation to isolate fishes from one another and from the background
Information from a single fish is sent to a feature extractor whose purpose is to reduce the data
by measuring certain features

The features are passed to a classifier


Classification
Select the length of the fish as a possible feature for discrimination

The length is a poor feature alone!


Select the lightness as a possible feature.

Threshold decision boundary and cost relationship


Move our decision boundary toward smaller values of lightness in order to minimize the cost
(reduce the number of sea bass that are classified salmon!)

Task of decision theory


Adopt the lightness and add the width of the fish

We might add other features that are not correlated with the ones we already have. A precaution
should be taken not to reduce the performance by adding noisy features

Ideally, the best decision boundary should be the one which provides an optimal performance
such as in the following figure:

However, our satisfaction is premature because the central aim of designing a classifier is to
correctly classify novel input

Issue of generalization!

should we add as more features as we can?


do not use redundant features
consider noise in the measurements
moreover,
avoid adding too many features
more features means higher dimensional feature vectors
difficult to work in high dimensional spaces
this is called the curse of dimensionality
more on this later

Application:
Face Detection:
Face Detection and Tracking Using CAMShift and Viola Jones algorithm
This example shows how to automatically detect and track a face.
Introduction
Object detection and tracking are important in many computer vision applications including
activity recognition, automotive safety, and surveillance. In this example, you will develop a
simple face tracking system by dividing the tracking problem into three separate problems:
1.

Detect a face to track

2.

Identify facial features to track

3.

Track the face

Step 1: Detect a Face To Track


Before you begin tracking a face, you need to first detect it. Use
the vision.CascadeObjectDetector to detect the location of a face in a video frame. The cascade
object detector uses the Viola-Jones detection algorithm and a trained classification model for
detection. By default, the detector is configured to detect faces, but it can be configured for other
object types.
Step 2: Identify Facial Features To Track
Once the face is located in the video, the next step is to identify a feature that will help you track
the face. For example, you can use the shape, texture, or color. Choose a feature that is unique to
the object and remains invariant even when the object moves.
In this example, you use skin tone as the feature to track. The skin tone provides a good deal of
contrast between the face and the background and does not change as the face rotates or moves.

Step 3: Track the Face


With the skin tone selected as the feature to track, you can now use
the vision.HistogramBasedTracker for tracking. The histogram based tracker uses the CAMShift
algorithm, which provides the capability to track an object using a histogram of pixel values. In
this example, the Hue channel pixels are extracted from the nose region of the detected face.
These pixels are used to initialize the histogram for the tracker. The example tracks the object
over successive video frames using this histogram.
Facetracker.m
% Create a cascade detector object.
faceDetector = vision.CascadeObjectDetector();
% Read a video frame and run the detector.
videoFileReader = vision.VideoFileReader('1.wmv');
videoFrame = step(videoFileReader);
bbox
= step(faceDetector, videoFrame);
% Draw the returned bounding box around the detected face.
videoOut = insertObjectAnnotation(videoFrame,'rectangle',bbox,'Face');
figure, imshow(videoOut), title('Detected face');
% Get the skin tone information by extracting the Hue from the video frame
% converted to the HSV color space.
[hueChannel,~,~] = rgb2hsv(videoFrame);
% Display the Hue Channel data and draw the bounding box around the face.
figure, imshow(hueChannel), title('Hue channel data');
rectangle('Position',bbox(1,:),'LineWidth',2,'EdgeColor',[1 1 0])
% Detect the nose within the face region. The nose provides a more accurate
% measure of the skin tone because it does not contain any background
% pixels.
noseDetector = vision.CascadeObjectDetector('Nose', 'UseROI', true);
noseBBox = step(noseDetector, videoFrame, bbox(1,:));
% Create a tracker object.
tracker = vision.HistogramBasedTracker;
% Initialize the tracker histogram using the Hue channel pixels from the
% nose.
initializeObject(tracker, hueChannel, noseBBox(1,:));

% Create a video player object for displaying video frames.


videoInfo = info(videoFileReader);
videoPlayer = vision.VideoPlayer('Position',[300 300 videoInfo.VideoSize+30]);
% Track the face over successive video frames until the video is finished.
while ~isDone(videoFileReader)
% Extract the next video frame
videoFrame = step(videoFileReader);
% RGB -> HSV
[hueChannel,~,~] = rgb2hsv(videoFrame);
% Track using the Hue channel data
bbox = step(tracker, hueChannel);
% Insert a bounding box around the object being tracked
videoOut = insertObjectAnnotation(videoFrame,'rectangle',bbox,'Face');
% Display the annotated video frame using the video player object
step(videoPlayer, videoOut);
end
% Release resources
release(videoFileReader);
release(videoPlayer);

FACE RECOGNITION:
Face recognition is the recognizing a special face from a set of different faces. Face has a
significant role in human beings communications where, each person along with his/her feelings
mainly is distinguished by his/her face image. One can easily find out that one of the main
problems in machine-human being interactions is the face recognition problem. A human face is
a complex object with features varying over time. So a robust face recognition system must
operate under a variety of conditions. Face recognition has been undoubtedly one of the major
topics in the image processing and pattern recognition in the last decade due to the new interests
in, security, smart environments, video indexing and access control. Existing and future

applications of face recognition are many [1]. We divide these applications into two main
categories of governmental and commercial uses. Rapid progression
through customs by using face as a live passport in immigration, comparison of surveillance
images against an image database of known terrorists and other unwanted people in
security/counterterrorism, and Verifying identity of people found unconscious, dead or
individuals refusing to identify themselves in hospital are examples of governmental uses.
Withdrawing cash from an automated teller machine (ATM) without cards or pin numbers in
banking and access control of home and office in premises access control are some examples of
commercial uses of face recognition systems which demonstrate the importance of these
systems.
A new Hidden Markov Model (HMM)-based face recognition system is designed. As a novel
point despite of five-state HMM used in pervious researches, we add two new face regions,
eyebrows and chin, to the model. As another novel point, we used a small number of quantized
Singular Values Decomposition (SVD) coefficients as features describing blocks of face images.
This makes the system very fast. The system has been evaluated on the Olivetti Research
Laboratory (ORL) face database. In order to additional reduction in computational complexity
and memory consumption the images are resized to 6464 jpeg format. Before anything, an
order-statistic filter is used as a preprocessing operation. Then a top-down sequence of
overlapping sub-image blocks is considered. Using quantized SVD coefficients of these blocks,
each face is considered as a numerical sequence that can be easily modeled by HMM. The
system has been examined on 400 face images of the Olivetti Research Laboratory (ORL) face
database. The experiments showed a recognition rate of 99%, using half of the images for
training.
Mainmenu.m
% Face Recognition System
% Original Paper :
% H. Miar-Naimi and P. Davari A New Fast and Efficient HMM-Based
% Face Recognition System Using a 7-State HMM Along With SVD Coefficients
clear all;
close all;
clc;
if (exist('DATABASE.mat','file'))
load DATABASE.mat;
end
while (1==1)
choice=menu('Face Recognition',...
'Generate Database',...
'Calculate Recognition Rate',...
'Recognize from Image',...
'Recognize from Webcam',...

'Exit');
if (choice ==1)
if (~exist('DATABASE.mat','file'))
[myDatabase,minmax] = gendata();
else
pause(0.1);
choice2 = questdlg('Generating a new database will remove any previous trained
database. Are you sure?', ...
'Warning...',...
'Yes', ...
'No','No');
switch choice2
case 'Yes'
pause(0.1);
[myDatabase minmax] = gendata();
case 'No'
end
end
end
if (choice == 2)
if (~exist('myDatabase','var'))
fprintf('Please generate database first!\n');
else
recognition_rate = testsys(myDatabase,minmax);
end
end
if (choice == 3)
if (~exist('myDatabase','var'))
fprintf('Please generate database first!\n');
else
pause(0.1);
[file_name file_path] = uigetfile ({'*.pgm';'*.jpg';'*.png'});
if file_path ~= 0
filename = [file_path,file_name];
facerec (filename,myDatabase,minmax);
end
end
end
if (choice == 4)
I = getcam();
if (~isempty(I))
filename = ['./',num2str(floor(rand()*10)+1),'.pgm'];
imwrite(I,filename);
if (exist('myDatabase','var'))
facerec (filename,myDatabase,minmax);
end

end
end
if (choice == 5)
clear choice choice2
return;
end
end
facerec.m
% Face Recognition System
function [person_index,maxlogpseq] = facerec(filename,myDatabase,minmax)
I = imread(filename);
try
I = rgb2gray(I);
end
I = imresize(I,[56 46]);
I = ordfilt2(I,1,true(3));
min_coeffs = minmax(1,:);
max_coeffs = minmax(2,:);
delta_coeffs = minmax(3,:);
seq = zeros(1,52);
for blk_begin=1:52
blk = I(blk_begin:blk_begin+4,:);
[U,S,V] = svd(double(blk));
blk_coeffs = [U(1,1) S(1,1) S(2,2)];
blk_coeffs = max([blk_coeffs;min_coeffs]);
blk_coeffs = min([blk_coeffs;max_coeffs]);
qt = floor((blk_coeffs-min_coeffs)./delta_coeffs);
label = qt(1)*7*10+qt(2)*7+qt(3)+1;
seq(1,blk_begin) = label;
end
number_of_persons_in_database = size(myDatabase,2);
results = zeros(1,number_of_persons_in_database);
for i=1:number_of_persons_in_database
TRANS = myDatabase{6,i}{1,1};
EMIS = myDatabase{6,i}{1,2};
[ignore,logpseq] = hmmdecode(seq,TRANS,EMIS);
P=exp(logpseq);
results(1,i) = P;
end
[maxlogpseq,person_index] = max(results);

fprintf(['This person is ',myDatabase{1,person_index},'.\n']);


gendata.m
% Face Recognition System
function [myDatabase,minmax] = gendata()
eps=.000001;
ufft = [1 5 6 8 10];
fprintf ('Loading Faces ...\n');
data_folder_contents = dir ('./data');
myDatabase = cell(0,0);
person_index = 0;
max_coeffs = [-Inf -Inf -Inf];
min_coeffs = [ Inf 0 0];
for person=1:size(data_folder_contents,1);
if (strcmp(data_folder_contents(person,1).name,'.') || ...
strcmp(data_folder_contents(person,1).name,'..') || ...
(data_folder_contents(person,1).isdir == 0))
continue;
end
person_index = person_index+1;
person_name = data_folder_contents(person,1).name;
myDatabase{1,person_index} = person_name;
fprintf([person_name,' ']);
person_folder_contents = dir(['./data/',person_name,'/*.pgm']);
blk_cell = cell(0,0);
for face_index=1:5
I = imread(['./data/',person_name,'/',person_folder_contents(ufft(face_index),1).name]);
I = imresize(I,[56 46]);
I = ordfilt2(I,1,true(3));
blk_index = 0;
for blk_begin=1:52
blk_index=blk_index+1;
blk = I(blk_begin:blk_begin+4,:);
[U,S,V] = svd(double(blk));
blk_coeffs = [U(1,1) S(1,1) S(2,2)];
max_coeffs = max([max_coeffs;blk_coeffs]);
min_coeffs = min([min_coeffs;blk_coeffs]);
blk_cell{blk_index,face_index} = blk_coeffs;
end
end
myDatabase{2,person_index} = blk_cell;
if (mod(person_index,10)==0)

fprintf('\n');
end
end
delta = (max_coeffs-min_coeffs)./([18 10 7]-eps);
minmax = [min_coeffs;max_coeffs;delta];
for person_index=1:40
for image_index=1:5
for block_index=1:52
blk_coeffs = myDatabase{2,person_index}{block_index,image_index};
min_coeffs = minmax(1,:);
delta_coeffs = minmax(3,:);
qt = floor((blk_coeffs-min_coeffs)./delta_coeffs);
myDatabase{3,person_index}{block_index,image_index} = qt;
label = qt(1)*10*7+qt(2)*7+qt(3)+1;
myDatabase{4,person_index}{block_index,image_index} = label;
end
myDatabase{5,person_index}{1,image_index} = cell2mat(myDatabase{4,person_index}
(:,image_index));
end
end
TRGUESS = ones(7,7) * eps;
TRGUESS(7,7) = 1;
for r=1:6
TRGUESS(r,r) = 0.6;
TRGUESS(r,r+1) = 0.4;
end
EMITGUESS = (1/1260)*ones(7,1260);
fprintf('\nTraining ...\n');
for person_index=1:40
fprintf([myDatabase{1,person_index},' ']);
seqmat = cell2mat(myDatabase{5,person_index})';
[ESTTR,ESTEMIT]=hmmtrain(seqmat,TRGUESS,EMITGUESS,'Tolerance',.01,'Maxiterations',
10,'Algorithm', 'BaumWelch');
ESTTR = max(ESTTR,eps);
ESTEMIT = max(ESTEMIT,eps);
myDatabase{6,person_index}{1,1} = ESTTR;
myDatabase{6,person_index}{1,2} = ESTEMIT;
if (mod(person_index,10)==0)
fprintf('\n');
end
end
fprintf('done.\n');
save DATABASE myDatabase minmax

testsys.m
% Face Recognition System
function recognition_rate = testsys(myDatabase,minmax)
ufft = [2 3 4 7 9];
total = 0;
recognition_rate = 0;
fprintf('Please Wait...\n');
data_folder_contents = dir ('./data');
number_of_folders_in_data_folder = size(data_folder_contents,1);
person_index = 0;
for person=1:number_of_folders_in_data_folder
if (strcmp(data_folder_contents(person,1).name,'.') || ...
strcmp(data_folder_contents(person,1).name,'..') || ...
(data_folder_contents(person,1).isdir == 0))
continue;
end
person_index = person_index+1;
person_name = data_folder_contents(person,1).name;
fprintf([person_name,'\n']);
person_folder_contents = dir(['./data/',person_name,'/*.pgm']);
for face_index=1:size(ufft,2)
total = total + 1;
filename = ['./data/',person_name,'/',person_folder_contents(ufft(face_index),1).name];
answer_person_index = facerec(filename,myDatabase,minmax);
if (answer_person_index == person_index)
recognition_rate = recognition_rate + 1;
end
end
end
recognition_rate = recognition_rate/total*100;
fprintf(['\nRecognition Rate is ',num2str(recognition_rate),'%% for a total of ',num2str(total),'
unseen faces.\n']);

Command Output:

Clicking on Generate database button

Clicked Yes button,we load the faces and train the system.
Loading Faces ...
s1 s10 s11 s12 s13 s14 s15 s16 s17 s18
s19 s2 s20 s21 s22 s23 s24 s25 s26 s27
s28 s29 s3 s30 s31 s32 s33 s34 s35 s36
s37 s38 s39 s4 s40 s5 s6 s7 s8 s9

Training ...
s1 s10 s11 s12 s13 s14 s15 s16 s17 s18
s19 s2 s20 s21 s22 s23 s24 s25 s26 s27
s28 s29 s3 s30 s31 s32 s33

s34 s35 s36


s37 s38 s39 s4 s40 s5 s6 s7 s8 s9
done.
After clicking on Calculate Recognition rate,we get the following rate on command window.
Recognition Rate is 96.5% for a total of 200 unseen faces.
Then we try to recognize a face from orl database that we are using.
We chose 8th picture of S7 from ORL database.It is recognized correctly.

(Recognized Image)
For unknown image output is as below.

Optical Character Recognition System:


OCR.m
% OCR (Optical Character Recognition).
% PRINCIPAL PROGRAM
warning off %#ok<WNOFF>
% Clear all

clc, close all, clear all


% Read image
imagen=imread('TEST_2.jpg');
% Show image
imshow(imagen);
title('INPUT IMAGE WITH NOISE')
% Convert to gray scale
if size(imagen,3)==3 %RGB image
imagen=rgb2gray(imagen);
end
% Convert to BW
threshold = graythresh(imagen);
imagen =~im2bw(imagen,threshold);
% Remove all object containing fewer than 30 pixels
imagen = bwareaopen(imagen,30);
%Storage matrix word from image
word=[ ];
re=imagen;
%Opens text.txt as file for write
fid = fopen('text.txt', 'wt');
% Load templates
load templates
global templates
% Compute the number of letters in template file
num_letras=size(templates,2);
while 1
%Fcn 'lines' separate lines in text
[fl re]=lines(re);
imgn=fl;
%Uncomment line below to see lines one by one
%imshow(fl);pause(0.5)
%----------------------------------------------------------------% Label and count connected components
[L Ne] = bwlabel(imgn);
for n=1:Ne
[r,c] = find(L==n);
% Extract letter
n1=imgn(min(r):max(r),min(c):max(c));
% Resize letter (same size of template)
img_r=imresize(n1,[42 24]);
%Uncomment line below to see letters one by one
%imshow(img_r);pause(0.5)
%------------------------------------------------------------------% Call fcn to convert image to text
letter=read_letter(img_r,num_letras);
% Letter concatenation

word=[word letter];
end
%fprintf(fid,'%s\n',lower(word));%Write 'word' in text file (lower)
fprintf(fid,'%s\n',word);%Write 'word' in text file (upper)
% Clear 'word' variable
word=[ ];
%*When the sentences finish, breaks the loop
if isempty(re) %See variable 're' in Fcn 'lines'
break
end
end
fclose(fid);
%Open 'text.txt' file
winopen('text.txt')
clear all
read_letter.m
function letter=read_letter(imagn,num_letras)
% Computes the correlation between template and input image
% and its output is a string containing the letter.
% Size of 'imagn' must be 42 x 24 pixels
% Example:
% imagn=imread('D.bmp');
% letter=read_letter(imagn)
global templates
comp=[ ];
for n=1:num_letras
sem=corr2(templates{1,n},imagn);
comp=[comp sem];
end
vd=find(comp==max(comp));
%*-*-*-*-*-*-*-*-*-*-*-*-*if vd==1
letter='A';
elseif vd==2
letter='B';
elseif vd==3
letter='C';
elseif vd==4
letter='D';
elseif vd==5
letter='E';
elseif vd==6
letter='F';

elseif vd==7
letter='G';
elseif vd==8
letter='H';
elseif vd==9
letter='I';
elseif vd==10
letter='J';
elseif vd==11
letter='K';
elseif vd==12
letter='L';
elseif vd==13
letter='M';
elseif vd==14
letter='N';
elseif vd==15
letter='O';
elseif vd==16
letter='P';
elseif vd==17
letter='Q';
elseif vd==18
letter='R';
elseif vd==19
letter='S';
elseif vd==20
letter='T';
elseif vd==21
letter='U';
elseif vd==22
letter='V';
elseif vd==23
letter='W';
elseif vd==24
letter='X';
elseif vd==25
letter='Y';
elseif vd==26
letter='Z';
%*-*-*-*-*
elseif vd==27
letter='1';
elseif vd==28
letter='2';
elseif vd==29

letter='3';
elseif vd==30
letter='4';
elseif vd==31
letter='5';
elseif vd==32
letter='6';
elseif vd==33
letter='7';
elseif vd==34
letter='8';
elseif vd==35
letter='9';
else
letter='0';
end

lines.m
function [fl re]=lines(im_texto)
% Divide text in lines
% im_texto->input image; fl->first line; re->remain line
% Example:
% im_texto=imread('TEST_3.jpg');
% [fl re]=lines(im_texto);
% subplot(3,1,1);imshow(im_texto);title('INPUT IMAGE')
% subplot(3,1,2);imshow(fl);title('FIRST LINE')
% subplot(3,1,3);imshow(re);title('REMAIN LINES')
im_texto=clip(im_texto);
num_filas=size(im_texto,1);
for s=1:num_filas
if sum(im_texto(s,:))==0
nm=im_texto(1:s-1, :); % First line matrix
rm=im_texto(s:end, :);% Remain line matrix
fl = clip(nm);
re=clip(rm);
%*-*-*Uncomment lines below to see the result*-*-*-*%
subplot(2,1,1);imshow(fl);
%
subplot(2,1,2);imshow(re);
break
else
fl=im_texto;%Only one line.
re=[ ];
end

end
function img_out=clip(img_in)
[f c]=find(img_in);
img_out=img_in(min(f):max(f),min(c):max(c));%Crops image
create_template.m
%CREATE TEMPLATES
%Letter
A=imread('letters_numbers\A.bmp');B=imread('letters_numbers\B.bmp');
C=imread('letters_numbers\C.bmp');D=imread('letters_numbers\D.bmp');
E=imread('letters_numbers\E.bmp');F=imread('letters_numbers\F.bmp');
G=imread('letters_numbers\G.bmp');H=imread('letters_numbers\H.bmp');
I=imread('letters_numbers\I.bmp');J=imread('letters_numbers\J.bmp');
K=imread('letters_numbers\K.bmp');L=imread('letters_numbers\L.bmp');
M=imread('letters_numbers\M.bmp');N=imread('letters_numbers\N.bmp');
O=imread('letters_numbers\O.bmp');P=imread('letters_numbers\P.bmp');
Q=imread('letters_numbers\Q.bmp');R=imread('letters_numbers\R.bmp');
S=imread('letters_numbers\S.bmp');T=imread('letters_numbers\T.bmp');
U=imread('letters_numbers\U.bmp');V=imread('letters_numbers\V.bmp');
W=imread('letters_numbers\W.bmp');X=imread('letters_numbers\X.bmp');
Y=imread('letters_numbers\Y.bmp');Z=imread('letters_numbers\Z.bmp');
%Number
one=imread('letters_numbers\1.bmp'); two=imread('letters_numbers\2.bmp');
three=imread('letters_numbers\3.bmp');four=imread('letters_numbers\4.bmp');
five=imread('letters_numbers\5.bmp'); six=imread('letters_numbers\6.bmp');
seven=imread('letters_numbers\7.bmp');eight=imread('letters_numbers\8.bmp');
nine=imread('letters_numbers\9.bmp'); zero=imread('letters_numbers\0.bmp');
%*-*-*-*-*-*-*-*-*-*-*letter=[A B C D E F G H I J K L M...
N O P Q R S T U V W X Y Z];
number=[one two three four five...
six seven eight nine zero];
character=[letter number];
templates=mat2cell(character,42,[24 24 24 24 24 24 24 ...
24 24 24 24 24 24 24 ...
24 24 24 24 24 24 24 ...
24 24 24 24 24 24 24 ...
24 24 24 24 24 24 24 24]);
save ('templates','templates')
clear all

Test2.jpg

Text Content
ESTA67
ES767
UNA4567
PRUEBA5887

Conclusion:
A comparative view of all the models of pattern recognition has been shown which depicts that
for various domains in this areas different models or combination of models can be used. In case
of noisy patterns, choice of statistical model is a good solution. Practical importance of structural
model depends upon recognition of simple pattern primitives and their relationships represented
by description language. As compared to statistical pattern recognition, structural pattern
recognition is a newer area of research. For complex patterns and applications utilizing large
number of pattern classes, it is beneficial to describe each pattern in terms of its components. A
wise decision regarding the selection of Pattern grammar influences computations efficiency of
recognition system. Pattern primitives and pattern grammar to be utilized depends upon the
application requirements. Low dependence of neural networks on prior knowledge and
availability of efficient learning algorithms have made the neural networks famous in the field of
Patten Recognition. Although neural networks and statistical pattern recognition models have
different principles most of the neural networks are similar to statistical pattern recognition
modelsAs each model has its own pros and cons, therefore to enhance system performance for
complex applications it is beneficial to append two or more recognition models at various stages
of recognition process.

References:
1. H. Miar-Naimi and P. Davari A New Fast and Efficient HMM-Based Face Recognition
System Using a 7-State HMM Along With SVD Coefficient
2.
http://in.mathworks.com/
3. [1]. K.S. Fu, A Step towards Unification of Syntactic and Statistical Pattern Recognition,
IEEE Trans. Pattern Analysis and Machine Intelligence, vol. 5, no. 2, pp. 200-205, Mar. 1983.
[2]. Amin Fazel and Shantnu Chakrabartty, An Overview of Statistical Pattern Recognition
Techniques for Speaker Verification, IEEE circuits and systems, pp. 61-81, magazine 2 nd
quarter 2011. [3]. L. Devroye, L. Gyorfi, and G. Lugosi, "A Probabilistic Theory of Pattern
Recognition." Berlin: Springer-Verlag, 1996. [4]. R.O.Duda and P.E.Hart, Pattern Classification
and Scene Analysis, New York: John Wiley & sons, 1973. [5]. W.W.Cooley and P.R. Lohnes,"
Multivariate Data Analysis."New York: Wiley, 1971. [6]. R.Fisher, "The Use of Multiple
Measurements in Taxonomic Problems," Ann. Eugenics, vol. 7, no. 2, pp. 179-188,1936. [7].
M.M. Tatsouka, "Multivariate Analysis."New York: Wiley, 1971

You might also like