女人为什么会得痔疮:图像的SVD分解 - 读书.生活.新知 - 歪酷博客 Ycool Blog

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 01:16:41
feelfree @ 2007-04-01 20:47
% doSvd: Apply Singular Value Decomposition for an matrix data as an
%  feature extraction method. 
%  [S] = doSvd(I, R) returns the approximated vector of
%  singular values S (of USV) of the matrix I. The # of coefficients is
%  reduced into R percent (such as 5). 
%
%  See also svd
% http://www.mathworks.com/access/helpdesk/help/techdoc/ref/svd.html
%
% Author        : Naotoshi Seo
% First Edition : April, 2005
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
function [S] = doSvd(I, R)
% Testing
% I = imread('lena.png');
I = double(I);
if nargin < 2
     R = 100;
end
 
S = svd(I);
DIM = floor(length(S)*(R*0.01));
S = S(1:DIM);
end

function doSvdTest(percent)                                    I = imread('lena.png');                                    [m n c] = size(I);                                    I = double(I);                                    R = I(:, :, 1);                                    G = I(:, :, 2);                                    B = I(:, :, 3);                                    [RU, RS, RV] = svd(R);                                    % TIPS: svd(R) returns diag(RS) because it is the most important                                    [GU, GS, GV] = svd(G);                                    [BU, BS, BV] = svd(B);                                    RdS = diag(RS);                                    GdS = diag(GS);                                    BdS = diag(BS);                                    D = length(RdS);                                    DIM = floor(D*(percent*0.01));                                    RdS(DIM+1:end) = 0; % reduce dimensions                                    GdS(DIM+1:end) = 0;                                    BdS(DIM+1:end) = 0;                                    RSR = sparse(1:D, 1:D, RdS, m, n); % reconstruct into matrix                                    GSR = sparse(1:D, 1:D, GdS, m, n);                                    BSR = sparse(1:D, 1:D, BdS, m, n);                                    RR = RU * RSR * RV'; % recover                                    GR = GU * GSR * GV';                                    BR = BU * BSR * BV';                                    IR = cat(3, RR, GR, BR);                                    IR = uint8(IR);                                    imshow(IR);                                    imwrite(IR, sprintf('lenasvd%02d.png', percent), 'PNG');                                    end
ps: 这是一个日本人写的。很好!其实感觉日本人很务实的,他们不像美国人那样在对理论很执着,他们提出的很多算法,
工程性很强,比如著名的OTSU阈值分割算法。