影像壓縮編碼碼matlab實現——常用引數計算

fpga&matlab發表於2020-12-24

function PSNR = psnr(f1, f2)
%計算兩幅影像的峰值訊雜比

k = 8;     %k為影像中表示一個畫素點所用的二進位制位數,即位深。
fmax = 2.^k - 1;
a = fmax.^2;
e = double(f1) - double(f2);
[m, n] = size(e);
b = sum(e(:).^2);
PSNR = 10*log(m*n*a/b);

 

function b = bytes(f)
%函式bytes返回輸入f佔用的位元數
%if ischar(f)
%    info = dir(f);
%    b = info.bytes;
%elseif isstruct(f)
%    b = 0;
%    fields = fieldnames(f);
%    for k = 1:length(fields)
%        b = b + bytes(f.(fields{k}));
%    end
%else
    info = whos('f');
    b = info.bytes;
%end

function SHAN = entropy(x, n)
% 計算熵,n是影像 x的灰度級,如果 n是預設則 n = 256
error(nargchk(1, 2, nargin));
if nargin<2
    n = 256;
end
x = double(x);
xh = hist(x(:), n);
xh = xh/sum(xh(:));
i = find(xh);
SHAN = -sum(xh(i).*log2(xh(i)));

 

function ERMS = erms(f1, f2)
%計算f1和f2均方根誤差

e = double(f1) - double(f2);
[m, n] = size(e);
ERMS = sqrt(sum(e(:).^2)/(m*n));


function f = frequency(vector)
%函式frequency計算各符號出現的概率

if ~isa(vector, 'uint8')
    error('input argument must be a uint8 vector');
end
f = repmat(0, 1, 256);
len = length(vector);
for index = 0:255
    f(index+1) = sum(vector == uint8(index));
end
f = f./len;   %歸一化

 

function cr = imageratio(f1, f2)
%計算兩幅影像壓縮比
error(nargchk(2, 2, nargin));
cr = bytes(f1)/bytes(f2);

相關文章