0% found this document useful (0 votes)
9 views

Lab05 ML

Uploaded by

akbarmughal2824
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lab05 ML

Uploaded by

akbarmughal2824
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

National University of Technology (NUTECH)

Electrical Engineering Department


EE4407-Machine Learning Lab

LAB No: 05

NAME: M Ahmed Mustafa

ID NO: F20603040

Lab 05: Fundamentals of Image Processing for Machine Learning

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

Role in Machine Learning and Computer Vision


In machine learning, particularly in the field of computer vision, image processing is used to preprocess
images for better feature extraction, which is essential for the performance of machine learning
algorithms. Preprocessed images can feed into machine learning models more effectively, allowing
the models to train faster and perform better. In computer vision, image processing is used to
understand and interpret the visual world. It plays a crucial role in various applications, from facial
recognition and autonomous vehicles to medical imaging analysis.

Overview of Common Image Processing Tasks


1. Reading: The process of loading an image file into an array of pixel values that can be
manipulated by a computer program.
National University of Technology (NUTECH)
Electrical Engineering Department
EE4407-Machine Learning Lab

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.

3. Filtering: Applying filters to an image to enhance certain features or remove noise.


Examples include smoothing, sharpening, and edge enhancement filters.

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:

import os # for operating system interaction


import cv2 # used for image processing
import numpy as np # OpenCV uses for image arrays
import matplotlib.pyplot as plt # for displaying images

Define Directories:

input_dir = 'path_to_input_images'
output_dir = 'path_to_output_images'

Get List of Image Filenames:

filenames = [f for f in os.listdir(input_dir) if


f.endswith('.jpg')]
National University of Technology (NUTECH)
Electrical Engineering Department
EE4407-Machine Learning Lab

Image Processing Loop: Within a for loop, each image file is processed:
Read Image in Grayscale:

gray_image = cv2.imread(os.path.join(input_dir, filename),


cv2.IMREAD_GRAYSCALE)

Each image is read in grayscale, reducing the data needed for


processing.

Resize Image:

resized_image = cv2.resize(gray_image, (64, 64))

The image is resized to 64x64 pixels, a common requirement for


machine learning models.

Apply Gaussian Blur:

blurred_image = cv2.GaussianBlur(resized_image, (5, 5), 0)

A Gaussian blur is applied to the resized image, which helps


to reduce image noise and detail.

Perform Edge Detection:

edges = cv2.Canny(blurred_image, 100, 200)

The Canny edge detection algorithm is applied to the blurred


image to highlight edges.

Save Processed Image:

cv2.imwrite(os.path.join(output_dir, filename), edges)

The processed image with detected edges is saved to the output


directory.
National University of Technology (NUTECH)
Electrical Engineering Department
EE4407-Machine Learning Lab

Display Images: The original and processed images are displayed side by
side for comparison:

Read and Convert Original Image for Display:

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.

Convert Edge Image for Display:

processed_image = cv2.cvtColor(edges, cv2.COLOR_GRAY2RGB)

The edge-detected image is converted from grayscale to RGB to


match the format expected by matplotlib.

Plotting the Images side by side

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

Implement an image-preprocessing pipeline that prepares an image dataset for machine


learning model input. For this, create a folder having the 3x images in jpg format and bring
them to the IDE for further processing.

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'

filenames = [f for f in os.listdir(input_dir) if f.endswith('.jpg')]

for filename in filenames:


gray_image = cv2.imread(os.path.join(input_dir, filename),
cv2.IMREAD_GRAYSCALE)

output_path = os.path.join(output_dir, filename)


cv2.imwrite(output_path, gray_image)
resized_image = cv2.resize(gray_image, (64, 64))
blurred_image = cv2.GaussianBlur(resized_image, (5, 5), 0)
edges = cv2.Canny(blurred_image, 100, 200)
cv2.imwrite(os.path.join(output_dir, filename), edges)

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

You might also like