![](http://www.weebly.com/weebly/images/file_icons/pdf.png)
note3.pdf | |
File Size: | 102 kb |
File Type: |
http://www.informatik.uni-konstanz.de/fileadmin/informatik/ag-saupe/Webpages/lehre/na_08/Lab2/5_FaceRecognition/html/myFaceRecognition.html
close all
clear all
clc
%imaqhwinfo;
%dev_info = imaqhwinfo('winvideo',1)
vid=videoinput('winvideo',1, 'YUY2_640x480');
%preview(vid);
im=getsnapshot(vid);
figure(),imshow(im);
%% conversion of data format optional
im=ycbcr2rgb(im);
figure(),imshow(im);
%% backup of image
I=im;
%%convert image to gray scale for edge detection
I = rgb2gray(I);
figure(),imshow(I);
%% Edge detection
I=edge(I,'canny');
figure(),imshow(I);
clear all
clc
%imaqhwinfo;
%dev_info = imaqhwinfo('winvideo',1)
vid=videoinput('winvideo',1, 'YUY2_640x480');
%preview(vid);
im=getsnapshot(vid);
figure(),imshow(im);
%% conversion of data format optional
im=ycbcr2rgb(im);
figure(),imshow(im);
%% backup of image
I=im;
%%convert image to gray scale for edge detection
I = rgb2gray(I);
figure(),imshow(I);
%% Edge detection
I=edge(I,'canny');
figure(),imshow(I);
Video:-
Code
clear all
clc
close all
% %imaqhwinfo;
% %dev_info = imaqhwinfo('winvideo',1)
% vid=videoinput('winvideo',1, 'YUY2_640x480');
% %preview(vid);
% im=getsnapshot(vid);
% figure(),imshow(im);
% %% conversion of data format optional
% im=ycbcr2rgb(im);
% figure(),imshow(im);
% %% backup of image
% I=im;
faceDetector = vision.CascadeObjectDetector();
I = imread('C:\Data\Feb15\9\face1.jpg'); % from video
[m,n,z]=size(I);% size
%figure, imshow(I);
bbox = step(faceDetector, I);
faceImage = imcrop(I,bbox(1,:));
%figure, imshow(faceImage);
for i = 1:1:5 %for loop till n
%% loop reading of images
a='C:\Data\Feb15\9\'; %path
b=num2str(i); %number to string
c='.jpg' % file extension
path=strcat(a,b,c); % joins strings
I=imread(path); %reads image
% figure(),imshow(I) % display to be commented
bbox = step(faceDetector, I);
T = imcrop(I,bbox(1,:));
% faceDetector = vision.CascadeObjectDetector('RightEye');
% bbox = step(faceDetector, T);
% T1 = imcrop(I,bbox(1,:));
%figure, imshow(T);
I=faceImage;
[I_SSD,I_NCC]=template_matching(T,I);
figure
subplot(2,2,1), imshow(I); title('Original image')
subplot(2,2,2), imshow(T); title('Template image');
subplot(2,2,3), imshow(I_SSD); title('SSD Matching');
subplot(2,2,4), imshow(I_NCC); title('Normalized-CC');
end
clc
close all
% %imaqhwinfo;
% %dev_info = imaqhwinfo('winvideo',1)
% vid=videoinput('winvideo',1, 'YUY2_640x480');
% %preview(vid);
% im=getsnapshot(vid);
% figure(),imshow(im);
% %% conversion of data format optional
% im=ycbcr2rgb(im);
% figure(),imshow(im);
% %% backup of image
% I=im;
faceDetector = vision.CascadeObjectDetector();
I = imread('C:\Data\Feb15\9\face1.jpg'); % from video
[m,n,z]=size(I);% size
%figure, imshow(I);
bbox = step(faceDetector, I);
faceImage = imcrop(I,bbox(1,:));
%figure, imshow(faceImage);
for i = 1:1:5 %for loop till n
%% loop reading of images
a='C:\Data\Feb15\9\'; %path
b=num2str(i); %number to string
c='.jpg' % file extension
path=strcat(a,b,c); % joins strings
I=imread(path); %reads image
% figure(),imshow(I) % display to be commented
bbox = step(faceDetector, I);
T = imcrop(I,bbox(1,:));
% faceDetector = vision.CascadeObjectDetector('RightEye');
% bbox = step(faceDetector, T);
% T1 = imcrop(I,bbox(1,:));
%figure, imshow(T);
I=faceImage;
[I_SSD,I_NCC]=template_matching(T,I);
figure
subplot(2,2,1), imshow(I); title('Original image')
subplot(2,2,2), imshow(T); title('Template image');
subplot(2,2,3), imshow(I_SSD); title('SSD Matching');
subplot(2,2,4), imshow(I_NCC); title('Normalized-CC');
end
Function save as template_matching.m
function [I_SSD,I_NCC,Idata]=template_matching(T,I,IdataIn)
% [I_SSD,I_NCC,Idata]=template_matching(T,I,Idata)
%
% inputs,
% T : Image Template, can be grayscale or color 2D or 3D.
% I : Color image, can be grayscale or color 2D or 3D.
% (optional)
% Idata : Storage of temporary variables from the image I, to allow
% faster search for multiple templates in the same image.
%
% outputs,
% I_SSD: The sum of squared difference 2D/3D image. The SSD sign is
% reversed and normalized to range [0 1]
% I_NCC: The normalized cross correlation 2D/3D image. The values
% range between 0 and 1
% Idata : Storage of temporary variables from the image I, to allow
% faster search for multiple templates in the same image.
%
% Example 2D,
% % Find maximum response
% I = im2double(imread('lena.jpg'));
% % Template of Eye Lena
% T=I(124:140,124:140,:);
% % Calculate SSD and NCC between Template and Image
% [I_SSD,I_NCC]=template_matching(T,I);
% % Find maximum correspondence in I_SDD image
% [x,y]=find(I_SSD==max(I_SSD(:)));
% % Show result
% figure,
% subplot(2,2,1), imshow(I); hold on; plot(y,x,'r*'); title('Result')
% subplot(2,2,2), imshow(T); title('The eye template');
% subplot(2,2,3), imshow(I_SSD); title('SSD Matching');
% subplot(2,2,4), imshow(I_NCC); title('Normalized-CC');
%
% Example 3D,
% % Make some random data
% I=rand(50,60,50);
% % Get a small volume as template
% T=I(20:30,20:30,20:30);
% % Calculate SDD between template and image
% I_SSD=template_matching(T,I);
% % Find maximum correspondence
% [x,y,z]=ind2sub(size(I_SSD),find(I_SSD==max(I_SSD(:))));
% disp(x);
% disp(y);
% disp(z);
%
if(nargin<3), IdataIn=[]; end
% Convert images to double
T=double(T); I=double(I);
if(size(T,3)==3)
% Color Image detected
[I_SSD,I_NCC,Idata]=template_matching_color(T,I,IdataIn);
else
% Grayscale image or 3D volume
[I_SSD,I_NCC,Idata]=template_matching_gray(T,I,IdataIn);
end
function [I_SSD,I_NCC,Idata]=template_matching_color(T,I,IdataIn)
if(isempty(IdataIn)), IdataIn.r=[]; IdataIn.g=[]; IdataIn.b=[]; end
% Splite color image, and do template matching on R,G and B image
[I_SSD_R,I_NCC_R,Idata.r]=template_matching_gray(T(:,:,1),I(:,:,1),IdataIn.r);
[I_SSD_G,I_NCC_G,Idata.g]=template_matching_gray(T(:,:,2),I(:,:,2),IdataIn.g);
[I_SSD_B,I_NCC_B,Idata.b]=template_matching_gray(T(:,:,3),I(:,:,3),IdataIn.b);
% Combine the results
I_SSD=(I_SSD_R+I_SSD_G+I_SSD_B)/3;
I_NCC=(I_NCC_R+I_NCC_G+I_NCC_B)/3;
function [I_SSD,I_NCC,Idata]=template_matching_gray(T,I,IdataIn)
% Calculate correlation output size = input size + padding template
T_size = size(T); I_size = size(I);
outsize = I_size + T_size-1;
% calculate correlation in frequency domain
if(length(T_size)==2)
FT = fft2(rot90(T,2),outsize(1),outsize(2));
if(isempty(IdataIn))
Idata.FI = fft2(I,outsize(1),outsize(2));
else
Idata.FI=IdataIn.FI;
end
Icorr = real(ifft2(Idata.FI.* FT));
else
FT = fftn(rot90_3D(T),outsize);
FI = fftn(I,outsize);
Icorr = real(ifftn(FI.* FT));
end
% Calculate Local Quadratic sum of Image and Template
if(isempty(IdataIn))
Idata.LocalQSumI= local_sum(I.*I,T_size);
else
Idata.LocalQSumI=IdataIn.LocalQSumI;
end
QSumT = sum(T(:).^2);
% SSD between template and image
I_SSD=Idata.LocalQSumI+QSumT-2*Icorr;
% Normalize to range 0..1
I_SSD=I_SSD-min(I_SSD(:));
I_SSD=1-(I_SSD./max(I_SSD(:)));
% Remove padding
I_SSD=unpadarray(I_SSD,size(I));
if (nargout>1)
% Normalized cross correlation STD
if(isempty(IdataIn))
Idata.LocalSumI= local_sum(I,T_size);
else
Idata.LocalSumI=IdataIn.LocalSumI;
end
% Standard deviation
if(isempty(IdataIn))
Idata.stdI=sqrt(max(Idata.LocalQSumI-(Idata.LocalSumI.^2)/numel(T),0) );
else
Idata.stdI=IdataIn.stdI;
end
stdT=sqrt(numel(T)-1)*std(T(:));
% Mean compensation
meanIT=Idata.LocalSumI*sum(T(:))/numel(T);
I_NCC= 0.5+(Icorr-meanIT)./ (2*stdT*max(Idata.stdI,stdT/1e5));
% Remove padding
I_NCC=unpadarray(I_NCC,size(I));
end
function T=rot90_3D(T)
T=flipdim(flipdim(flipdim(T,1),2),3);
function B=unpadarray(A,Bsize)
B((size(A)-Bsize)/2)+1;
Bend=Bstart+Bsize-1;
if(ndims(A)==2)
B=A(Bstart(1):Bend(1),Bstart(2):Bend(2));
elseif(ndims(A)==3)
B=A(Bstart(1):Bend(1),Bstart(2):Bend(2),Bstart(3):Bend(3));
end
function local_sum_I= local_sum(I,T_size)
% Add padding to the image
B = padarray(I,T_size);
% Calculate for each pixel the sum of the region around it,
% with the region the size of the template.
if(length(T_size)==2)
% 2D localsum
s = cumsum(B,1);
c = s(1+T_size(1):end-1,:)-s(1:end-T_size(1)-1,:);
s = cumsum(c,2);
local_sum_I= s(:,1+T_size(2):end-1)-s(:,1:end-T_size(2)-1);
else
% 3D Localsum
s = cumsum(B,1);
c = s(1+T_size(1):end-1,:,:)-s(1:end-T_size(1)-1,:,:);
s = cumsum(c,2);
c = s(:,1+T_size(2):end-1,:)-s(:,1:end-T_size(2)-1,:);
s = cumsum(c,3);
local_sum_I = s(:,:,1+T_size(3):end-1)-s(:,:,1:end-T_size(3)-1);
end
% [I_SSD,I_NCC,Idata]=template_matching(T,I,Idata)
%
% inputs,
% T : Image Template, can be grayscale or color 2D or 3D.
% I : Color image, can be grayscale or color 2D or 3D.
% (optional)
% Idata : Storage of temporary variables from the image I, to allow
% faster search for multiple templates in the same image.
%
% outputs,
% I_SSD: The sum of squared difference 2D/3D image. The SSD sign is
% reversed and normalized to range [0 1]
% I_NCC: The normalized cross correlation 2D/3D image. The values
% range between 0 and 1
% Idata : Storage of temporary variables from the image I, to allow
% faster search for multiple templates in the same image.
%
% Example 2D,
% % Find maximum response
% I = im2double(imread('lena.jpg'));
% % Template of Eye Lena
% T=I(124:140,124:140,:);
% % Calculate SSD and NCC between Template and Image
% [I_SSD,I_NCC]=template_matching(T,I);
% % Find maximum correspondence in I_SDD image
% [x,y]=find(I_SSD==max(I_SSD(:)));
% % Show result
% figure,
% subplot(2,2,1), imshow(I); hold on; plot(y,x,'r*'); title('Result')
% subplot(2,2,2), imshow(T); title('The eye template');
% subplot(2,2,3), imshow(I_SSD); title('SSD Matching');
% subplot(2,2,4), imshow(I_NCC); title('Normalized-CC');
%
% Example 3D,
% % Make some random data
% I=rand(50,60,50);
% % Get a small volume as template
% T=I(20:30,20:30,20:30);
% % Calculate SDD between template and image
% I_SSD=template_matching(T,I);
% % Find maximum correspondence
% [x,y,z]=ind2sub(size(I_SSD),find(I_SSD==max(I_SSD(:))));
% disp(x);
% disp(y);
% disp(z);
%
if(nargin<3), IdataIn=[]; end
% Convert images to double
T=double(T); I=double(I);
if(size(T,3)==3)
% Color Image detected
[I_SSD,I_NCC,Idata]=template_matching_color(T,I,IdataIn);
else
% Grayscale image or 3D volume
[I_SSD,I_NCC,Idata]=template_matching_gray(T,I,IdataIn);
end
function [I_SSD,I_NCC,Idata]=template_matching_color(T,I,IdataIn)
if(isempty(IdataIn)), IdataIn.r=[]; IdataIn.g=[]; IdataIn.b=[]; end
% Splite color image, and do template matching on R,G and B image
[I_SSD_R,I_NCC_R,Idata.r]=template_matching_gray(T(:,:,1),I(:,:,1),IdataIn.r);
[I_SSD_G,I_NCC_G,Idata.g]=template_matching_gray(T(:,:,2),I(:,:,2),IdataIn.g);
[I_SSD_B,I_NCC_B,Idata.b]=template_matching_gray(T(:,:,3),I(:,:,3),IdataIn.b);
% Combine the results
I_SSD=(I_SSD_R+I_SSD_G+I_SSD_B)/3;
I_NCC=(I_NCC_R+I_NCC_G+I_NCC_B)/3;
function [I_SSD,I_NCC,Idata]=template_matching_gray(T,I,IdataIn)
% Calculate correlation output size = input size + padding template
T_size = size(T); I_size = size(I);
outsize = I_size + T_size-1;
% calculate correlation in frequency domain
if(length(T_size)==2)
FT = fft2(rot90(T,2),outsize(1),outsize(2));
if(isempty(IdataIn))
Idata.FI = fft2(I,outsize(1),outsize(2));
else
Idata.FI=IdataIn.FI;
end
Icorr = real(ifft2(Idata.FI.* FT));
else
FT = fftn(rot90_3D(T),outsize);
FI = fftn(I,outsize);
Icorr = real(ifftn(FI.* FT));
end
% Calculate Local Quadratic sum of Image and Template
if(isempty(IdataIn))
Idata.LocalQSumI= local_sum(I.*I,T_size);
else
Idata.LocalQSumI=IdataIn.LocalQSumI;
end
QSumT = sum(T(:).^2);
% SSD between template and image
I_SSD=Idata.LocalQSumI+QSumT-2*Icorr;
% Normalize to range 0..1
I_SSD=I_SSD-min(I_SSD(:));
I_SSD=1-(I_SSD./max(I_SSD(:)));
% Remove padding
I_SSD=unpadarray(I_SSD,size(I));
if (nargout>1)
% Normalized cross correlation STD
if(isempty(IdataIn))
Idata.LocalSumI= local_sum(I,T_size);
else
Idata.LocalSumI=IdataIn.LocalSumI;
end
% Standard deviation
if(isempty(IdataIn))
Idata.stdI=sqrt(max(Idata.LocalQSumI-(Idata.LocalSumI.^2)/numel(T),0) );
else
Idata.stdI=IdataIn.stdI;
end
stdT=sqrt(numel(T)-1)*std(T(:));
% Mean compensation
meanIT=Idata.LocalSumI*sum(T(:))/numel(T);
I_NCC= 0.5+(Icorr-meanIT)./ (2*stdT*max(Idata.stdI,stdT/1e5));
% Remove padding
I_NCC=unpadarray(I_NCC,size(I));
end
function T=rot90_3D(T)
T=flipdim(flipdim(flipdim(T,1),2),3);
function B=unpadarray(A,Bsize)
B((size(A)-Bsize)/2)+1;
Bend=Bstart+Bsize-1;
if(ndims(A)==2)
B=A(Bstart(1):Bend(1),Bstart(2):Bend(2));
elseif(ndims(A)==3)
B=A(Bstart(1):Bend(1),Bstart(2):Bend(2),Bstart(3):Bend(3));
end
function local_sum_I= local_sum(I,T_size)
% Add padding to the image
B = padarray(I,T_size);
% Calculate for each pixel the sum of the region around it,
% with the region the size of the template.
if(length(T_size)==2)
% 2D localsum
s = cumsum(B,1);
c = s(1+T_size(1):end-1,:)-s(1:end-T_size(1)-1,:);
s = cumsum(c,2);
local_sum_I= s(:,1+T_size(2):end-1)-s(:,1:end-T_size(2)-1);
else
% 3D Localsum
s = cumsum(B,1);
c = s(1+T_size(1):end-1,:,:)-s(1:end-T_size(1)-1,:,:);
s = cumsum(c,2);
c = s(:,1+T_size(2):end-1,:)-s(:,1:end-T_size(2)-1,:);
s = cumsum(c,3);
local_sum_I = s(:,:,1+T_size(3):end-1)-s(:,:,1:end-T_size(3)-1);
end
Save as facerecog.m
function [recognized_img]=facerecog(datapath,testimg)
D = dir(datapath); % D is a Lx1 structure with 4 fields as: name,date,byte,isdir of all L files present in the directory 'datapath'
imgcount = 0;
for i=1 : size(D,1)
if not(strcmp(D(i).name,'.')|strcmp(D(i).name,'..')|strcmp(D(i).name,'Thumbs.db'))
imgcount = imgcount + 1; % Number of all images in the training database
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%% creating the image matrix X %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
imgcount=6;
X = [];
for i = 1 : imgcount
str = strcat(datapath,'\',int2str(i),'.jpg');
img = imread(str);
img = rgb2gray(img);
[r c] = size(img);
temp = reshape(img',r*c,1); %% Reshaping 2D images into 1D image vectors
%% here img' is used because reshape(A,M,N) function reads the matrix A columnwise
%% where as an image matrix is constructed with first N pixels as first row,next N in second row so on
X = [X temp]; %% X,the image matrix with columnsgetting added for each image
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Now we calculate m, A and eigenfaces.The descriptions are below :
%
% m - (MxN)x1 Mean of the training images
% A - (MxN)xP Matrix of image vectors after each vector getting subtracted from the mean vector m
% eigenfaces - (MxN)xP' P' Eigenvectors of Covariance matrix (C) of training database X
% where P' is the number of eigenvalues of C that best represent the feature set
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% calculating mean image vector %%%%%
m = mean(X,2); % Computing the average face image m = (1/P)*sum(Xj's) (j = 1 : P)
imgcount = size(X,2);
%%%%%%%% calculating A matrix, i.e. after subtraction of all image vectors from the mean image vector %%%%%%
A = [];
for i=1 : imgcount
temp = double(X(:,i)) - m;
A = [A temp];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% CALCULATION OF EIGENFACES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% we know that for a MxN matrix, the maximum number of non-zero eigenvalues that its covariance matrix can have
%%% is min[M-1,N-1]. As the number of dimensions (pixels) of each image vector is very high compared to number of
%%% test images here, so number of non-zero eigenvalues of C will be maximum P-1 (P being the number of test images)
%%% if we calculate eigenvalues & eigenvectors of C = A*A' , then it will be very time consuming as well as memory.
%%% so we calculate eigenvalues & eigenvectors of L = A'*A , whose eigenvectors will be linearly related to eigenvectors of C.
%%% these eigenvectors being calculated from non-zero eigenvalues of C, will represent the best feature sets.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
L= A' * A;
[V,D]=eig(L); %% V : eigenvector matrix D : eigenvalue matrix
%%%% again we use Kaiser's rule here to find how many Principal Components (eigenvectors) to be taken
%%%% if corresponding eigenvalue is greater than 1, then the eigenvector will be chosen for creating eigenface
L_eig_vec = [];
for i = 1 : size(V,2)
if( D(i,i) > 1 )
L_eig_vec = [L_eig_vec V(:,i)];
end
end
%%% finally the eigenfaces %%%
eigenfaces = A * L_eig_vec;
%In this part of recognition, we compare two faces by projecting the images into facespace and
% measuring the Euclidean distance between them.
%
% recogimg - the recognized image name
% testimg - the path of test image
% m - mean image vector
% A - mean subtracted image vector matrix
% eigenfaces - eigenfaces that are calculated from eigenface function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% finding the projection of each image vector on the facespace (where the eigenfaces are the co-ordinates or dimensions) %%%%%
projectimg = [ ]; % projected image vector matrix
for i = 1 : size(eigenfaces,2)
temp = eigenfaces' * A(:,i);
projectimg = [projectimg temp];
end
%%%%% extractiing PCA features of the test image %%%%%
test_image = imread(testimg);
test_image = test_image(:,:,1);
[r c] = size(test_image);
temp = reshape(test_image',r*c,1); % creating (MxN)x1 image vector from the 2D image
temp = double(temp)-m; % mean subtracted vector
projtestimg = eigenfaces'*temp; % projection of test image onto the facespace
%%%%% calculating & comparing the euclidian distance of all projected trained images from the projected test image %%%%%
euclide_dist = [ ];
for i=1 : size(eigenfaces,2)
temp = (norm(projtestimg-projectimg(:,i)))^2;
euclide_dist = [euclide_dist temp];
end
[euclide_dist_min recognized_index] = min(euclide_dist);
recognized_img = strcat(int2str(recognized_index),'.jpg');
Save as any name
clear all
clc
close all
% %imaqhwinfo;
% %dev_info = imaqhwinfo('winvideo',1)
% vid=videoinput('winvideo',1, 'YUY2_640x480');
% %preview(vid);
% im=getsnapshot(vid);
% figure(),imshow(im);
% %% conversion of data format optional
% im=ycbcr2rgb(im);
% figure(),imshow(im);
% %% backup of image
% I=im;
faceDetector = vision.CascadeObjectDetector();
I = imread('C:\Data\February14\18\face1.jpg'); % from video
[m,n,z]=size(I);% size
%figure, imshow(I);
bbox = step(faceDetector, I);
faceImage = imcrop(I,bbox(1,:));
figure, imshow(faceImage);
for i = 1:1:6 %for loop till n
%% loop reading of images
a='C:\Data\February14\18\'; %path
b=num2str(i); %number to string
c='.jpg' % file extension
path=strcat(a,b,c); % joins strings
I=imread(path); %reads image
% figure(),imshow(I) % display to be commented
bbox = step(faceDetector, I);
T = imcrop(I,bbox(1,:));
% faceDetector = vision.CascadeObjectDetector('RightEye');
% bbox = step(faceDetector, T);
% T1 = imcrop(I,bbox(1,:));
figure, imshow(T);
I=faceImage;
% [I_SSD,I_NCC]=template_matching(T,I);
% figure
% subplot(2,2,1), imshow(I); title('Original image')
% subplot(2,2,2), imshow(T); title('Template image');
% subplot(2,2,3), imshow(I_SSD); title('SSD Matching');
% subplot(2,2,4), imshow(I_NCC); title('Normalized-CC');
end
clc
close all
% %imaqhwinfo;
% %dev_info = imaqhwinfo('winvideo',1)
% vid=videoinput('winvideo',1, 'YUY2_640x480');
% %preview(vid);
% im=getsnapshot(vid);
% figure(),imshow(im);
% %% conversion of data format optional
% im=ycbcr2rgb(im);
% figure(),imshow(im);
% %% backup of image
% I=im;
faceDetector = vision.CascadeObjectDetector();
I = imread('C:\Data\February14\18\face1.jpg'); % from video
[m,n,z]=size(I);% size
%figure, imshow(I);
bbox = step(faceDetector, I);
faceImage = imcrop(I,bbox(1,:));
figure, imshow(faceImage);
for i = 1:1:6 %for loop till n
%% loop reading of images
a='C:\Data\February14\18\'; %path
b=num2str(i); %number to string
c='.jpg' % file extension
path=strcat(a,b,c); % joins strings
I=imread(path); %reads image
% figure(),imshow(I) % display to be commented
bbox = step(faceDetector, I);
T = imcrop(I,bbox(1,:));
% faceDetector = vision.CascadeObjectDetector('RightEye');
% bbox = step(faceDetector, T);
% T1 = imcrop(I,bbox(1,:));
figure, imshow(T);
I=faceImage;
% [I_SSD,I_NCC]=template_matching(T,I);
% figure
% subplot(2,2,1), imshow(I); title('Original image')
% subplot(2,2,2), imshow(T); title('Template image');
% subplot(2,2,3), imshow(I_SSD); title('SSD Matching');
% subplot(2,2,4), imshow(I_NCC); title('Normalized-CC');
end
maincode for today
clear all
close all
clc
datapath = uigetdir('C:\Data\February14\18\','select path of training images');
testpath = uigetdir('C:\Data\February14\18\','select path of test images');
prompt = {'Enter test image name (a number between 1 to 5):'};
dlg_title = 'Person identification system';
num_lines= 1;
def = {' Type 4 here'};
TestImage = inputdlg(prompt,dlg_title,num_lines,def);
TestImage = strcat(testpath,'\',char(TestImage),'.jpg');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%% calling the functions %%%%%%%%%%%%%%%%%%%%%%%%
recog_img = facerecog(datapath,TestImage);
selected_img = strcat(datapath,'\',recog_img);
select_img = imread(selected_img);
imshow(select_img);
title('Recognized Image');
test_img = imread(TestImage);
figure,imshow(test_img);
title('Test Image');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
result = strcat('the recognized image is : ',recog_img);
disp(result);
close all
clear all
clc
%dev_info = imaqhwinfo('winvideo',1)
vid=videoinput('winvideo',1, 'YUY2_640x480');
%preview(vid);
%vid=videoinput('winvideo',1,);
vid.ReturnedColorspace = 'rgb';
%start(vid);
preview(vid);
keydown = waitforbuttonpress;
%keydown = waitforbuttonpress;
stoppreview(vid);
%stop(vid)
I=getsnapshot(vid);
%I=ycbcr2rgb(I);
figure(1),imshow(I);
%[x1 y1 button] = ginput(1);
%% Crop Image Using Submatrix Operation
[y,x] = ginput(2); %select two cursor points
r1 = x(1,1); c1 = y(1,1); %get first cursor point = first corner of the rectangle
r2 = x(2,1); c2 = y(2,1); %get second cursor point = second corner of the rectangle
b = I(r1:r2,c1:c2,:); %create the sub-matrix
imshow(b)
clear all
clc
%dev_info = imaqhwinfo('winvideo',1)
vid=videoinput('winvideo',1, 'YUY2_640x480');
%preview(vid);
%vid=videoinput('winvideo',1,);
vid.ReturnedColorspace = 'rgb';
%start(vid);
preview(vid);
keydown = waitforbuttonpress;
%keydown = waitforbuttonpress;
stoppreview(vid);
%stop(vid)
I=getsnapshot(vid);
%I=ycbcr2rgb(I);
figure(1),imshow(I);
%[x1 y1 button] = ginput(1);
%% Crop Image Using Submatrix Operation
[y,x] = ginput(2); %select two cursor points
r1 = x(1,1); c1 = y(1,1); %get first cursor point = first corner of the rectangle
r2 = x(2,1); c2 = y(2,1); %get second cursor point = second corner of the rectangle
b = I(r1:r2,c1:c2,:); %create the sub-matrix
imshow(b)