MATLAB Codes of JPEG Image Compression for Gray-level Image
来源:互联网 发布:linux执行文件权限不够 编辑:程序博客网 时间:2024/05/16 23:55
Matlab Codes:
function JPEG_Compressor (filename,quant_multiple)% JPEG image compression program for graylevel image only% This program compresses a graylevel image using the JPEG algorithm% Parameter:% -- filename, e.g. filename = 'D:\tigerGraylevel.jpg';% -- quant_multiple, i.e.,set the multiplier to change size of quant. levels,and vary quant_mult from 0.1 to 3 (see jpeg.results file)%%%{ [X, map] = imread(...) reads the indexedimage in filename into X andits associated colormap into map. % Colormap valuesin the image file are automatically rescaled into the range [0,1]. }orig_img = imread(filename); % image matrix was stored in variable X % image colormap is stored in variable map orig_img = double(orig_img); %Convert image to double precision which is needed for the following calculationsblocksize = 8; % set the size of chunks to take the DCT of ( =< 8)DCT_quantizer = ... % levels for quantizing the DCT block (8x8 matrix)[ 16 11 10 16 24 40 51 61; ... 12 12 14 19 26 58 60 55; ... 14 13 16 24 40 57 69 56; ... 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 ];[rows, cols] = size(orig_img); % finds image's rows and columnscolors = max(max(orig_img)); % find the maximum intensity value in the original image% Introductionstr = char( ... 'CpE 645 Image Processin Final Project Changjiang Cai', ... 'This program implements the JPEG data compression algorithm.');ssdisp(0, str);%{fprintf(fileID,formatSpec,A1,...,An) appliesthe formatSpec to all elements of arrays A1,...An,in column order, and writes the data to a text file. fprintf uses the encoding scheme specified in the call to fopen%}fprintf (1, 'Image used: %s\n', filename);fprintf (1, 'Its size is: %dx%d\n', rows, cols);fprintf (1, 'Number of colors (guess): %d\n', colors);fprintf (1, 'DCT block size: %dx%d\n', blocksize, blocksize);fprintf (1, 'Quant. size multiplier: %d\n', quant_multiple);% Display the original imagefigureax(1) = subplot(1,2,1);image(orig_img)title('Original image')% Prepare image for transform% Level-shift the image (center intensity values around 0)% {B = ceil(A) rounds the elements of A to the nearest integers greater thanor equal to A. % For complex A,the imaginary and real parts are rounded independently.}orig_img = orig_img - ceil(colors/2);% Replicate edges of image to make its dimensions a multiple of blocksize% {B = floor(A) rounds the elements of A to the nearest integers less thanor equal to A. % For complex A,the imaginary and real parts are rounded independently.}% the number of coloums to be paddedpad_cols = (1 - (cols/blocksize - floor(cols/blocksize))) * blocksize; % the above formula may produce this result: when cols = 8, blocksize = 8,then pad_cols = 8% thus, if this case happens, we have to make pad_cols be 0, compulsively. if pad_cols == blocksize, pad_cols = 0; end% the number of rows to be paddedpad_rows = (1 - (rows/blocksize - floor(rows/blocksize))) * blocksize;if pad_rows == blocksize, pad_rows = 0; end% duplicate the last column for padding new columnsfor extra_cols = 1:pad_cols orig_img(1:rows, cols+extra_cols) = orig_img(1:rows, cols);endcols = cols + pad_cols; % orig_img is now pad_cols wider% duplicate the last row for padding new rowsfor extra_rows = 1:pad_rows orig_img(rows+extra_rows, 1:cols) = orig_img(rows, 1:cols);endrows = rows + pad_rows; % orig_img is now pad_rows taller% the 1-D DCT transform matrixDCT_trans = dctmtx(blocksize);% Take DCT of blocks of size blocksizefprintf(1, '\nFinding the DCT and quantizing...\n');starttime = cputime; % "cputime" is an internal cpu time counterjpeg_img = orig_img - orig_img; % zero the matrix for the compressed imagefor row = 1: blocksize: rows-blocksize+1 for col = 1: blocksize: cols-blocksize+1 % take a block of the image: DCT_matrix = orig_img(row: row + blocksize-1, col: col + blocksize-1); % perform the transform operation on the 2-D block DCT_matrix = DCT_trans*DCT_matrix*DCT_trans'; % quantize it (levels stored in DCT_quantizer matrix): DCT_matrix = floor (DCT_matrix ... ./ (DCT_quantizer(1:blocksize, 1:blocksize) * quant_multiple) + 0.5); % place it into the compressed-image matrix: jpeg_img(row: row + blocksize-1, col: col + blocksize-1) = DCT_matrix; endendfprintf(1, ' CPU time used: %1.3f\n', (cputime - starttime))% Reverse the process (take the Inverse DCT)fprintf(1, 'Reconstructing quantized values and taking the inverse DCT...\n');starttime = cputime;recon_img = orig_img - orig_img; % zero the matrix for the reconstructed imagefor row = 1: blocksize: rows-blocksize+1 for col = 1: blocksize: cols-blocksize+1 % take a block of the image: IDCT_matrix = jpeg_img(row: row + blocksize-1, col: col + blocksize-1); % reconstruct the quantized values: IDCT_matrix = IDCT_matrix ... .* (DCT_quantizer(1:blocksize, 1:blocksize) * quant_multiple); % perform the inverse DCT: IDCT_matrix = DCT_trans' * IDCT_matrix * DCT_trans; % place it into the reconstructed image: recon_img(row: row + blocksize-1, col: col + blocksize-1) = IDCT_matrix; endendfprintf(1, ' CPU time used: %1.3f\n', (cputime - starttime))% Restore image to normal% Level-shift the image backrecon_img = recon_img + ceil(colors/2);orig_img = orig_img + ceil(colors/2);% Clip off padded rows and columnsrows = rows - pad_rows;cols = cols - pad_cols;orig_img = orig_img(1:rows, 1:cols);recon_img = recon_img(1:rows, 1:cols);% Display imageax(2) = subplot(1,2,2);image(recon_img)title('Decompressed JPEG image')linkaxes(ax,'xy')axis(ax,'image')% Calculate the root-mean-square errorfprintf(1, 'Calculate the root-mean-square error...\n');error = orig_img -recon_img;e = reshape(error,1,[]);e_rms = sqrt((e*e')/64);fprintf(1, '\nThe root-mean-square error (RMSE) is: %1.3f\n\n', e_rms);% Calculate signal-to-noise ratiofprintf(1, 'Finding the signal-to-noise ratio...\n');starttime = cputime;PSNR = 0; % Peak-Signal-to-Noise Ratio of the reconstructed imagefor row = 1:rows for col = 1:cols PSNR = PSNR + (orig_img(row, col) - recon_img(row, col)) ^ 2; endendPSNR = 10 * log10 ((255^2) / (1 / (((rows + cols) / 2) ^ 2) * PSNR)); % (averaged rows and cols together)fprintf(1, ' CPU time used: %1.3f\n', (cputime - starttime))fprintf(1, '\nThe signal-to-noise ratio (PSNR) is: %1.3f dB\n\n', PSNR); end
0 0
- MATLAB Codes of JPEG Image Compression for Gray-level Image
- Study of JPEG Image Compression 图像压缩
- image forensics_double jpeg _the first day_jpeg compression
- deep learning for image compression
- Deep Learning of Binary Hash Codes for Fast Image Retrieval
- Deep Learning of Binary Hash Codes for Fast Image Retrieval
- Deep Learning of Binary Hash Codes for Fast Image Retrieval_CVPR2015
- image jpeg
- The source of image UBB codes creator
- 图像检索系列一:Deep Learning of Binary Hash Codes for Fast Image Retrieval
- Deep Learning of Binary Hash Codes for Fast Image Retrieval 代码编译
- 图像检索系列一:Deep Learning of Binary Hash Codes for Fast Image Retrieval
- 图像检索系统《Deep Learning of Binary Hash Codes for Fast Image Retrieval》
- 快速图像检索(Deep Learning of Binary Hash Codes for Fast Image Retrieval)
- Deep Learning of Binary Hash Codes for Fast Image Retrieval (CVPRW15)
- sdut 1961 Image Compression
- Image Compression Artifacts
- compression image in ROS
- JSF教程(10)——生命周期之Update Model Values Phase
- 韩国泛泰手机全系列L/K/S三版通刷自动识别手机版本脚本及使用说明以及其他
- Study of JPEG Image Compression 图像压缩
- SSRAM、SDRAM和Flash简要介绍
- 项目经理注意事项(3)---宏观把控
- MATLAB Codes of JPEG Image Compression for Gray-level Image
- 独特区间
- Android_低版本使用ActionBar
- Mac回收Trash(废纸篓)空间问题
- 今日作息及食谱(5.31)
- U盘上安装多系统 (三)
- 2014年5月31日-6月6日(共16小时,剩4469小时)
- nyoj 265 积木 【左右两边加中间】
- gcc编译器编译c++程序