0% found this document useful (0 votes)
51 views13 pages

Program 2A: All All 'Cameraman - Tif' 'Original Image' 'Gray' 'No of Levels of Quantization '

The document contains 3 MATLAB programs (2A, 2B, 2C) that perform image quantization on a test image. Program 2A quantizes the image to different levels and calculates the mean squared error (MSE) and peak signal-to-noise ratio (PSNR). Program 2B performs gamma correction before quantization. Program 2C adds random noise before quantization and removes it after to obtain the quantized image. It runs for different noise levels.

Uploaded by

Jatin Joshua
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views13 pages

Program 2A: All All 'Cameraman - Tif' 'Original Image' 'Gray' 'No of Levels of Quantization '

The document contains 3 MATLAB programs (2A, 2B, 2C) that perform image quantization on a test image. Program 2A quantizes the image to different levels and calculates the mean squared error (MSE) and peak signal-to-noise ratio (PSNR). Program 2B performs gamma correction before quantization. Program 2C adds random noise before quantization and removes it after to obtain the quantized image. It runs for different noise levels.

Uploaded by

Jatin Joshua
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

PROGRAM 2A

clear all;
close all;
clc;
Img =imread('cameraman.tif');
imagesc(Img,[0,255]);
title('Original image');
colormap('gray');
L=input('No of levels of quantization ');
Imgd=double(Img);
for i=1:L+1
t(i)=256*(i-1)/l;
end;
for j=1:L
r(j)=t(j)+(128/l);
end;
S=size(Img);
for R=1:S(1)
for C=1:S(2)
for k=1:L
if t(k)<=Imgd(R,C)
if Imgd(R,C)<=t(k+1)
Img1(R,C)=r(k);
else Img1(R,C)=Imgd(R,C);
end;
end;
end;
end;
end;
figure;
subplot(2,1,1);
imagesc(Img,[0,255]);
title('Original image');
colormap('gray');
subplot(2,1,2);
imagesc(Img1,[0,255]);
title(['The quantized image to ',int2str(L),' levels']);
colormap('gray');
Img1d=double(Img1);
M=0;
for l=1:S(1)
for m=1:S(2)
M1=(Imgd(l,m)-Img1d(l,m))*(Imgd(l,m)-Img1d(l,m));
M=M+M1;
end;
end;
MSE=((1/S(1))^2)*M
PSNR=10*log10((255^2)/MSE)
PROGRAM 2B

Img=imread('cameraman.tif');
L=input('No of levels of Quantization ');
Imgd=double(Img);
mx=max(max(Imgd));
mxd=double(mx);
S=size(Imgd);
for R=1:S(1)
for C=1:S(2)
u(R,C)=Imgd(R,C)/mxd;
end;
end;
al=1;
b=1/3;
for R=1:S(1)
for C=1:S(2)
c(R,C)=al*(u(R,C)^b);
end;
end;
c=c*mxd;

for i=1:L+1
t(i)= max(max(Imgd))*(i-1)/L;
end;
for j=1:L
r(j)=t(j)+(((max(max(Imgd))+min(min(Imgd)))/2)/L);
end;
for R=1:S(1)
for C=1:S(2)
for k=1:L
if t(k)<=c(R,C)
if c(R,C)<=t(k+1)
Imgc(R,C)=r(k);
else Imgc(R,C)=c(R,C);
end;
end;
end;
end;
end;
for R=1:S(1)
for C=1:S(2)
u1(R,C)=(Imgc(R,C)/mxd)^3;
end;
end;
u2=mxd*u1;
figure;
subplot(2,1,1);
imagesc(u2,[0,255]);
title(['The quantized image to ',int2str(L),' levels']);
colormap('gray');
subplot(2,1,2);
imagesc(Img,[0,255]);
title('Original image');
colormap('gray');
u2d=double(u2);
Imgd=double(Img);
M=0;
for i=1:S(1)
for j=1:S(2)
M1=(Imgd(i,j)-u2d(i,j))*(Imgd(i,j)-u2d(i,j));
M=M+M1;
end;
end;
MSE=((1/S(1))^2)*M
PSNR=10*log10((255^2)/MSE)

PROGRAM 2C

Img=imread('cameraman.tif');
A=input('Input the value of A : ');
L=input('No of Levels of Quantization ');
Rn=(2*A)*(-0.5+rand(size(Img)));
Imgd=double(Img);
Rnd=double(Rn);
v=Imgd+Rnd;
for i=1:L+1
t(i)=256*(i-1)/l;
end;
for j=1:L
r(j)=t(j)+(128/L);
end;
S=size(v);
for R=1:S(1)
for C=1:S(2)
for k=1:L
if t(k)<=v(R,C)
if v(R,C)<=t(k+1)
Img1(R,C)=r(k);
else Img1(R,C)=v(R,C);
end;
end;
end;
end;
end;

Img2=Img1-Rnd;
figure;
subplot(2,1,1);
imagesc(Imgd,[0,255]);
title(['The quantized image to ',int2str(L),' levels']);
colormap('gray');
subplot(2,1,2);
imagesc(Img2,[0,255]);
title('Original image');
colormap('gray');
Img2d=double(Img2);
M=0;
for i=1:S(1)
for j=1:S(2)
M1=(Imgd(i,j)-Img2d(i,j))*(Imgd(i,j)-Img2d(i,j));
M=M+M1;
end;
end;
MSE=((1/S(1))^2)*M
PSNR=10*log10((255^2)/MSE)

OUTPUTS:

PROGRAM 2A

Original image

50

100

150

200

250
50 100 150 200 250

No of levels of quantization 128


Original image

50

100

150

200

250
50 100 150 200 250

The quantized image to 128 levels

50

100

150

200

250
50 100 150 200 250

MSE =

0.5026

PSNR =

51.1186

-------------------------------------------------------------------------------------------------------------------
No of levels of quantization 64

MSE =

1.4889

PSNR =

46.4023
Original image

50

100

150

200

250
50 100 150 200 250

The quantized image to 64 levels

50

100

150

200

250
50 100 150 200 250

No of levels of quantization 32

MSE =

5.4799

PSNR =

40.7430
Original image

50

100

150

200

250
50 100 150 200 250

The quantized image to 32 levels

50

100

150

200

250
50 100 150 200 250

OUTPUTS PROGRAM 2B

No of levels of Quantization 40

MSE =

11.5560

PSNR =

37.5027
The quantized image to 40 levels

50

100

150

200

250
50 100 150 200 250

Original image

50

100

150

200

250
50 100 150 200 250

No of levels of Quantization 60

MSE =

5.2954

PSNR =

40.8918
The quantized image to 60 levels

50

100

150

200

250
50 100 150 200 250

Original image

50

100

150

200

250
50 100 150 200 250

No of levels of Quantization 80

MSE =

2.9339

PSNR =

43.4563
The quantized image to 80 levels

50

100

150

200

250
50 100 150 200 250

Original image

50

100

150

200

250
50 100 150 200 250

OUPUTS PROGRAM 2C

Input the value of A : 10


No of Levels of Quantization 64

MSE =

0.8625

PSNR =

48.7733
The quantized image to 64 levels

50

100

150

200

250
50 100 150 200 250

Original image

50

100

150

200

250
50 100 150 200 250

Input the value of A : 20


No of Levels of Quantization 64

MSE =

11.9996

PSNR =

37.3391
The quantized image to 64 levels

50

100

150

200

250
50 100 150 200 250

Original image

50

100

150

200

250
50 100 150 200 250

Input the value of A : 40


No of Levels of Quantization 64

MSE =

74.9850

PSNR =

29.3811
The quantized image to 64 levels

50

100

150

200

250
50 100 150 200 250

Original image

50

100

150

200

250
50 100 150 200 250

You might also like