%PSNR
function psnr_val = calcular_psnr(img_orig, img_rec)
    % Convertir las matrices uint8 a double para evitar desbordamiento aritmético
    img_orig = double(img_orig);
    img_rec = double(img_rec);
    
    % Calcular el Error Cuadrático Medio (MSE)
    MSE = mean((img_orig(:) - img_rec(:)).^2);
    
    if MSE == 0
        % Si el MSE es cero, las imágenes son idénticas
        psnr_val = Inf;
    else
        MAX_I = 255; % Valor máximo para una imagen de 8 bits
        psnr_val = 10 * log10((MAX_I^2) / MSE);
    end
end

%SSIM
function mssim_val = calcular_ssim(img_orig, img_rec)
    % Convertir las matrices uint8 a double
    img_orig = double(img_orig);
    img_rec = double(img_rec);
    
    % Parámetros definidos en el artículo
    K1 = 0.01;
    K2 = 0.03;
    L = 255; 
    C1 = (K1*L)^2;
    C2 = (K2*L)^2;
    
    % Ventana de ponderación gaussiana 11x11 con sigma 1.5
    % Requiere el paquete 'image'
    window = fspecial('gaussian', 11, 1.5);
    window = window / sum(window(:));
    
    % Cálculos locales mediante convolución bidimensional
    mu1 = filter2(window, img_orig, 'valid');
    mu2 = filter2(window, img_rec, 'valid');
    
    mu1_sq = mu1.^2;
    mu2_sq = mu2.^2;
    mu1_mu2 = mu1.*mu2;
    
    sigma1_sq = filter2(window, img_orig.^2, 'valid') - mu1_sq;
    sigma2_sq = filter2(window, img_rec.^2, 'valid') - mu2_sq;
    sigma12 = filter2(window, img_orig.*img_rec, 'valid') - mu1_mu2;
    
    % Calcular mapa SSIM
    ssim_map = ((2*mu1_mu2 + C1).*(2*sigma12 + C2)) ./ ...
               ((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2));
               
    % Retornar el SSIM medio (MSSIM) sobre toda la imagen
    mssim_val = mean(ssim_map(:));
end

%magnitud de compresión
function mag_comp = calcular_magnitud_compresion(img_orig, unidades, header)
    % Se obtiene la información de metadatos de las variables locales
    info_orig = whos('img_orig');
    info_unidades = whos('unidades');
    info_header = whos('header');
    
    % Extracción del tamaño real en memoria (bytes)
    bytes_orig = info_orig.bytes;
    bytes_unidades = info_unidades.bytes;
    bytes_header = info_header.bytes;
    
    % Cálculo de la magnitud
    mag_comp = (bytes_unidades + bytes_header) / bytes_orig;
end

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: