matlab imratio,imratio.m

function cr = imratio(f1, f2)

%IMRATIO Computes the ratio of the bytes in two images/variables.

% CR = IMRATIO(F1, F2) returns the ratio of the number of bytes in

% variables/files F1 and F2. If F1 and F2 are an original and

% compressed image, respectively, CR is the compression ratio.

% Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins

% Digital Image Processing Using MATLAB, Prentice-Hall, 2004

% $Revision: 1.4 $ $Date: 2003/11/21 13:11:15 $

error(nargchk(2, 2, nargin)); % Check input arguments

cr = bytes(f1) / bytes(f2); % Compute the ratio

%-------------------------------------------------------------------%

function b = bytes(f)

% Return the number of bytes in input f. If f is a string, assume

% that it is an image filename; if not, it is an image variable.

if ischar(f)

info = dir(f); b = info.bytes;

elseif isstruct(f)

% MATLAB's whos function reports an extra 124 bytes of memory

% per structure field because of the way MATLAB stores

% structures in memory. Don't count this extra memory; instead,

% add up the memory associated with each field.

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

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是对图像 `peppers.jpg` 进行霍夫曼编码和 JPEG 图像压缩的代码: ```matlab % 读入图像 I = imread('peppers.jpg'); % 霍夫曼编码 R1 = whos('I'); % 记录原图像的大小 c = huffman(hist(double(I(:)),256)); h3I = mat2huff(I); % 编码 whos('h3I') % 显示编码后的大小 hcode = h3I.code; R2 = whos('hcode'); % 记录编码后的大小 bincode = dec2bin(double(hcode)); % 转换为二进制 ratio1 = R1.bytes / R2.bytes; % 计算压缩比例 % JPEG 图像压缩 c2 = im2jpeg(I); % 采用默认的量化表进行压缩 I2 = jpeg2im(c2); % 解压缩 whos('c2') % 显示压缩后的大小 ratio2 = R1.bytes / whos('c2').bytes; % 计算压缩比例 % 显示原图像和压缩后的图像 figure; subplot(1,2,1); imshow(I); title('原图像'); subplot(1,2,2); imshow(I2); title('JPEG 压缩后的图像'); % 显示压缩前后的 MSE、PSNR 和 SSIM fprintf('MSE = %f\n', immse(I, I2)); fprintf('PSNR = %f dB\n', psnr(I, I2)); fprintf('SSIM = %f\n', ssim(I, I2)); ``` 在这个代码中,我们首先使用 `imread` 函数读入了图像 `peppers.jpg`,然后对该图像进行了霍夫曼编码和 JPEG 图像压缩。 在霍夫曼编码中,我们使用了 `huffman` 函数计算出图像的直方图,并使用 `mat2huff` 函数将图像编码成霍夫曼码。然后,我们使用 `dec2bin` 函数将霍夫曼码转换成二进制,并计算了编码前后的大小以及压缩比例。 在 JPEG 图像压缩中,我们使用了 `im2jpeg` 函数对图像进行压缩,并使用 `jpeg2im` 函数对压缩后的数据进行解压缩。然后,我们计算了压缩前后的 MSE、PSNR 和 SSIM,以评价压缩效果。 希望这个答案对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值