声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2328|回复: 0

[共享资源] 卡洛变换的MATLAB程序(Karhunen-Loeve)

[复制链接]
发表于 2006-11-13 10:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
卡洛变换的MATLAB程序,运用于图像处理和统计信号处理

  1. % Karhunen-Loeve, for face recognition
  2. % By Alex Chirokov, Alex.Chirokov@gmail.com
  3. clear all;

  4. % Load the ATT image set
  5. k = 0;
  6. for i=1:1:40
  7.     for j=1:1:10
  8.         filename  = sprintf('C:\\MATLAB\\att_faces\\s%d\\%d.pgm',i,j);
  9.         image_data = imread(filename);
  10.         k = k + 1;
  11.         x(:,k) = image_data(:);
  12.         anot_name(k,:) = sprintf('%2d:%2d',i,j);   % for plot annotations
  13.      end;
  14. end;
  15. nImages = k;                     %total number of images
  16. imsize = size(image_data);       %size of image (they all should have the same size)
  17. nPixels = imsize(1)*imsize(2);   %number of pixels in image
  18. x = double(x)/255;               %convert to double and normalize
  19. %Calculate the average
  20. avrgx = mean(x')';
  21. for i=1:1:nImages
  22.     x(:,i) = x(:,i) - avrgx; % substruct the average
  23. end;
  24. subplot(2,2,1); imshow(reshape(avrgx, imsize)); title('mean face')
  25. %compute covariance matrix
  26. cov_mat = x'*x;
  27. [V,D] = eig(cov_mat);         %eigen values of cov matrix
  28. V = x*V*(abs(D))^-0.5;               
  29. subplot(2,2,2); imshow(ScaleImage(reshape(V(:,nImages  ),imsize))); title('1st eigen face');
  30. subplot(2,2,3); imshow(ScaleImage(reshape(V(:,nImages-1),imsize))); title('2st eigen face');
  31. subplot(2,2,4); plot(diag(D)); title('Eigen values');

  32. %image decomposition coefficients
  33. KLCoef =  x'*V;

  34. %reconstruction of Image
  35. %KLCoef(:,1:1:1)= 0;  % filtration
  36. image_index = 12;  %index of face to be reconstructed
  37. reconst = V*KLCoef';
  38. diff = abs(reconst(:,image_index) - x(:,image_index));
  39. strdiff_sum = sprintf('delta per pixel: %e',sum(sum(diff))/nPixels);
  40. figure;
  41. subplot(2,2,1); imshow((reshape(avrgx+reconst(:,image_index), imsize))); title('Reconstructed');
  42. subplot(2,2,2); imshow((reshape(avrgx+x(:,image_index), imsize)));title('original');
  43. subplot(2,2,3); imshow(ScaleImage(reshape(diff, imsize))); title(strdiff_sum);

  44. for i=1:1:nImages
  45.     dist(i) = sqrt(dot(KLCoef(1,:)-KLCoef(i,:), KLCoef(1,:)-KLCoef(i,:))); %euclidean
  46. end;
  47. subplot(2,2,4); plot(dist,'.-'); title('euclidean distance from the first face');

  48. %2D plot of first 2 decomposition coefficients, with annotatons
  49. % annotations have format Face:Extression, i.e 5:6 means image was taken
  50. % from s5/6.pgm expression 6 of the person in the set s5.
  51. figure;
  52. show_faces = 1:1:nImages/2;
  53. plot(KLCoef(show_faces,nImages), KLCoef(show_faces,nImages-1),'.'); title('Desomposition: Numbers indicate (Face:Expression)');
  54. for i=show_faces
  55.     name = anot_name(i,:);
  56.     text(KLCoef(i,nImages), KLCoef(i,nImages-1),name,'FontSize',8);
  57. end;

  58. % Find similar faces,  variable 'image_index' defines face used in comparison
  59. image_index = 78;
  60. for i=1:1:nImages
  61.     dist_comp(i) = sqrt(dot(KLCoef(image_index,:)-KLCoef(i,:), KLCoef(image_index,:)-KLCoef(i,:))); %euclidean
  62.     strDist(i) = cellstr(sprintf('%2.2f\n',dist_comp(i)));
  63. end;
  64. [sorted, sorted_index] = sort(dist_comp); % sort distances
  65. figure; % open new figure
  66. for i=1:1:9
  67.     subplot(3,3,i); imshow((reshape(avrgx+x(:,sorted_index(i)), imsize))); title(strDist(sorted_index(i)));
  68. end;
复制代码
回复
分享到:

使用道具 举报

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-14 06:50 , Processed in 0.057703 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表