0% found this document useful (0 votes)
68 views2 pages

%read The Image, and Capture The Dimensions

This document contains code to perform face recognition using principal component analysis (PCA). It loads training images, calculates the mean image and mean-shifted images. It then computes the covariance matrix and performs eigendecomposition to obtain the eigenvalues and eigenvectors. Weights are calculated by projecting the mean-shifted images onto the eigenvectors. For a test image, the same steps are performed to obtain weights, which are then compared to the training image weights using Euclidean distance to find the most similar image.

Uploaded by

Atersaw Tigyhun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views2 pages

%read The Image, and Capture The Dimensions

This document contains code to perform face recognition using principal component analysis (PCA). It loads training images, calculates the mean image and mean-shifted images. It then computes the covariance matrix and performs eigendecomposition to obtain the eigenvalues and eigenvectors. Weights are calculated by projecting the mean-shifted images onto the eigenvectors. For a test image, the same steps are performed to obtain weights, which are then compared to the training image weights using Euclidean distance to find the most similar image.

Uploaded by

Atersaw Tigyhun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

%Read the image, and capture the dimensions

img_orig = imread('hand.jpg');
height = size(img_orig,1);
width = size(img_orig,2);

%Initialize the output images


out = img_orig;
bin = zeros(height,width);

%Convert the image from RGB to YCbCr


img_ycbcr = rgb2ycbcr(img_orig);
Cb = img_ycbcr(:,:,2);
Cr = img_ycbcr(:,:,3);

%Detect Skin
[r,c,v] = find(Cb>=77 & Cb<=127 & Cr>=133 & Cr<=173);
numind = size(r,1);

%Mark Skin Pixels


for i=1:numind
out(r(i),c(i),:) = [0 0 255];
bin(r(i),c(i)) = 1;
end
imshow(img_orig);
figure; imshow(out);
figure; imshow(bin);

%now for count of fingers

img2=im2bw(bin,graythresh(bin));
imshow(img2)
img2=~img2;
imshow(img2)
B = bwboundaries(img2);
imshow(img2)
text(10,10,strcat('\color{green}Objects Found:',num2str(length(B))))
hold on
=

import os
from PIL import Image
import numpy as np
import glob
import numpy.linalg as linalg

#Step 1: put training images into a 2D array


filenames = glob.glob('C:\\Users\\Karim\\Desktop\\Training & Test images\\New folder\\Training/*.png')
filenames.sort()
img = [Image.open(fn).convert('L').resize((90, 90)) for fn in filenames]
images = np.asarray([np.array(im).flatten() for im in img])
#Step 2: find the mean image and the mean-shifted input images
mean_image = images.mean(axis=0)
shifted_images = images - mean_image

#Step 3: Covariance
c = np.asmatrix(shifted_images) * np.asmatrix(shifted_images.T)

#Step 4: Sorted eigenvalues and eigenvectors


eigenvalues,eigenvectors = linalg.eig(c)
idx = np.argsort(-eigenvalues)
eigenvalues = eigenvalues[idx]
eigenvectors = eigenvectors[:, idx]

#Step 6: Finding weights


w = eigenvectors.T * np.asmatrix(shifted_images)
w = np.asarray(w)

#Step 7: Input (Test) image


input_image = Image.open('C:\\Users\\Karim\\Desktop\\Training & Test images\\New
folder\\Test\\31.png').convert('L').resize((90, 90))
input_image = np.asarray(input_image).flatten()

#Step 8: get the normalized image, covariance, eigenvalues and eigenvectors for input image
shifted_in = input_image - mean_image
c = np.cov(input_image)
cmat = c.reshape(1,1)
eigenvalues_in, eigenvectors_in = linalg.eig(cmat)

#Step 9: Fing weights of input image


w_in = eigenvectors_in.T * np.asmatrix(shifted_in)
w_in = np.asarray(w_in)

#Step 10: Euclidean distance


df = np.asarray(w - w_in) # the difference between the images
dst = np.sqrt(np.sum(df**2, axis=1)) # their euclidean distances
idx = np.argmin(dst) # index of the smallest value in 'dst' which should be equal to index of the most
simillar image in 'images'
print idx
T

You might also like