DWT Image compression basics
This page of MATLAB source code covers DWT image compression. It explains basic steps for DWT based image compression matlab source code.
Before we move to image compression basics let us go through lossless and lossy data compression techniques. This is needed as in order to compress the image, initially input image of size 256x256 pixels is converted to raw data information.
Input image for DWT based compression
There are four basic steps for image compression and image restoration as outlined below.
STEP-1. Applying the transform (Haar or Daubechies-6 wavelet)
STEP-2. Choosing a soft threshold or hard threshold
STEP-3. Compression methods-Run Length Coding referred as RLE or DPCM(Differential Pulse Code Modulation)
STEP-4. Applying the Inverse Transform to recover the compressed image
DWT image compression MATLAB Code
clear all;
% CLOSE ALL closes all the open figure windows.
close all;
X=imread('lenna256x256.gif');
X=X(1:256,1:256);
figure;
%load woman;
%subplot(2,1,1);
imshow(uint8(X));
% STEP-1: wavelet transform (rowwise) ...haar wavelet...traditional approach...
f=X;
[m n]=size(X);
k=n/2;
%k=n;
for i=1:1:m
for j=1:1:k
f(i,j) =X(i,2*j)+X(1,2*j-1);
f(i,j+k) = X(i,2*j) - X(i,2*j-1);
end
end
X = f;
k=m/2; %wavelet transform column wise
%k=m;
for j=1:1:n
for i=1:1:k
X(i,j) = uint8((f(2*i,j)+f(2*i-1,j))/2);
X(i+k,j)= uint8((f(2*i,j)-f(2*i-1,j))/2);
end
end
Y=X;
figure; imshow(Y(1:128,1:128));
Image compression output after step-1
%%STEP:2 Threshold part for further image compression
threshold=70;
[m n]=size(Y);
for i=1:m
for j=1:n
if(Y(i,j)Y(i,j) = 0;
else
%Y(i,j)=Y(i,j); % HARD THRESHOLD
Y(i,j)=sign(Y(i,j))*(abs(Y(i,j))-threshold);% SOFT THRESHOLD
end
end
end
Y1=Y;
figure; imshow(Y1(1:128,1:128));