# Assignment 1
## ELL715 - Digital Image Processing
#### 1. Image denoted as _f(x, y)_, is transformed to image _g(x,y)_. _g(x,y)_ is 3 times larger along y-axis and 2 times larger along x-axis than _f(x, y)_. Also _g(x,y)_ is at 6 units horizontal and 7 units vertical distance from _f(x,y)_. <br> Write a code to do this. Show _f(x,y)_ and _g(x,y)_. <br> Compute _h(x,y)_ third image by rotating pixels of image 2 _g(x,y)_ by 75◦ degrees counter clockwise. Write a code to do this. <br> Show _f(x,y)_, _g(x,y)_ and _h(x,y)_.
##### Code
```matlab
clc;
clear all;
close all;
F=imread('C:\Users\pc\Desktop\ELL715\Assignment1\test1.jpg'); % f(x,y)
imshow(F); %test image
F=imresize(F, 0.04) %resize to show translation
figure;
imshow(F);
F_ref = imref2d(size(F)) %referencing image F to x-y coordinates
T=[2 0 0; 0 3 0; 6 7 1] %transformation - resize and translation matrix
tform=affine2d(T);
[G, G_ref]=imwarp(F,tform); % g(x,y) by transforming f(x,y)
%The outputs are a spatially referenced image
%specified by the image data G and its associated spatial referencing
%object G_ref
theta=-75; %anti-clockwise rotation by 75 degrees
R=[cosd(theta) sind(theta) 0; -sind(theta) cosd(theta) 0; 0 0 1] %rotation matrix
tform2=affine2d(R);
[H, H_ref]=imwarp(G,tform2); % h(x,y) by rotating g(x,y)
figure;
subplot(1,3,1); %comparing f(x,y), g(x,y) and h(x,y)
imshow(F, F_ref);
title('f(x,y)');
subplot(1,3,2);
imshow(G, G_ref);
title('g(x,y)');
subplot(1,3,3);
imshow(H, H_ref);
title('h(x,y)');
```
##### Images
<p align = "center">
<img width = 350 src=https://i.imgur.com/K1Xur65.jpg alt="3D diagram">
<figcaption align = "center">Fig 1.a: Input Image</figcaption>
</p>
<p align = "center">
<img width = 1080 src=https://i.imgur.com/sJjQy0I.png alt="3D diagram">
<figcaption align = "center">Fig 1.b: Output Images</figcaption>
</p>
![]()
##### Conclusion
The input image had to be within 10 times of 6 pixels to show noticeable translation, thus we chose spectrum as it is relatively unaffected by the pixelation caused by resizing.
Details of _f(x,y), g(x,y), h(x,y)_:
```matlab
F_ref =
imref2d with properties:
XWorldLimits: [0.5000 32.5000]
YWorldLimits: [0.5000 24.5000]
ImageSize: [24 32]
PixelExtentInWorldX: 1
PixelExtentInWorldY: 1
ImageExtentInWorldX: 32
ImageExtentInWorldY: 24
XIntrinsicLimits: [0.5000 32.5000]
YIntrinsicLimits: [0.5000 24.5000]
G_ref =
imref2d with properties:
XWorldLimits: [7 71]
YWorldLimits: [8.5000 80.5000]
ImageSize: [72 64]
PixelExtentInWorldX: 1
PixelExtentInWorldY: 1
ImageExtentInWorldX: 64
ImageExtentInWorldY: 72
XIntrinsicLimits: [0.5000 64.5000]
YIntrinsicLimits: [0.5000 72.5000]
H_ref =
imref2d with properties:
XWorldLimits: [0.1679 87.1679]
YWorldLimits: [-62.4457 18.5543]
ImageSize: [81 87]
PixelExtentInWorldX: 1
PixelExtentInWorldY: 1
ImageExtentInWorldX: 87
ImageExtentInWorldY: 81
XIntrinsicLimits: [0.5000 87.5000]
YIntrinsicLimits: [0.5000 81.5000]
```
Hence, we successfully implemented the transformations of scaling, translation, and rotation as directed and the resulting image references are in accordance with the theoretically expected output.
#### 2. Take an 8-bit gray scale image and perform the following operations using MATLAB.
##### Initial Code
```matlab
clc;
close all;
clear all;
I=imread('C:\Users\pc\Desktop\ELL715\Assignment1\test2.jpg');
figure;
imshow(I);
title('8-bit Image');
G=im2gray(I); % Grayscale
figure;
imshow(G);
title('8-bit Grayscale');
G0 = double(G)/255; % Normalization
```
<p align = "center">
<img width = 1080 src=https://i.ibb.co/kyLTX9V/Q2-8bit.png alt="3D diagram">
<figcaption align = "center">Fig 2.1: Input 8-bit Image</figcaption>
</p>
<p align = "center">
<img width = 1080 src=https://i.ibb.co/gTPNnyh/Q2-Grayscale.png alt="3D diagram">
<figcaption align = "center">Fig 2.2: Input 8-bit Grayscale Image</figcaption>
</p>
##### a. –ve of the image, log and antilog of the image
```matlab
A = 255-G; % negative in 8-bit
figure;
subplot(1,3,1);imshow(A);
title('Negative Image');
const=2;
B=const*log(1+(G0)); % Log Transformation
subplot(1,3,2);imshow(B);
title('Log Transformation');
C= exp(0.5*G0)-1; % Antilog transformation
subplot(1,3,3);imshow(C);
title('Antilog Transformation');
```
<p align = "center">
<img width = 1080 src=https://i.ibb.co/xq64SXz/Q2-A.png alt="3D diagram">
<figcaption align = "center">Fig 2.a</figcaption>
</p>
##### b. Apply Gamma correction for gamma = 0.4, 2.5, 10, 25 and 100
```matlab
const = 2.5;
G1=const*(G0.^(0.4)); % Gamma correction
G2=const*(G0.^(2.5));
G3=const*(G0.^(10));
G4=const*(G0.^(25));
G5=const*(G0.^(100));
figure;
subplot(2,3,1); imshow(G); title('Original Grayscale');
subplot(2,3,2); imshow(G1); title('Gamma=0.4');
subplot(2,3,3); imshow(G2); title('Gamma=2.5');
subplot(2,3,4); imshow(G3); title('Gamma=10');
subplot(2,3,5); imshow(G4); title('Gamma=25');
subplot(2,3,6); imshow(G5); title('Gamma=100');
```
<p align = "center">
<img width = 1080 src=https://i.ibb.co/w4HwbWB/Q2-B.png alt="3D diagram">
<figcaption align = "center">Fig 2.b</figcaption>
</p>
##### c. 2, 3, 4 power of image
```matlab
P1=G0.^(2);
P2=G0.^(3);
P3=G0.^(4);
figure;
subplot(2,2,1); imshow(G); title('Original Grayscale');
subplot(2,2,2); imshow(P1); title('Power=2');
subplot(2,2,3); imshow(P2); title('Power=3');
subplot(2,2,4); imshow(P3); title('Power=4');
```
<p align = "center">
<img width = 1080 src=https://i.ibb.co/G0hm2Qn/Q2-C.png alt="3D diagram">
<figcaption align = "center">Fig 2.c</figcaption>
</p>
##### d. Plot Bit-planes of image (show all the 8-plane images)
```matlab
figure;
B1=bitget(G,1); subplot(2,4,1); imshow(logical(B1)); title('Bit plane 1');
B2=bitget(G,2); subplot(2,4,2); imshow(logical(B2)); title('Bit plane 2');
B3=bitget(G,3); subplot(2,4,3); imshow(logical(B3)); title('Bit plane 3');
B4=bitget(G,4); subplot(2,4,4); imshow(logical(B4)); title('Bit plane 4');
B5=bitget(G,5); subplot(2,4,5); imshow(logical(B5)); title('Bit plane 5');
B6=bitget(G,6); subplot(2,4,6); imshow(logical(B6)); title('Bit plane 6');
B7=bitget(G,7); subplot(2,4,7); imshow(logical(B7)); title('Bit plane 7');
B8=bitget(G,8); subplot(2,4,8); imshow(logical(B8)); title('Bit plane 8');
```
<p align = "center">
<img width = 1080 src=https://i.ibb.co/7yB5DBp/Q2-D.png alt="3D diagram">
<figcaption align = "center">Fig 2.d</figcaption>
</p>
##### e. Plot the histogram of original image and apply Histogram equalization and plot the resulted image
```matlab
figure;
subplot(2,2,1),imshow(I);
title('Original Image');
subplot(2,2,2), imshow(G);
title('Grayscale Image');
subplot(2,2,3), imhist(G);
title('Histogram of Grayscale');
H=histeq(G);
subplot(2,2,4), imshow(H);
title('Image Post Histogram Equalization');
```
<p align = "center">
<img width = 1080 src=https://i.ibb.co/wCL30vW/Q2-E.png alt="3D diagram">
<figcaption align = "center">Fig 2.e</figcaption>
</p>
##### f. Apply a transformation that highlights range [120,200] but preserves all other levels.
```matlab
F=G;
F((F>=120)&(F<=200))=255; % highlight
figure;
subplot(1,2,1); imshow(G); title('Original Grayscale');
subplot(1,2,2); imshow(F); title('Highlights Implemented');
```
<p align = "center">
<img width = 1080 src=https://i.ibb.co/pyy70dG/Q2-F.png alt="3D diagram">
<figcaption align = "center">Fig 2.f</figcaption>
</p>
##### Conclusion:
The negative, log, and antilog of the test grayscale image came out to be as expected. For Gamma corrections and the powers of test image, the output becomes darker with increasing the parameter agreeing with expected behaviour. Increment in bit planes resulted in clearer images progressively.
The two tasks of implementing histogram equalisation and selective highlights were successfully implemented on the test image.
#### 3. Use the test image available on [link](https://drive.google.com/drive/folders/1kQgo9J9bgqCe3DOU3eB3G8MWpYYokhrJ)
##### Initial Code
```matlab
clc;
close all;
clear all;
load('Copy of testimage.mat');
figure;
imshow(im); title('Original Image');
```
<p align = "center">
<img width = 1080 src=https://i.ibb.co/gZCtPQc/Q3.png alt="3D diagram">
<figcaption align = "center">Fig 3</figcaption>
</p>
##### a. Create a function to calculate the histogram and then implement histogram equalization on the test image without using inbuilt MATLAB functions
```matlab
[m,n]=size(im);
hist=zeros(1,256); % initialize histogram
for i=1:m
for j=1:n
hist(im(i,j)+1)=hist(im(i,j)+1)+1;
end
end
pdf=hist/(m*n); % probability distribution
cdf=zeros(1,256); % continuous
T=zeros(1,256); % pixel values
cdf(1)=pdf(1);
T(1)=round(cdf(1)*255);
for i=2:256
cdf(i)=pdf(i)+cdf(i-1);
T(i)=round(cdf(i)*255);
end
res=uint8(zeros(m,n));
for i=1:m
for j=1:n
res(i,j)=T(im(i,j)+1);
end
end
% res is in the range [0,255]
figure;
subplot(2,3,1); bar(hist,0.3); title('Implemented Histogram');
subplot(2,3,2); imhist(res); ylim([0,16000]); title('Implemented Histogram Equalisation');
subplot(2,3,3); imshow(res); title('Implemented Equalised Image');
```
<p align = "center">
<img width = 1080 src=https://i.ibb.co/PCtJd95/Q3-A.png alt="3D diagram">
<figcaption align = "center">Fig 3.a</figcaption>
</p>
##### b. Use the built-in function on the same image and compare with the histogram from step 1. Check mean squared error of both matrices
```matlab
in_built_hist=histeq(im);
figure;
subplot(2,3,1); bar(hist, 0.3); title('Implemented Histogram');
subplot(2,3,2); imhist(res); ylim([0,16000]); title('Implemented Histogram Equalisation');
subplot(2,3,3); imshow(res); title('Implemented Equalised Image');
subplot(2,3,4); imhist(im); ylim([0,16000]); title('In-built Histogram');
subplot(2,3,5); imhist(in_built_hist); ylim([0,16000]); title('In-built Histogram Equalisation');
subplot(2,3,6); imshow(in_built_hist); title('In-built Equalised Image');
mse=sum(sum((res-in_built_hist).^2))/(m*n);
```
<p align = "center">
<img width = 1080 src=https://i.ibb.co/7t22YVm/Q3-B.png alt="3D diagram">
<figcaption align = "center">Fig 3.b</figcaption>
</p>
##### c. Apply adaptive histogram equalization (CLAHE) and compare with other mapped images
```matlab
clahe=adapthisteq(im);
%Results
figure;
subplot(4,2,1), imshow(im); title('Original');
subplot(4,2,2), imhist(im); ylim([0,16000]); title('Original');
subplot(4,2,3), imshow(res); title('Implemented Eq');
subplot(4,2,4), imhist(res); ylim([0,16000]); title('Implemented Eq');
subplot(4,2,5), imshow(in_built_hist); title('In-built Hist Eq');
subplot(4,2,6), imhist(in_built_hist); ylim([0,16000]); title('In-built Hist Eq');
subplot(4,2,7), imshow(clahe); title('CLAHE');
subplot(4,2,8), imhist(clahe); ylim([0,16000]); title('CLAHE');
```
<p align = "center">
<img width = 1080 src=https://i.ibb.co/HzpQPfx/Q3-C.png alt="3D diagram">
<figcaption align = "center">Fig 3.c</figcaption>
</p>
##### Conclusion:
The results of our implementations for histogram and histogram equalisation closely matched the inbuilt MATLAB functions. Also, we successfully implemented adaptive histogram equalization (CLAHE) and compared with previous results.
The MSE loss from 3.b is reported as:
```matlab
mse = 6.3246
```