SVD decomposition on color image?
35 views (last 30 days)
Show older comments
I am trying to preform SVD Decomposition on a color image. I have attached my code. The main goal is to preform SVD on a color image, and apply various rank approximations. The issue is when i change the rank to k=55, I get a greyscale image and not color. How could I fix this to get a color image when I change the rank?
% Read jpg file
A = imread('woodchuck.jpg');
% Convert to A to double
A = im2double(A);
% Color Channels
R = A(:, :, 1);
G = A(:, :, 2);
B = A(:, :, 3);
% Perform svd
[UR,SR,VR] = svd(R);
[UG,SG,VG] = svd(G);
[UB,SB,VB] = svd(B);
DR = UR*SR*VR';
DG = UG*SG*VG';
DB = UB*SB*VB';
%Create new matrix
% Plot rank 5 approximation, rank 10 approximation, rank 50, and true image
figure('Position',[50 50 800 800])
subplot(2,2,1)
M=zeros(480,640,3);
M(:,:,1) = DR;
M(:,:,2) = DG;
M(:,:,3) = DB;
imshow(M); title ('Original Image')
subplot(2,2,2)
k = 55;
Mk = UR(:,1:k)*SR(1:k, 1:k)* VR(:,1:k)';
Mk = UG(:,1:k)*SG(1:k, 1:k)* VG(:,1:k)';
Mk = UB(:,1:k)*SB(1:k, 1:k)* VB(:,1:k)';
imshow(Mk); title('Rank 55')
2 Comments
Christine Tobler
on 16 Feb 2021
The lines
Mk = UR(:,1:k)*SR(1:k, 1:k)* VR(:,1:k)';
Mk = UG(:,1:k)*SG(1:k, 1:k)* VG(:,1:k)';
Mk = UB(:,1:k)*SB(1:k, 1:k)* VB(:,1:k)';
each overwrite Mk with the next channel, so you're only seeing a grayscale image with the last channel (blue). Use Mk(:, :, 1) = ..., Mk(:, :, 2) = ..., Mk(:, :, 3) = ... instead.
Answers (0)
See Also
Categories
Find more on Eigenvalues in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!