DIP - Assignment - 1
DIP - Assignment - 1
DIP - Assignment - 1
ASSIGNMENT # 01
SUBMITTED TO
Dr. Jameel Khan
SUBMITTED BY
Hamza (21-TE-80)
DEPARTMENT OF TELECOMMUNICATION
ENGINEERING
1. Image Interpolation:
Image interpolation is a technique used to estimate and generate new pixel values in an image when the
image is transformed, such as resized, rotated, or distorted. The process involves predicting what the pixel
values between known data points should be.
Image interpolation occurs in all digital photos at some place. It happens anytime you resize or remap
(distort) your image from one pixel grid to another. Image resizing is necessary when you need to increase
or decrease the total number of pixels, whereas remapping can occur under a wider variety of scenarios:
correcting for lens distortion, changing perspective, and rotating an image.
There are several techniques and algorithms for image interpolation, even if the same image resizes or
remap is performed, the results can vary significantly depending on the interpolation algorithm. Itis only an
approximation, therefore an image will always lose some quality each time interpolation is performed.
Why Interpolation:
When you zoom in on an image or increase its resolution, the computer must add new pixels where
none existed before. These pixels need to be filled with color values that closely match the
surrounding pixels.
Similarly, when rotating or transforming an image, the positions of the pixels change, and new pixel
values must be calculated.
Nearest neighbour interpolation is a simple and commonly used method in digital image processing for
resizing and resampling. It is particularly favoured for its computational simplicity and speed.
Nearest neighbour interpolation works by selecting the pixel value from the closest pixel in the original
image and assigning it to the corresponding pixel in the new image.
If (x′, y′) represents the coordinates of a pixel in the resized image, and (x, y) represents the
corresponding coordinates in the original image, nearest neighbour interpolation can be expressed as:
I′ (x′, y′) = I(round(x), round(y)) where I (x, y) is the pixel value in the original image and I′ (x′ , y′) is
the pixel value in the resized image.
For each pixel in the resized image, the algorithm finds the nearest pixel in the original image based on
its distance, then copies its value. This results in a fast and straightforward process but can cause blocky
or jagged effects when scaling up, as no new pixel values are generated—just existing ones are repeated.
It is the most basic and requires the least processing time of all the interpolation algorithms because it
only considers one pixel — the closest one to the interpolated point. This has the effect of simply
making each pixel bigger.
Bilinear interpolation is used to estimate the pixel value at a non-integer position in two-dimensional
space (both the x and y dimensions of an image). This technique is used to resize images or to
estimate pixel values along the rows and columns. When resizing images, bilinear interpolation takes
two adjacent pixel values (for example, along a row or column) and estimates the intermediate pixel
value by assuming a linear relationship between them. In this way, the pixel value is computed as a
weighted average of its two neighbours. In other words, it considers the closest 2x2 neighbourhood
of known pixel values surrounding the unknown pixel. It then takes a weighted average of these 4
pixels to arrive at its final interpolated value. This results in much smoother looking images than
nearest neighbour.
Perform linear interpolation along the x-axis using two pairs of pixel values.
Perform a second interpolation along the y-axis using the results from the first step
MATLAB Code for Bilinear Interpolation:
OUTPUT:
3. Bicubic Image Interpolation:
Bicubic interpolation is an advanced interpolation technique used for resampling and resizing images
or data. It calculates the intensity of a pixel based on the values of the 16 nearest pixels (4x4
neighborhood) surrounding the target pixel. This method is more sophisticated than bilinear
interpolation, which uses only 4 neighboring pixels, and it provides smoother, more accurate results
as it goes one step beyond bilinear by considering the closest 4x4 neighbourhood of known pixels —
for a total of 16 pixels.
Since these are at various distances from the unknown pixel, closer pixels are given a higher
weighting in the calculation. Bicubic produces noticeably sharper images than the previous two
methods and is perhaps the ideal combination of processing time and output quality.
For this reason, it is a standard in many image editing programs (including Adobe Photoshop),
printer drivers and in-camera interpolation.
MATLAB Code for Bicubic Image Interpolation:
OUTPUT:
★ Comparison between Nearest Neighbor Bilinear & Bicubic interpolations:
The following graphs show the difference images or error maps that directly highlight where the interpolated
images differ from the original. This technique involves calculating the pixel-wise difference between the
original image and the upscaled versions, and then visualizing that difference.
Visual Comparison:
Nearest Neighbor: The nearest neighbor method produces a blocky image, especially when
upscaled. This method doesn't generate new pixel values but simply repeats existing values, which
can make the image look pixelated and jagged.
Bilinear: This method offers smoother results compared to our nearest neighbor. While some details
are lost due to averaging, the image looks softer and less pixelated.
Bicubic: Bicubic interpolation usually gives the smoothest and most visually appealing results. The
transitions between pixels are more natural, and the image retains more details, particularly when
scaling back up to the original size.
2. Affine Transformations:
is a mathematical operation used to change the geometry of an object in a 2D or 3D space. It involves
transformations like rotation, translation (shifting), scaling, and shearing, all while preserving the points'
relative relationships (parallel lines remain parallel). It can be thought of as a combination of these
operations.
Affine transformations can be represented using matrix multiplication. The general form of the
transformation matrix for 2D affine transformations is:
Where:
x′, y' are the transformed coordinates after applying the affine transformation.
The rotation, scaling, and shearing are controlled by the 2x2 matrix part:
The translation is controlled by the additional values tx and ty, which shift the points in the x and y
direction
Rotation
Translation
Scaling
Here is the Original Image I will be using to perform all common affine transformations:
Rotation:
CODE:
OUTPUT:
Translation:
CODE:
OUTPUT
Scaling:
CODE
OUTPUT
Combined Transformation (Rotation, Scaling, Translation):
3. Remove and Highlight Kidneys from an Image
I have stored the coordinates (x, y) values of both kidneys in a txt file and then load them in code and apply
a mask, draw polygons around them and then darken the remaining area while highlighting both kidneys.
CODE:
OUTPUT:
On the other hand, if we follow the approach mentioned in the assignment guidelines then we get this best
possible result:
Hamza (21-TE-
80)