% get input matrix size N = size(in,1);
% build the matrix n = 0:N-1; for k = 0:N-1 if (k>0)
C(k+1,n+1) = cos(pi*(2*n+1)*k/2/N)/sqrt(N)*sqrt(2); else
C(k+1,n+1) = cos(pi*(2*n+1)*k/2/N)/sqrt(N); end end
out = (C')*in*C; %
---------------------------------------------------------------------------------
% plot_bases - use the inverse DCT in 2 dimensions to plot the base pictures %
% Note: we can get resolution be zero pading of the input matrix !!! % that is by calling: in = zeros(base_size*resolution) % where: resolution is an integer > 1 % So I will use zero pading for resolution (same as in the fourier theory) % instead of linear interpolation. %
---------------------------------------------------------------------------------
function plot_bases( base_size,resolution,plot_type )
figure;
for k = 1:base_size for l = 1:base_size
in = zeros(base_size*resolution); in(k,l) = 1; % \for the \(k,l)\ subplot( base_size,base_size,(k-1)*base_size+l ); switch lower(plot_type)
case 'surf3d', surf( pdip_inv_dct2( in ) ); case 'mesh3d', mesh( pdip_inv_dct2( in ) );
case 'mesh2d', mesh( pdip_inv_dct2( in ) ); view(0,90); case 'gray2d', imshow( 256*pdip_inv_dct2( in ) ); end axis off; end end
% add a title to the figure
subplot(base_size,base_size,round(base_size/2));
h = title( 'Bases of the DCT transform (section 1.3)' ); set( h,'FontWeight','bold' ); %
---------------------------------------------------------------------------------
% image_8x8_block_dct - perform a block DCT for an image %
---------------------------------------------------------------------------------
function transform_image = image_8x8_block_dct( input_image )
transform_image = zeros( size( input_image,1 ),size( input_image,2 ) ); for m = 0:15 for n = 0:15
transform_image( m*8+[1:8],n*8+[1:8] ) = ...
pdip_dct2( input_image( m*8+[1:8],n*8+[1:8] ) ); end end %
---------------------------------------------------------------------------------
% image_8x8_block_inv_dct - perform a block inverse DCT for an image %
---------------------------------------------------------------------------------
function restored_image = image_8x8_block_inv_dct( transform_image )
restored_image =
zeros( size( transform_image,1 ),size( transform_image,2 ) ); for m = 0:15 for n = 0:15
restored_image( m*8+[1:8],n*8+[1:8] ) = ...
pdip_inv_dct2( transform_image( m*8+[1:8],n*8+[1:8] ) ); end end %
---------------------------------------------------------------------------------
% calc_snr - calculates the snr of a figure being compressed %
% assumption: SNR calculation is done in the following manner: % the deviation from the original image is considered % to be the noise therefore: %
% noise = original_image - compressed_image %
% the SNR is defined as: %
% SNR = energy_of_image/energy_of_noise %
% which yields: %
% SNR =
energy_of_image/((original_image-compressed_image)^2) %
---------------------------------------------------------------------------------
function SNR = calc_snr( original_image,noisy_image )
original_image_energy = sum( original_image(:).^2 );
noise_energy = sum( (original_image(:)-noisy_image(:)).^2 ); SNR = original_image_energy/noise_energy;
以下是1-9号原图像,放到matlab的.m文件目录里,重命名9个图像名为1、2、3、4、5、6、7、8、9