Digital Image Processing
( DSE – 3 )
Practical File
Submitted By
ALKA VERMA
Roll No.: 23DOECBTEC000094 (Batch – ECE B)
B. Tech Electronics and Communication Engineering
(5rd Semester)
To
Dr. Gurinder Singh
Assistant Professor
Department of Electronics and Communication Engineering
FACULTY OF TECHNOLOGY
UNIVERSITY OF DELHI
NEW DELHI – 110007
Assignment 4
Spatial/Geometric Transformations of Images
1. Write a MATLAB program to perform combined geometric transformation/
affine transformation — scaling, rotation, and translation —on a grayscale
image without using inbuilt functions.
Use scaling factors Sx = 1.5, Sy = 1.2, rotation angle θ = 45∘
, and translation
Tx = 50, Ty = 30.
Display the original and transformed images side by side.
clc; clear; close all;
% Step 1: Read grayscale image
I = imread('[Link]');
I = double(I);
[rows, cols] = size(I);
% Step 2: Transformation parameters
Sx = 1.5;
Sy = 1.2;
theta = 45;
Tx = 50;
Ty = 30;
% Step 3: Convert degrees to radians (since MATLAB trigonometric functions use radians)
theta = theta * pi / 180;
% Step 4: Define new image size (make it big enough to fit the transformed image)
new_rows = round(rows * Sy + Ty);
new_cols = round(cols * Sx + Tx);
out = zeros(new_rows, new_cols);
% Step 5: Apply inverse mapping for each pixel
for y_new = 1:new_rows
for x_new = 1:new_cols
% Inverse transformation equations
% (Find from where in original image this pixel came)
x_old = ((x_new - Tx)*cos(theta) + (y_new - Ty)*sin(theta)) / Sx;
y_old = (-(x_new - Tx)*sin(theta) + (y_new - Ty)*cos(theta)) / Sy;
% Nearest neighbor interpolation (round to nearest pixel)
x_old = round(x_old);
y_old = round(y_old);
% Check bounds to avoid indexing outside the image
if x_old >= 1 && x_old <= cols && y_old >= 1 && y_old <= rows
out(y_new, x_new) = I(y_old, x_old);
end
end
end
% Step 6: Display results
figure;
subplot(1,2,1);
imshow(uint8(I)); title('Original Image');
subplot(1,2,2);
imshow(uint8(out)); title('Transformed Image');
2. Write a MATLAB program to reduce the size of an image to half its original
dimensions using manual coordinate transformation and nearest-neighbour
interpolation, without using any inbuilt resizing functions.
% Step 1: Read the grayscale image
I = imread('[Link]');
I = double(I);
[rows, cols] = size(I);
% Step 2: Define scaling factors
scale_x = 0.5;
scale_y = 0.5;
% Step 3: Calculate new image dimensions
new_rows = round(rows * scale_y);
new_cols = round(cols * scale_x);
% Step 4: Initialize an empty matrix for reduced image
I_reduced = zeros(new_rows, new_cols);
% Step 5: Manual coordinate transformation + nearest neighbour
for y_new = 1:new_rows
for x_new = 1:new_cols
% Compute corresponding old coordinates
x_old = round(x_new / scale_x);
y_old = round(y_new / scale_y);
% Ensure indices are within image bounds
if x_old < 1, x_old = 1; end
if y_old < 1, y_old = 1; end
if x_old > cols, x_old = cols; end
if y_old > rows, y_old = rows; end
% Nearest neighbour interpolation: pick closest pixel
I_reduced(y_new, x_new) = I(y_old, x_old);
end
end
% Step 6: Display results
figure;
subplot(1,2,1);
imshow(uint8(I));
title('Original Image');
subplot(1,2,2);
imshow(uint8(I_reduced));
title('Reduced Image (Half Size)');
size(I)
size(I_reduced)
3. Perform a shearing transformation on a digital image using MATLAB, without
using inbuilt image transformation functions.
Task:
(a) Read an input image (grayscale or RGB).
(b) Apply horizontal shearing with factor kxand vertical shearing with factor ky.
(c) Implement the transformation manually using the shearing matrix:
S=[
1 kx 0
ky 1 0
001
]
(d)Use inverse mapping for pixel assignment and handle pixels outside the original
image.
(e)Display the original and sheared images side by side.
%% (a) Read input image
img = imread('[Link]');
if size(img,3) == 3
img_gray = rgb2gray(img);
else
img_gray = img;
end
img_gray = double(img_gray);
[rows, cols] = size(img_gray);
%% (b) Shearing factors
kx = 0.5; % horizontal shear
ky = 0.3; % vertical shear
%% (c) Shearing matrix
S = [1 kx 0;
ky 1 0;
0 0 1];
%% Prepare output image size
% Compute corners after shearing
corners = [1 1 1;
cols 1 1;
1 rows 1;
cols rows 1]';
sheared_corners = S * corners;
% Find new image size
min_x = floor(min(sheared_corners(1,:)));
max_x = ceil(max(sheared_corners(1,:)));
min_y = floor(min(sheared_corners(2,:)));
max_y = ceil(max(sheared_corners(2,:)));
new_cols = max_x - min_x + 1;
new_rows = max_y - min_y + 1;
sheared_img = zeros(new_rows, new_cols);
%% (d) Inverse mapping
S_inv = inv(S);
for i = 1:new_rows
for j = 1:new_cols
% Map output pixel to input pixel using inverse
pos = S_inv * [j+min_x-1; i+min_y-1; 1];
x = pos(1);
y = pos(2);
% Check if mapped position is inside original image
if x >= 1 && x <= cols && y >= 1 && y <= rows
% Nearest neighbor interpolation
sheared_img(i,j) = img_gray(round(y), round(x));
end
end
end
%% (e) Display original and sheared images
figure;
subplot(1,2,1);
imshow(uint8(img_gray));
title('Original Image');
subplot(1,2,2);
imshow(uint8(sheared_img));
title('Sheared Image');