jpeg2im.m

function x = jpeg2im(y) 
%IM2JPEG Compresses an image using a JPEG approximation.
%   Y = IM2JPEG(X, QUALITY) compresses image X based on 8 x 8 DCT
%   transforms, coefficient quantization, and Huffman symbol
%   coding. Input QUALITY determines the amount of information that
%   is lost and compression achieved.  Y is an encoding structure
%   containing fields: 
%
%      Y.size      Size of X
%      Y.numblocks Number of 8-by-8 encoded blocks
%      Y.quality   Quality factor (as percent)
%      Y.huffman   Huffman encoding structure, as returned by
%                  MAT2HUFF
%
%   See also JPEG2IM.

%   Copyright 2002-2006 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
%   Digital Image Processing Using MATLAB, Prentice-Hall, 2004
%   $Revision: 1.5 $  $Date: 2006/07/15 20:44:34 $
%
%   Revised: 3/20/06 by R.Woods to correct an 'eob' coding problem, to
%   check the 'quality' input for <= 0, and to fix a warning when
%   struct y is created.

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


m = [16 11  10  16  24  40  51  61        % JPEG normalizing array
     12  12  14  19  26  58  60  55       % and zig-zag redordering
     14  13  16  24  40  57  69  56       % pattern.
     14  17  22  29  51  87  80  62
     18  22  37  56  68  109 103 77
     24  35  55  64  81  104 113 92
     49  64  78  87  103 121 120 101
     72  92  95  98  112 100 103 99];

order = [1 9  2  3  10 17 25 18 11 4  5  12 19 26 33  ...
        41 34 27 20 13 6  7  14 21 28 35 42 49 57 50  ...
        43 36 29 22 15 8  16 23 30 37 44 51 58 59 52  ...
        45 38 31 24 32 39 46 53 60 61 54 47 40 48 55  ...
        62 63 56 64];

rev=order;
for k=1:length(order)
    rev(k)=find(order==k);
end
m=double(y.quality)/100*m;
xb=double(y.numblocks);
sz=double(y.size);
xn=sz(2);
xm=sz(1);
x=huff2mat(y.huffman);
eob = max(x(:));               % Create end-of-block symbol

z = zeros(64,xb) ;k=1;

for j = 1:xb                       % Process 1 block (col) at a time
 for i=1:64
     if x(k)==eob
         k=k+1;
     end
 end
end
z=z(rev,:);
x=col2im(z,[8 8],[xm xn],'distinct');
x=blkproc(x,[8 8],'x.*P1',m);
t=dctmtx(8);
x=dlkproc(x,[8 8],'P1* x *P2',t',t);
x=x+double(2^(y.bits-1));
if y.bits<=8
    x=uint8(x);
else
    x=uint16(x);
end


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我理解了您的要求。下面是一个简单的 MATLAB 图像压缩代码,它可以将 JPEG 格式的图像进行压缩,并显示原图像和压缩后的图像,同时展示压缩比例。 ```matlab % 读入 JPEG 图像 im = imread('image.jpg'); % 显示原图像 figure(1); imshow(im); title('原图像'); % 将图像转换为 YCbCr 颜色空间,只对 Y 分量进行压缩 im_ycbcr = rgb2ycbcr(im); im_y = im_ycbcr(:,:,1); % 对 Y 分量进行离散余弦变换(DCT) im_dct = dct2(im_y); % 设定压缩比例 compression_ratio = 0.5; % 计算需要保留的 DCT 系数数量 dct_coeffs = numel(im_dct); num_coeffs = round(dct_coeffs * compression_ratio); % 保留最大的 DCT 系数 dct_flat = reshape(im_dct, [], 1); [~, idx] = sort(abs(dct_flat), 'descend'); dct_thresh = dct_flat(idx(num_coeffs)); im_dct(abs(im_dct) < dct_thresh) = 0; % 对 Y 分量进行反离散余弦变换(IDCT) im_y_compress = idct2(im_dct); % 将压缩后的 Y 分量与原图像的 CbCr 分量组合成压缩后的图像 im_compress_ycbcr = im_ycbcr; im_compress_ycbcr(:,:,1) = im_y_compress; im_compress = ycbcr2rgb(im_compress_ycbcr); % 显示压缩后的图像 figure(2); imshow(im_compress); title('压缩后的图像'); % 计算压缩比例并显示 num_pixels = size(im, 1) * size(im, 2); num_coeffs_compress = sum(abs(im_dct(:)) > 0); compression_ratio_actual = num_coeffs_compress / dct_coeffs; fprintf('压缩比例:%f\n', compression_ratio_actual); ``` 请将代码保存为 `compress_image.m` 并将 `image.jpg` 替换为您要压缩的 JPEG 图像。压缩比例可以通过修改 `compression_ratio` 变量来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值