Tutor HuntResources Physics Resources

Image Processing Using Matlab

Wavelet Transform based multi-resolution analysis to image compression and decompression

Date : 28/09/2014

Author Information

Dhananjay

Uploaded by : Dhananjay
Uploaded on : 28/09/2014
Subject : Physics

Problem 1 The selected test image had three samples. First of all, the mean of all tree samples was taken to represent the image as a two dimensional array. Further, the array values were converted to type 'double'. The difference for two images were calculated and a normalized histogram plot was made. Laplacian and Gaussian curves with same mean and standard deviation as the original data was also plotted. MATLAB code The MATLAB code has been shown below: close all; clear all; clc; A = imread(`image1.jpg`); B = imread(`image2.jpg`); B(end,:,:) = []; A = (((A(:,:,1))/3)+((A(:,:,2))/3)+((A(:,:,3))/3)); B = (((B(:,:,1))/3)+((B(:,:,2))/3)+((B(:,:,3))/3)); D = (double(A)-double(B)); D = reshape(D,224*225,1); M = sum(sum(D))/(225*224); S = std(reshape(D,224*225,1)); b = S/(sqrt(2)); figure; [xo,x] = histnorm(D,20,`plot`); hold plot(x,(1/(2*b))*exp((-abs(x-M))/b),`r`); plot(x,normpdf(x,M,S),`g`) hold xlabel(`Difference between pixel`) ylabel(`normalized count`) legend(`data`,`laplacian`,`normal`)

The results have been presented below:

It may be seen that the Gaussian curve represents a better descri ption of the histogram. Problem 2 The two test images selected have been shown below: Figure2: Test image1. Figure 3: Test image 2 The difference of two dimensional arrays representing the two test images was calculated. A thresholding for removing and most and least significant bits was performed by kepping only those pixels which were between (100,200) interval. MATLAB code The MATLAB code has been shown below: close all; clc; clear all; A = imread(`image1.jpg`); B = imread(`image2.jpg`); B(end,:,:) = []; D = (A)-(B); imwrite(D,`diffe.tiff`); imshow(D) D((D<100 | D>200))=0; imwrite(D,`diffrem.tiff`); figure; imshow(D)

The image after taking the difference has been shown below: Figure 4: difference of images The image after thresholding has been represented below Figure 5: image in figure 4 after thresholding Problem 3 Block truncation coding algorithm was implemented for a chosen test image. The test image was resized to a two-dimensional array and the array size was made to a multiple of the block size for ease of computation. MATLAB code The MATLAB code has been shown below: close all; clear all; clc; R = im2double(imread(`t4.jpg`)); %imfinfo(`t4.jpg`) R = (((R(:,:,1))/3)+((R(:,:,2))/3)+((R(:,:,3))/3)); K = input(`Enter blocksize: `); [n,m] = size(R); k=1; r1 = mod(n,K); c1 = mod(m,K); if n > m X = R(1:m-c1,:); elseif n < m X = R(:,1:n-r1); elseif n == m X = R(1:n-c1,1:m-r1); end subplot(131),imshow(X),title(`ORIGINAL`); dlmwrite(`original.txt`,X,`delimiter`,` `); f0 = dir(`original.txt`); size0 = f0.bytes; bppORIGINAL = size0/((n-r1)*(m-c1)) %tic for i=1:K:n-r1 for j=1:K:m-c1 tmp(1:K,1:K)=X(i:i+(K-1),j:j+(K-1)); mn = mean(reshape(tmp,K*K,1)); tmp1(i:i+(K-1),j:j+(K-1))=tmp>mn; q(k) =(length(find(tmp > mn))); sig = std(reshape(tmp,K*K,1)); a(k) = mn - sig*sqrt(K*K/(K*K-q(k))); b(k) = mn + sig*(1/sqrt(K*K/(K*K-q(k)))); k = k + 1; end end %toc subplot(132),imshow(tmp1);title(`ENCODED`); dlmwrite(`encoded.txt`,tmp1,`delimiter`,` `) f = dir(`encoded.txt`); size = f.bytes; bppENCODED = size/((n-r1)*(m-c1)) %imfinfo(`encoded.jpg`) MSE = (1/(n-r1)*(m-c1))*sum(sum((tmp1-X).^2)) PSNR = 10*log10(255*255/MSE) k1 = 1; %tic for i=1:K:n-r1 for j=1:K:m-c1 tmp21(1:K,1:K)=tmp1(i:i+(K-1),j:j+(K-1)); tmp22=((tmp21==1)*round(b(k1))); tmp23=((tmp21==0)*round(a(k1))); tmp21=tmp23+tmp22; out_put(i:i+(K-1),j:j+(K-1))=tmp21; k1 = k1 + 1; end end %toc subplot(133),imshow(out_put);title(`DECODED`); dlmwrite(`decoded.txt`,out_put,`delimiter`,` `); f1 = dir(`decoded.txt`); size1 = f1.bytes; bppDECODED = size1/((n-r1)*(m-c1))

The results have been shown below: Figure 6: original, encoded and decoded images using BTC algorithm. The bit rate for original image was found to be 7.8283. The bit rate for the encoded image was found to be 2 which is consistent with the fact that it is a pre black and white image. The bit rate for decoded image was found to be 2.0077. The peak signal to noise ratio was 7.6446. Problem 4 The discrete cosine function for a dimensional array was coded using the standard formula for the same. The test cases were successfully reproduced as given in the cheat sheet. MATLAB code The MATLAB code has been shown below: %function y = DcT(A) close all; clear all; clc; A = [235.0623,188.2428,44.9479,103.4551] N = length(A); y = zeros(1,length(A)); r = sqrt(2*ones(1,length(A))/N); r(1) = 1/sqrt(N); for i = 1:length(A) for k = 1:length(A) C = cos((pi/N)*(k-1+.5)*(i-1)); y(i) = y(i) + r(i)*A(k)*C; end end y z = zeros(1,N); for i = 1:length(A) for k = 1:length(A) IC = cos((pi/N)*(i-.5)*(k-1)); z(i) = z(i) + r(k)*y(k)*IC; end end z

Figure 7: A is the input matrix, y is matrix after DCT application and z is the matrix after inverse DCT has been applied to y. Problem 5 Using standard relations, the DCT for a two dimensional array was coded and test results were successfully reproduced. MATLAB code The MATLAB code has been shown below: function [y,z] = p5(A) n1 = size(A); n2 = length(A); %%2 Dimensional DCT %A = [113.49 215.78 213.72 212.10; 237.61 133.91 5 128.21; 118.82 51.67 173.72 180.91; 106.75 171.39 96.76 109.36] N = 4; y = zeros(n1); r = (2*ones(n1)/N); r(1,:) = sqrt(2)/N; r(:,1) = sqrt(2)/N; r(1,1) = 1/N; %r(1) = 1/sqrt(N); for i1 = 1:n2 for k1 = 1:n2 for i = 1:n2 C1 = cos((pi/N)*(i-.5)*(i1-1)); for k = 1:n2 C = cos((pi/N)*(k-.5)*(k1-1)); y(i1,k1) = y(i1,k1) + r(i1,k1)*A(i,k)*C*C1; end end end end %y %%Inverse 2 Dimensional DCT z = zeros(n1); for i = 1:n2 for k = 1:n2 for i1 = 1:n2 IC1 = cos((pi/N)*(i-.5)*(i1-1)); for k1 = 1:n2 IC = cos((pi/N)*(k-.5)*(k1-1)); z(i,k) = z(i,k) + r(i1,k1)*y(i1,k1)*IC*IC1; end end end end %z end

Figure 8: output for one of the test cases. Problem 6 An 8x8 DCT was performed on a test image. In DCT most of the energy gets compacted in few frequencies. First six coefficients taken in zonal scan order were selected in each 8x8 block resulting probability density functions were plotted. MATLAB code The MATLAB code has been shown below: close all; clear all; clc; count = 1; A = im2double(imread(`t4.jpg`)); A = (((A(:,:,1))/3)+((A(:,:,2))/3)+((A(:,:,3))/3)); [r,c] = size(A); r1 = mod(r,8); c1 = mod(c,8); if r > c B = A(1:c-c1,:); elseif r < c B = A(:,1:r-r1); elseif r == c B = A; end n = 8; [cc,rr] = meshgrid(0:7); D = sqrt(2 / n) * cos(pi * (2*cc + 1) .* rr / (2 * n)); D(1,:) = D(1,:) / sqrt(2); %D = dctmtx(size(B,1)); d1 = zeros(size(B)); for j = 1:8:length(B)-8 for i = 1:8:length(B)-8 d1(i:i+7,j:j+7) = D*B(i:i+7,j:j+7)*D`; a1(count) = d1(i,j); a2(count) = d1(i+1,j); a3(count) = d1(i,j+1); a4(count) = d1(i+1,j+1); a5(count) = d1(i+2,j); a6(count) = d1(i,j+2); count = count + 1; end end figure, imshow(d1); [s1 s2] = size(a1); a = [a1;a2;a3;a4;a5;a6]; %%plot a1 for i = 1:6 M = mean(a(i,:)); S = std(a(i,:)); b = S/(sqrt(2)); h = figure; [xo,x] = histnorm(a(i,:),50,`plot`); hold plot(x,(1/(2*b))*exp((-abs(x-M))/b),`r`); plot(x,normpdf(x,M,S),`g`) hold xlabel(`pixel range`) ylabel(`normalized count`) legend(`data`,`laplacian`,`normal`) print(h, `-djpeg`, sprintf(`%s%d%s`,`a`,i,`.jpg`)) end

Figure 9: histogram plots for first six coefficients taken in zonal scan order. Gaussian and Laplacian curves with same mean and standard deviation as the original data have also been plotted. It may be noted that laplacian curve is a better approximation to the histograms plotted here. Problem7 Based on the results obtained in problems above it may be concluded that keeping only the most significant coefficients is a better method for bit allocation. While the bit rate of image after DCT is higher, the reconstructed image after inverse DCT is of much better quality as compared to what was obtained for BTC algorithm where coefficients were quantized using fewer bits. The latter may be seen to compress the image it a smaller size as the bit rate in the transformed image is always 2. However, the image reconstruction does not yield a very high quality image. MATLAB code The MATLAB codes for problem 3 with blocksize 8 was used with another MATLAB code to carry out the DCT with keeping only the most significant coefficients. The code has been shown below: close all; clear all; clc; count = 1; A = im2double(imread(`t4.jpg`)); A = (((A(:,:,1))/3)+((A(:,:,2))/3)+((A(:,:,3))/3)); [r,c] = size(A); r1 = mod(r,8); c1 = mod(c,8); if r > c B = A(1:c-c1,:); elseif r < c B = A(:,1:r-r1); elseif r == c B = A(1:c-c1,1:r-r1); end n = 8; [cc,rr] = meshgrid(0:7); D = sqrt(2 / n) * cos(pi * (2*cc + 1) .* rr / (2 * n)); D(1,:) = D(1,:) / sqrt(2); %D = dctmtx(size(B,1)); d1 = zeros(size(B)); for j = 1:8:length(B)-8 for i = 1:8:length(B)-8 d1(i:i+7,j:j+7) = D*B(i:i+7,j:j+7)*D`; id(i:i+7,j:j+7) = D`*d1(i:i+7,j:j+7)*D; count = count + 1; end end figure, imshow(d1); dlmwrite(`dtc.txt`,d1,`delimiter`,` `) f0 = dir(`dtc.txt`); s0 = f0.bytes; bpp0 = s0/((r-r1)*(c-c1)) figure, imshow(`t4.jpg`); %dlmwrite(`b4_dct.txt`,B,`delimiter`,` `) %f1 = dir(`b4_dct.txt`); %s1 = f1.bytes; %bpp1 = s1/((r-r1)*(c-c1)) figure, imshow(id); dlmwrite(`idct.txt`,id,`delimiter`,` `) f2 = dir(`idct.txt`); s2 = f2.bytes; bpp2 = s2/((r-r1)*(c-c1)) MSE = (1/(r-r1)*(c-c1))*sum(sum((d1-B).^2)); maxB = -1+2^(8); PSNR_dct = 10*log10(((maxB)^2)/MSE)

The peak signal to noise ratio in case of keeping only the most significant coefficients is a better method for bit allocation was found to be 4.72 for a particular image. The PSNR value using the BTC algorithm where coefficients were quantized using fewer bits was found to be 7.6446 with the same blockade.

This resource was uploaded by: Dhananjay

Other articles by this author