Contents

close all;  clear all;
%apo9hkeuw ola ta onomata gia na diatreksw ta arxeia
names = {'2_21_s', '2_22_s', '3_24_s', '4_13_s', '5_14_s',...
    '5_26_s', '7_3_s', '7_25_s', '9_10_s',  '11_24_s', '12_6_s',...
    '12_33_s', '14_18_s',  '15_2_s', '16_25_s', '18_20_s'};
err_gif=0; err_png=0; err_jpg=0; err_jpg2000=0;

for NN = 1:length(names)

Kleinw ta prohgoumena figures kai fortwnw thn eikona poy 9elw na epeksergastw

    close all;
    name = names{NN};
    filename = ['../MSRC/' name '.bmp'];
    im = imread(filename);
    imshow(im);
    numm = numel(im);

Xwrizw se channels sto RGB kai sto YCbCr

    [R, G, B] = deal(im(:, :, 1), im(:, :, 2), im(:, :, 3));

    YCBCR = rgb2ycbcr(im);
    [Y, Cb, Cr] = deal(YCBCR(:, :, 1), YCBCR(:, :, 2), YCBCR(:, :, 3));

Ypologizw entropia gia RGB kai YCbCr gia kathe xrwmatikh sunistwsa

    entRGB = [ entropy(R), entropy(G), entropy(B)];
    entYCbCr = [entropy(Y), entropy(Cb), entropy(Cr)];
    entr = [entRGB; entYCbCr];

png kai gif me paletes 64 kai 256 xrwmatwn

    [IND64,map64] = rgb2ind(im,64);
    [IND256,map256] = rgb2ind(im,256);

    figure('Name','Indexed image with 64 Colors');
    imagesc(IND64);  colormap(map64);
    title('Indexed image with 64 Colors');
    figure('Name','Indexed image with 256 Colors');
    imagesc(IND256);  colormap(map256);
    title('Indexed image with 256 Colors');

    %apo9hkeuw tis eikones kai tis ksanafortwnw gia na vrw to signal:error
    %ratio
    imwrite(IND64, map64, ['results/' name '_gif_64.gif']);
    [cur1, map1] = imread(['results/' name '_gif_64.gif']);
    imwrite(IND256, map256, ['results/' name '_gif_256.gif']);
    [cur2, map2] = imread(['results/' name '_gif_256.gif']);
    imwrite(IND64, map64, ['results/' name '_png_64.png']);
    [cur3, map3] = imread(['results/' name '_png_64.png']);
    imwrite(IND256, map256, ['results/' name '_png_256.png']);
    [cur4, map4] = imread(['results/' name '_png_256.png']);

%tis metatrepw apo indexed se rgb gia na sugkrinw me thn arxikh
    cur1 = im2uint8(ind2rgb(cur1, map1));
    cur2 = im2uint8(ind2rgb(cur2, map2));
    cur3 = im2uint8(ind2rgb(cur3, map3));
    cur4 = im2uint8(ind2rgb(cur4, map4));

    s1 = dir(['results/' name '_gif_64.gif']);
    s2 = dir(['results/' name '_gif_256.gif']);
    s3 = dir(['results/' name '_png_64.png']);
    s4 = dir(['results/' name '_png_256.png']);

    %vriskw to Compress ratio gia thn ka9e eikona gia na mporesw na tis
    %parasthsw sto idio plot me ta jpeg/2000
    cratiogif = [numm / s1.bytes, numm / s2.bytes];
    cratiopng = [numm / s3.bytes, numm / s4.bytes];

    errorpng = [sig2err(im, cur1), sig2err(im, cur2)];
    errorgif = [sig2err(im, cur3), sig2err(im, cur4)];

    figure(104);
    plot(cratiopng, errorpng, 'b*-'); hold on;
    plot(cratiogif, errorgif, 'ro-');
    title(['Signal to Error ratio για png και gif(εικόνα: ' name '.bmp)'], 'Interpreter', 'none');
    legend('Png', 'Gif');
    xlabel('Compression ratio'); ylabel('Signal to Error ratio');

JPEG2000 kai JPEG Compression

    [arxi, step, telos]=deal(10, 5, 50);
    iters = (telos-arxi)/step;
    entropiesjpg = zeros(2, 3, iters+1);
    entropiesjpg2000 = zeros(2, 3, iters+1);
    error1 = zeros(1, iters+1);  error2 = zeros(1, iters+1);
    cratio = zeros(1, 2*iters+2);

    for i=1:iters+1
        rt = 10+(i-1)*step;  q  = 80-(i-1)*70/iters;
% prosarmwzw to quality gia ka9e eikona, gia na einai to compress ratio tou jpeg
% konta sto compress ratio tou jpeg2000
        if NN==16
            q = 90-(i-1)*78/iters;
        elseif NN==14
            q = 95-(i-1)*70/iters;
        elseif NN==13
            q  = 65-(i-1)*58/iters;
        elseif NN==12
            q  = 90-(i-1)*70/iters;
        elseif NN==11
            q  = 98 -(i-1)*18/iters;
        elseif NN==10
            q  = 86-(i-1)*72/iters;
        elseif NN==8
            q = 75-(i-1)*66/iters;
        end

        imwrite(im,['results/' name '_jpeg2000_ratio_' int2str(rt) '.j2k'],'CompressionRatio',rt);
        imwrite(im,['results/' name '_jpeg_ratio_' int2str(rt) '.jpg'], 'Quality', q);

        cur1 = imread(['results/' name '_jpeg2000_ratio_' int2str(rt) '.j2k']);
        cur2 = imread(['results/' name '_jpeg_ratio_' int2str(rt) '.jpg']);
        s1 = dir(['results/' name '_jpeg2000_ratio_' int2str(rt) '.j2k']);
        s2 = dir(['results/' name '_jpeg_ratio_' int2str(rt) '.jpg']);
        cratio(2*i-1) = numm/s1.bytes; cratio(2*i) = numm/s2.bytes;
        figure('Name', 'Jpeg2000'); imshow(cur1);
        title(['Jpeg2000 (Compress Ratio: '...
            int2str(cratio(2*i-1)) ')']);
        figure('Name', 'Jpeg'); imshow(cur2);
        title(['Jpeg (Compress Ratio: '...
            int2str(cratio(2*i)) ')']);

        [R, G, B] = deal(cur1(:, :, 1), cur1(:, :, 2), cur1(:, :, 3));
        YCBCR = rgb2ycbcr(cur1);Y = YCBCR(:, :, 1);
        Cb = YCBCR(:, :, 2);   Cr = YCBCR(:, :, 3);

        entropiesjpg2000(:, :, i) = [
        entropy(R), entropy(G), entropy(B);
         entropy(Y), entropy(Cb), entropy(Cr)];

        [R, G, B] = deal(cur2(:, :, 1), cur2(:, :, 2), cur2(:, :, 3));
        YCBCR = rgb2ycbcr(cur2);Y = YCBCR(:, :, 1);
        Cb = YCBCR(:, :, 2);   Cr = YCBCR(:, :, 3);

        entropiesjpg(:, :, i) = [
         entropy(R), entropy(G), entropy(B);
         entropy(Y), entropy(Cb), entropy(Cr)];

         error1(i) = sig2err(im, cur1);
         error2(i) = sig2err(im, cur2);

    end

    err_png=err_png + sum(errorpng);
    err_gif=err_gif + sum(errorgif);
    err_jpg2000=err_jpg2000 + sum(error1);
    err_jpg=err_jpg + sum(error2);

    figure(101);plot(cratio(1:2:end), error1, 'LineWidth', 1.5);
    hold on; plot(cratio(2:2:end), error2, 'LineWidth', 1.5);
    title(['Signal to Error ratio (εικόνα: ' name '.bmp)'],  'Interpreter', 'none');
    plot(cratiogif, errorgif, 'LineWidth', 1);
    plot(cratiopng, errorpng, 'LineWidth', 1);
    legend('Jpeg2000', 'Jpeg', 'Gif', 'Png');
    xlabel('Compression ratio'); ylabel('Signal to Error ratio');

    figure(102); title(['entropies RGB (εικόνα: ' name '.bmp)'], 'Interpreter', 'none'); hold on;
    line([cratio(1) cratio(end)],  [entr(1, 1) entr(1, 1)], 'Color','red','LineStyle','-', 'LineWidth', 1.5);
    line([cratio(1) cratio(end)],  [entr(1, 2) entr(1, 2)], 'Color','green','LineStyle','-', 'LineWidth', 1.5);
    line([cratio(1) cratio(end)],  [entr(1, 3) entr(1, 3)], 'Color','blue','LineStyle','-', 'LineWidth', 1.5);
    plot(cratio(1:2:end),  squeeze(entropiesjpg2000(1, 1, :)), 'r--', 'LineWidth', 1.5);
    plot(cratio(1:2:end),  squeeze(entropiesjpg2000(1, 2, :)), 'g--', 'LineWidth', 1.5);
    plot(cratio(1:2:end),  squeeze(entropiesjpg2000(1, 3, :)), 'b--', 'LineWidth', 1.5);
    plot(cratio(2:2:end),  squeeze(entropiesjpg(1, 1, :)), 'r:', 'LineWidth', 1.5);
    plot(cratio(2:2:end),  squeeze(entropiesjpg(1, 2, :)), 'g:', 'LineWidth', 1.5);
    plot(cratio(2:2:end),  squeeze(entropiesjpg(1, 3, :)), 'b:', 'LineWidth', 1.5);
    legend('R channel (Initial)', 'G channel (Initial)', 'B channel (Initial)', ...
        'R channel (Jpeg2000)', 'G channel (Jpeg2000)', 'B channel (Jpeg2000)', ...
        'R channel (Jpeg)', 'G channel (Jpeg)', 'B channel (Jpeg)', 'Location', 'Best');
    xlabel('Compression ratio'); ylabel('Entropy');

    figure(103); title(['entropies YCbCr (εικόνα: ' name '.bmp)'], 'Interpreter', 'none'); hold on;
    line([cratio(1) cratio(end)],  [entr(2, 1) entr(2, 1)], 'Color','k','LineStyle','-', 'LineWidth', 1.5);
    line([cratio(1) cratio(end)],  [entr(2, 2) entr(2, 2)], 'Color','blue','LineStyle','-', 'LineWidth', 1.5);
    line([cratio(1) cratio(end)],  [entr(2, 3) entr(2, 3)], 'Color','red','LineStyle','-', 'LineWidth', 1.5);
    plot(cratio(1:2:end),  squeeze(entropiesjpg2000(2, 1, :)), 'k--', 'LineWidth', 1.5);
    plot(cratio(1:2:end),  squeeze(entropiesjpg2000(2, 2, :)), 'b--', 'LineWidth', 1.5);
    plot(cratio(1:2:end),  squeeze(entropiesjpg2000(2, 3, :)), 'r--', 'LineWidth', 1.5);
    plot(cratio(2:2:end),  squeeze(entropiesjpg(2, 1, :)), 'k:', 'LineWidth', 1.5);
    plot(cratio(2:2:end),  squeeze(entropiesjpg(2, 2, :)), 'b:', 'LineWidth', 1.5);
    plot(cratio(2:2:end),  squeeze(entropiesjpg(2, 3, :)), 'r:', 'LineWidth', 1.5);

    legend('Y channel (Initial)', 'Cb channel (Initial)','Cr channel(Initial)',...
        'Y channel (Jpeg2000)', 'Cb channel (Jpeg2000)','Cr channel(Jpeg2000)',...
        'Y channel (Jpeg)', 'Cb channel (Jpeg)', 'Cr channel(Jpeg)', 'Location', 'Best');
    xlabel('Compression ratio'); ylabel('Entropy');

    saveas(figure(101), ['results/' name 'fig1.png']);
    saveas(figure(102), ['results/' name 'fig2.png']);
    saveas(figure(103), ['results/' name 'fig3.png']);
    saveas(figure(104), ['results/' name 'fig4.png']);
end

fprintf('Συνολικό signal to error ratio από την συμπίεση σε PNG: %fdB\n', err_png);
fprintf('Συνολικό signal to error ratio από την συμπίεση σε GIF: %fdB\n', err_gif);
fprintf('Συνολικό signal to error ratio από την συμπίεση σε JPEG: %fdB\n', err_jpg);
fprintf('Συνολικό signal to error ratio από την συμπίεση σε JPEG2000: %fdB\n', err_jpg2000);

Functions

function [ratio] = sig2err(orig, comp)
% err   = tetragwnikh riza mesou tetragwnikou sfalmatos
% ratio = metrietai se dB
    erim = (orig-comp).^2;
    err = sqrt(mean(erim(:)));
    ratio = 20*log10(255/err);
end
Συνολικό signal to error ratio από την συμπίεση σε PNG: 1248.456833dB
Συνολικό signal to error ratio από την συμπίεση σε GIF: 1248.456833dB
Συνολικό signal to error ratio από την συμπίεση σε JPEG: 4948.596497dB
Συνολικό signal to error ratio από την συμπίεση σε JPEG2000: 5125.201181dB