Professional Documents
Culture Documents
LAB No: 05
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
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.
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'
National University of Technology (NUTECH)
Electrical Engineering Department
EE4407-Machine Learning Lab
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')
National University of Technology (NUTECH)
Electrical Engineering Department
EE4407-Machine Learning Lab
plt.show()
Code:
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
input_dir = 'C:\\Users\\Student\\naqash'
output_dir = 'C:\\Users\\Student.DESKTOP-7S88DU4\\Desktop\\output'
filenames = [f for f in os.listdir(input_dir) if f.endswith('.jpg')]
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()
National University of Technology (NUTECH)
Electrical Engineering Department
EE4407-Machine Learning Lab
Screenshot:
National University of Technology (NUTECH)
Electrical Engineering Department
EE4407-Machine Learning Lab
National University of Technology (NUTECH)
Electrical Engineering Department
EE4407-Machine Learning Lab
National University of Technology (NUTECH)
Electrical Engineering Department
EE4407-Machine Learning Lab
Code Explanation:
The script performs the following operations for each image:
Lab Task
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.
CONCLUSION