2

mail@pastecode.io avatar
unknown
matlab
2 years ago
2.6 kB
1
Indexable
Never
image = imread('pato.jpg');

image_red=image(:,:,1);
double_red = double(image_red);
[U_red,S_red,V_red] = svd(double_red);
singular_values_red = diag(S_red);
sum_total_singular_values_red_squares = sum(singular_values_red.^2);

image_green=image(:,:,2);
double_green = double(image_green);
[U_green,S_green,V_green] = svd(double_green);
singular_values_green = diag(S_green);
sum_total_singular_values_green_squares = sum(singular_values_green.^2);

image_blue=image(:,:,3);
double_blue = double(image_blue);
[U_blue,S_blue,V_blue] = svd(double_blue);
singular_values_blue = diag(S_blue);
sum_total_singular_values_blue_squares = sum(singular_values_blue.^2);

singular_values_quantity = length(singular_values_red);

preserved_quantities = [25 50 75];


for n = 1 : length(preserved_quantities)
    preserved_quantity = preserved_quantities(n);
    singular_values_length = round(singular_values_quantity*(preserved_quantity/100));

    [U_2_red,S_2_red,V_2_red] = svds(double_red,singular_values_length);
    singular_values = diag(S_2_red);
    sum_singular_values_squares = sum(singular_values.^2);
    preserved_red_quality = 100*(sum_singular_values_squares/sum_total_singular_values_red_squares);
  
    [U_2_green,S_2_green,V_2_green] = svds(double_green,singular_values_length);
    singular_values = diag(S_2_green);
    sum_singular_values_squares = sum(singular_values.^2);
    preserved_green_quality = 100*(sum_singular_values_squares/sum_total_singular_values_green_squares);

    [U_2_blue,S_2_blue,V_2_blue] = svds(double_blue,singular_values_length);
    singular_values = diag(S_2_blue);
    sum_singular_values_squares = sum(singular_values.^2);
    preserved_blue_quality = 100*(sum_singular_values_squares/sum_total_singular_values_blue_squares);
    
    
    %Apresentação das imagens com redução dos valores singulares
    A_2_red = U_2_red*S_2_red*V_2_red';
    A_2_green = U_2_green*S_2_green*V_2_green';
    A_2_blue = U_2_blue*S_2_blue*V_2_blue';

    image_2_red = uint8(A_2_red);
    image_2_green = uint8(A_2_green);
    image_2_blue = uint8(A_2_blue);
    new_image(:,:,1)=image_2_red;
    new_image(:,:,2)=image_2_green;
    new_image(:,:,3)=image_2_blue;
     
    if(n == 1)
        image_25 = new_image;
    elseif (n==2)
        image_50 = new_image;
    else
        image_75 = new_image;
    end
end

subplot(2,2,1), imshow(image)
title('Imagem original') 
subplot(2,2,2), imshow(image_25)
title('25%') 
subplot(2,2,3), imshow(image_50)
title('50%') 
subplot(2,2,4), imshow(image_75)
title('75%')