我想为图像更正此代码,需要做哪些更改..?

时间:2014-03-17 14:25:20

标签: image matlab image-processing

目前我正在认识一张脸,意味着我必须找到一张我们必须测试的面孔是否在训练数据库中。所以,我必须决定是或否..

是表示查找图像,并且没有表示没有图像数据库的打印消息。我有一个程序,目前这个程序正确找到一个正确的图像,但即使没有图像,甚至它显示其他不匹配的图像..实际上它应该打印数据库中没有图像。

那么,怎么做..?

以下是此链接上的测试和训练图像数据。

http://www.fileconvoy.com/dfl.php?id=g6e59fe8105a6e6389994740914b7b2fc99eb3e445

我的程序是根据不同的四个.m文件而来的,在这里,我们只需运行第一个代码..剩下的3个是函数,这里也给出了它.. **

 clear all

 clc

 close all

TrainDatabasePath = uigetdir('D:\Program Files\MATLAB\R2006a\work', 'Select training database path' );

TestDatabasePath = uigetdir('D:\Program Files\MATLAB\R2006a\work', 'Select test database path');

prompt = {'Enter test image name (a number between 1 to 10):'};

dlg_title = 'Input of PCA-Based Face Recognition System';

num_lines= 1;

def = {'1'};

TestImage = inputdlg(prompt,dlg_title,num_lines,def);

TestImage = strcat(TestDatabasePath,'\',char(TestImage),'.jpg');

im = imread(TestImage);

T = CreateDatabase(TrainDatabasePath);

[m, A, Eigenfaces] = EigenfaceCore(T);

OutputName = Recognition(TestImage, m, A, Eigenfaces);

SelectedImage = strcat(TrainDatabasePath,'\',OutputName);

SelectedImage = imread(SelectedImage);

imshow(im)

title('Test Image');

figure,imshow(SelectedImage);

title('Equivalent Image');

str = strcat('Matched image is : ',OutputName);

disp(str)

function T = CreateDatabase(TrainDatabasePath)

TrainFiles = dir(TrainDatabasePath);

Train_Number = 0;

for i = 1:size(TrainFiles,1)

if

not(strcmp(TrainFiles(i).name,'.')|strcmp(TrainFiles(i).name,'..')|strcmp(TrainFiles(i).name,'Thu mbs.db'))

Train_Number = Train_Number + 1; % Number of all images in the training database

end

end

T = [];

for i = 1 : Train_Number

    str = int2str(i);
    str = strcat('\',str,'.jpg');
    str = strcat(TrainDatabasePath,str);
    img = imread(str);
    img = rgb2gray(img);
    [irow icol] = size(img);
    temp = reshape(img',irow*icol,1);   % Reshaping 2D images into 1D image vectors
    T = [T temp]; % 'T' grows after each turn                    
end

function [m, A, Eigenfaces] = EigenfaceCore(T)

m = mean(T,2); % Computing the average face image m = (1/P)*sum(Tj's) (j = 1 : P)

Train_Number = size(T,2);

A = [];

for i = 1 : Train_Number

    temp = double(T(:,i)) - m; 

 Ai = Ti - m

    A = [A temp]; % Merging all centered images

end

L = A'*A; % L is the surrogate of covariance matrix C=A*A'.

[V D] = eig(L); % Diagonal elements of D are the eigenvalues for both L=A'*A and C=A*A'.

L_eig_vec = [];

for i = 1 : size(V,2)

    if( D(i,i)>1 )
        L_eig_vec = [L_eig_vec V(:,i)];
    end
end

Eigenfaces = A * L_eig_vec; % A: centered image vectors

function OutputName = Recognition(TestImage, m, A, Eigenfaces)

ProjectedImages = [];

Train_Number = size(Eigenfaces,2);

for i = 1 : Train_Number

    temp = Eigenfaces'*A(:,i); % Projection of centered images into facespace
    ProjectedImages = [ProjectedImages temp]; 
end

InputImage = imread(TestImage);

temp = InputImage(:,:,1);

[irow icol] = size(temp);

InImage = reshape(temp',irow*icol,1);

Difference = double(InImage)-m; % Centered test image

ProjectedTestImage = Eigenfaces'*Difference; % Test image feature vector

Euc_dist = [];

for i = 1 : Train_Number

    q = ProjectedImages(:,i);
    temp = ( norm( ProjectedTestImage - q ) )^2;
    Euc_dist = [Euc_dist temp];
end

[Euc_dist_min , Recognized_index] = min(Euc_dist);

OutputName = strcat(int2str(Recognized_index),'.jpg');

那么,当没有图像匹配时如何生成错误massege?

1 个答案:

答案 0 :(得分:1)

目前,您的应用程序似乎找到了最相似的图像(您在测量相似度时似乎使用了欧几里德距离),并将其返回。似乎没有任何关于图像是否匹配"的概念。或不。

定义相似度阈值,然后确定最相似的图像是否符合该阈值。如果是,请将其返回,否则显示错误消息。