Problem with creating .bmp images

8 views (last 30 days)
Wenlong
Wenlong on 21 Sep 2012
Hi, all
I'm running PCA on ORL human face image database. What I want to achieve it to extract the principal components, then add the mean vector back to these component. By writing these components as .bmp images, I can get eigen faces. Here is my code
clc
clear all
close all
path = 'D:\\Matlab 7.0\\work\\ORL\\';
data = zeros(400, 10304);
for i = 1:400
name = [path 'face' num2str(i) '.bmp'];
temp = imread(name);
temp = reshape(temp, 1, 10304);
data(i, :) = temp;
end
meanface = mean(data, 1);
data = data - repmat(meanface, 400, 1);
intermat = (data * data') / (400-1);
[intervec, val] = eig(intermat);
pc = data'*intervec;
pc = pc';
for i = 1:400
pc(i,:) = pc(i,:) / sqrt(dot(pc(i,:), pc(i,:)));
sd(i) = sqrt(val(i,i));
eigenvalue(i) = val(i,i);
end
pc = flipud(pc);
sd = fliplr(sd);
eigenvalue = fliplr(eigenvalue);
%%%%%%Write images %%%%%%%
for i = 1:399
mode = pc(i,:) + meanface;
mode = round(mode);
mode = reshape(mode, 112, 92);
name = ['pc' num2str(i) '.bmp'];
imwrite(mode, name, 'bmp');
end
I was expecting to see ghost-like images. However, this piece of code writes images that all white.
May I know if there is anything wrong with my code?
Thank you very much for your kindly help.
Best regards Wenlong

Answers (1)

Image Analyst
Image Analyst on 21 Sep 2012
I don't believe BMP supports floating point. If you want floating point, save as .mat. If you want a standard BMP, then cast to uint8 before saving, though beware that you may lose numerical resolution as it will truncate the values and may require scaling and shifting into the 0-255 value range.

Categories

Find more on Dimensionality Reduction and Feature Extraction in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!