Lab05 ML
Lab05 ML
LAB No: 05
ID NO: F20603040
Objective:
To introduce the fundamental concepts and applications of image processing within the field of
machine learning. Students will learn to manipulate and analyze image data, preparing them for
advanced topics like feature extraction and model training.
Tools/Software Requirements:
Python 3.x
Integrated Development Environment (IDE) such as PyCharm or Jupyter Notebook
Image processing libraries such as PIL (Pillow) or OpenCV
matplotlib library for displaying images
Sample images to work with
Image processing involves performing operations on images to enhance them or to extract useful
information. It is a form of signal processing where the input is an image, like a photograph or video
frame, and the output can be either an image or a set of characteristics or parameters related to the
image. In machine learning and computer vision, image processing is a critical step that precedes the
actual learning process. It transforms raw image data into a format that can be analyzed and used by
machine learning models to perform tasks such as classification, detection, and segmentation
2. Resizing: Altering the size of an image to meet specific dimensions, which is often necessary
for standardizing inputs to a machine-learning model. Standardizing input for a Convolutional
Neural Network (CNN) used in facial recognition systems. Without resizing, the CNN may not
be able to process the images if they do not all match its required input dimensions.
4. Grayscale Conversion: Removing the color information from an image, leaving just the
intensity of light. This simplifies the image data and is often used where color is not critical to
the analysis. Preprocessing for medical image analysis to highlight larger structures while
suppressing fine details that may not be of interest. Small variations or noise in medical images
could be misinterpreted as anomalies, leading to false positives.
5. Edge Detection: Identifying the boundaries of objects within an image. Edge detection is
used to isolate feature. Autonomous vehicles use edge detection in lane detection algorithms
to understand road boundaries. Without clear edge detection, the vehicle's guidance system
may have difficulty identifying lane markings, leading to navigation errors.
Implementation
Import Necessary Libraries:
Define Directories:
input_dir = 'path_to_input_images'
output_dir = 'path_to_output_images'
Image Processing Loop: Within a for loop, each image file is processed:
Read Image in Grayscale:
Resize Image:
Display Images: The original and processed images are displayed side by
side for comparison:
original_image =
cv2.cvtColor(cv2.imread(os.path.join(input_dir, filename)),
cv2.COLOR_BGR2RGB)
The original image is read again and converted from BGR to RGB
for correct color representation when displaying.
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(original_image)
plt.title('Original Image')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(processed_image)
plt.title('Processed Image')
plt.axis('off')
plt.show()
Lab Task
National University of Technology (NUTECH)
Electrical Engineering Department
EE4407-Machine Learning Lab
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
input_dir = 'C:\\Users\\Student\\Desktop\\dta'
output_dir = 'C:\\Users\\Student\\Desktop\\output_dta'
original_image = cv2.cvtColor(cv2.imread(os.path.join(input_dir,
filename)), cv2.COLOR_BGR2RGB)
processed_image = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(original_image)
National University of Technology (NUTECH)
Electrical Engineering Department
EE4407-Machine Learning Lab
plt.title('Original Image')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(processed_image)
plt.title('Processed Image')
plt.axis('off')
plt.show()
National University of Technology (NUTECH)
Electrical Engineering Department
EE4407-Machine Learning Lab
CONCLUSION