Updated Lab Manual 9 DIP
Updated Lab Manual 9 DIP
LAB MANUAL 9
Implementation of Image Processing techniques in Python
LAB OBJECTIVE:
The objective of this lab is to understand & implement Image Processing techniques in Python
Python 3
VS Code
VS Code Python extension
Windows
Install Python from python.org. Use the Download Python button that appears first on the page to
download the latest version.
Note:
To verify that you've installed Python successfully on your machine, run one of the following
commands (depending on your operating system):
py -3 --versionCopy
If the installation was successful, the output window should show the version of Python that
you installed. Alternatively, you can use the py -0 command in the VS Code integrated
terminal to view the versions of python installed on your machine.
Using a command prompt or terminal, create an empty folder called "hello", navigate into it,
and open VS Code (code) in that folder (.) by entering the following commands:
Note:
Alternately, you can create a folder through the operating system UI, then use VS Code's File >
Once you activate that environment, any packages you then install are isolated from other
environments, including the global interpreter environment, reducing many complications that
You can create non-global environments in VS Code using Venv or Anaconda with Python:
Create Environment.
Open the Command Palette (Ctrl+Shift+P), start typing the Python: Create
The command presents a list of environment types, Venv or Conda. For this example,
select Venv.
Integrating OpenCV with Visual Studio Code (VS Code) allows developers to leverage their
capabilities within a familiar development environment.
Below are some of the steps by which we can install OpenCV for Visual Studio Code
and Python:
Open your terminal or command prompt and install OpenCV using pip, the Python
package manager:
It provides a comprehensive set of functions and tools that facilitate the development of
While taking photographs is as simple as pressing a button, processing and improving those
That’s where image processing libraries like OpenCV come into play.
OpenCV is a popular open-source package that covers a wide range of image processing and
OpenCV is highly tuned for real-time applications and has a wide range of capabilities.
Data Augmentation
Data augmentation is the process of copying the original dataset with some slight
modifications to the original data, increasing the number of training samples to the dataset for
We can improve the model's performance by slightly changing the dataset and introducing
Random Rotation
Random Flip
Random Zoom
These techniques will improve the training samples for the model to train efficiently.
Image Resizing
Image Rotation
Image Translation
Image Normalization
Image Blurring
# Convert the image from BGR (OpenCV default) to RGB (for proper color display
in Matplotlib)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Get the original image dimensions (height and width, ignoring the third
dimension, which is color)
height, width = image_rgb.shape[:2]
# Resize the image (Zoomed-in version) using cubic interpolation for better
quality
zoomed_image = cv2.resize(
src=image_rgb,
dsize=(new_width, new_height),
interpolation=cv2.INTER_CUBIC # INTER_CUBIC provides smooth scaling for
enlargement
)
# Resize the image (Scaled-down version) using INTER_AREA for better quality
when shrinking
scaled_image = cv2.resize(
src=image_rgb,
dsize=(new_width1, new_height1),
interpolation=cv2.INTER_AREA # INTER_AREA is recommended for downscaling
)
# Create a figure with 1 row and 3 columns to display images side by side
fig, axs = plt.subplots(1, 3, figsize=(12, 5))
# Remove axis ticks (numbers) from all images for a cleaner display
Digital Image Processing 6th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
for ax in axs:
ax.set_xticks([])
ax.set_yticks([])
Output:
We just need to define rotation matrix listing rotation point, degree of rotation and the
scaling factor.
The rotation angle can be positive or negative. A positive angle rotates the image
The scale factor can be used to scale the image up or down. A scale factor of 1 will keep
the image the same size, while a scale factor of 2 will double the size of the python image.
# Create subplots
fig, axs = plt.subplots(1, 2, figsize=(7, 4))
plt.tight_layout()
plt.show()
Output
To Avoid Cropping
To avoid cropping when rotating an image, you need to calculate the new bounding
By default, cv2.warpAffine() keeps the output size the same as the input image, leading to
cropping.
We need to:
3. Use cv2.warpAffine() with the expanded canvas to keep the entire rotated image visible.
# Compute new bounding dimensions (to fit the rotated image without
cropping)
cos_val = abs(rotation_matrix[0, 0])
sin_val = abs(rotation_matrix[0, 1])
# Adjust the rotation matrix to shift the image to the center of the new
canvas
rotation_matrix[0, 2] += (new_w - w) / 2
rotation_matrix[1, 2] += (new_h - h) / 2
axs[0].imshow(image_rgb)
axs[0].set_title('Original Image')
axs[0].axis("off")
axs[1].imshow(rotated_image)
axs[1].set_title('Rotated Image (No Cropping)')
axs[1].axis("off")
plt.tight_layout()
plt.show()
Image Translation
Rotation spins an image around a fixed point (typically the center). It is performed using a
Translation moves an image horizontally and/or vertically without changing its orientation.
To translate an image using OpenCV, we need to create a transformation matrix. This matrix
the tx and ty elements. The tx element specifies the amount of translation in the x-axis, while
Output
Image Sheering
# Apply shearing
sheared_image = cv2.warpAffine(image_rgb, transformation_matrix, (width,
height))
# Create subplots
fig, axs = plt.subplots(1, 2, figsize=(8, 4))
Output
Image Normalization
Image normalization is a process of scaling the pixel values in an image to a specific range.
This is often done to improve the performance of image processing algorithms, as many
algorithms work better when the pixel values are within a certain range.
The normalization type specifies how the pixel values are scaled. There are several
different normalization types available, each with its own trade-offs between accuracy and
speed.
can help to improve the performance of algorithms such as image classification, object
# Normalization parameters
min_value = 0 # Minimum value after normalization
max_value = 1 # Maximum value after normalization
norm_type = cv2.NORM_MINMAX # Type of normalization
Output
localization/detection. There are several algorithms for detecting edges due to its wide
applicability.
In image processing and computer vision applications, Canny Edge Detection is a well-
In order to detect edges, the Canny edge detector first smoothes the image to reduce noise,
then computes its gradient, and then applies a threshold to the gradient.
The multi-stage Canny edge detection method includes the following steps::
o Weak edges (between threshold1 and threshold2) are kept only if connected to
strong edges.
Output
Task
blurring techniques:
Gaussian blurring:
Median blurrin
Bilateral blurring