PostProcessing.m 3.95 KB
clear all
sequence = ["CrowdRun_768x768_50_8bit_444","DucksTakeOff_768x768_50_8bit_444", "OldTownCross_768x768_50_8bit_444","ParkJoy_768x768_50_8bit_444", "ArenaOfValor_768x768_60_8bit_444","GlassHalf_768x768_24_8bit_444"];

resolution = {'2K','4K'};
QP = {'qp22', 'qp27', 'qp32', 'qp37'};
seq = sequence{1};
qp = QP{1};

Input_File = "dec_"+seq+"_"+qp+".rgb";
Output_8bit = "dec_"+seq+"_"+qp+"_GBR8.rgb";
Output_RGB = "dec_"+seq+"_"+qp+"_RGB8.rgb";
Input_File_Origin="C:\Users\user\Desktop\HM-16.8\input\\"+seq+".rgb";

BPP_path="C:\Users\user\Desktop\HM-16.8\output\encoder\\"+seq+"_"+qp+".log";

numFrame = 3;
channel=3;
width = 768;
height = 768;

%------------- setting ------------- 

finID = fopen(Input_File,'r');
fileSize = width * height * channel * numFrame;
%Data10 = uint16(zeros(fileSize,1));
Data10 = fread(finID,[fileSize,1],'uint16=>uint16'); % From the 10 bit(2byte) input file, read it by 2bytes(uint16) 
% whos Data10
fclose(finID);
Data8 = uint8((Data10+2) ./ 4);
% whos Data8
foutID = fopen(Output_8bit,'w');                   % Save the 8bit(1byte) variable Data8 to the output file
fwrite(foutID, Data8);
fclose(foutID);
%------------- 10bit to 8bit ------------- 

fileID = fopen(Output_8bit,'r');
fileID_ori = fopen(Input_File_Origin,'r');
fout = fopen(Output_RGB,'w');

total_R_PSNR = 0;
total_G_PSNR = 0;
total_B_PSNR = 0;

for frame = 1:numFrame
    G = uint8(zeros(height,width));
    B = uint8(zeros(height,width));
    R = uint8(zeros(height,width));
    
    R_Original = uint8(zeros(height,width));
    G_Original =uint8(zeros(height,width));
    B_Original = uint8(zeros(height,width));
    
    for j = 1:height
      G(:,j) = fread(fileID,width); 
    end
    for j = 1:height
      B(:,j) = fread(fileID,width); 
    end
    for j = 1:height
      R(:,j) = fread(fileID,width); 
    end
    
    for j = 1:height
      R_Original(:,j) = fread(fileID_ori,width); 
    end
    for j = 1:height
      G_Original(:,j) = fread(fileID_ori,width); 
    end
    for j = 1:height
      B_Original(:,j) = fread(fileID_ori,width); 
    end
    
    R_Original=im2double(R_Original);
    G_Original=im2double(G_Original);
    B_Original=im2double(B_Original);
    R_Test=im2double(R);
    G_Test=im2double(G);
    B_Test=im2double(B);
    
    R_ssimval = ssim(R_Test,R_Original);
    G_ssimval = ssim(G_Test,G_Original);
    B_ssimval = ssim(B_Test,B_Original);

    %R channel PSNR
    R_sse = 0; % sum of squared error
    for i = 1:height
        for j = 1:width
            d = abs(R_Original(i,j) - R_Test(i,j));
            R_sse = R_sse + (d^2);
        end
    end
    R_mse = R_sse / (height*width);  % mean square error
    A=max(max(R_Original(:)), max( R_Test(:)));
%     R_psnr = 20 * log10(255) - 10 * log10(R_mse);  % PSNR
    R_psnr = - 10 * log10(R_mse);  % PSNR
    total_R_PSNR = total_R_PSNR + R_psnr;

    %G channel PSNR
    G_sse = 0; % sum of squared error
    for i = 1:height
        for j = 1:width
            d = abs(G_Original(i,j) - G_Test(i,j));
            G_sse = G_sse + (d^2);
        end
    end
    G_mse = G_sse / (height*width);  % mean square error
    G_psnr =  - 10 * log10(G_mse);  % PSNR
    total_G_PSNR = total_G_PSNR + G_psnr;

    %B channel PSNR
    B_sse = 0; % sum of squared error
    for i = 1:height
        for j = 1:width
            d = abs(B_Original(i,j) - B_Test(i,j));
            B_sse = B_sse + (d^2);
        end
    end
    B_mse = B_sse / (height*width);  % mean square error
    B_psnr = - 10 * log10(B_mse);  % PSNR
    total_B_PSNR = total_B_PSNR + B_psnr;
    
    fwrite(fout, R);
    fwrite(fout, G);
    fwrite(fout, B);

end
fclose(fileID);
fclose(fileID_ori);
fclose(fout);

m_R_PSNR = total_R_PSNR / numFrame;
m_G_PSNR = total_G_PSNR / numFrame;
m_B_PSNR = total_B_PSNR / numFrame;
total = (m_R_PSNR + m_G_PSNR + m_B_PSNR) / 3;
total_ssim=(R_ssimval+G_ssimval+B_ssimval)/3;
fprintf("PSNR: \n%f\n\n", total);
fprintf("SSIM: \n%f\n\n", total_ssim); 

BPP=get_BPP(BPP_path, width, height, channel, numFrame);
fprintf("BPP: \n%f\n\n", BPP);