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

Image_Handling_Session -1_PDF

Uploaded by

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

Image_Handling_Session -1_PDF

Uploaded by

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

Topic: Image Handling

Dr. T. Swathi Priyadarshini, Assistant Professor, NGIT


Email_id: [email protected]
Image Handling
Previous Sessions:
• Basics of Python
• Numpy Arrays

This Session:

• Image Handling
DATA PRESENTATION

Audio File

Numerical File

Text File

Image File
DATA PRESENTATION

Audio File  1D array


Numerical File  2D array

Text File  1D or 2D array


Image File  2D or 3D array
Digital Image
• Digital Image : Numerical representation of an object or a scene, arranged as grid of
elements in rows and columns.
• Each element in the grid is called a pixel.
• Each pixel is related to the intensity/color of the point.
• A pixel value is represented as an integer value between (0-255) or a floating-point value
between (0-1).
Activity 1: Create 2D numpy array
Import numpy as np
array2d = np.array([[1,1,1,1,1,1,1],
[1,1,0,1,0,1,1],
[1,1,1,1,1,1,1],
[1,0,1,1,1,0,1],
[1,1,0,0,0,1,1],
[1,1,1,1,1,1,1]])

from matplotlib import pyplot as plt


plt.imshow(array2d, cmap = ‘gray’)
plt.show()

array2d.shape
Binary Images

• A 2D array of pixels that can have one of two colors, black and white.
• Each pixel is stored as a single bit — i.e. either 0 or 1 where ‘0’ represents black and ‘1’
represents white.
• Example: For above black and white image, have a shape of (8, 8).
Activity 2: Create 2D numpy array

arr2d = np.random.randint(0, 255, size=(5, 5))


arr2d

plt.imshow(arr2d, cmap= ‘gray’)


plt.show()

arr2d.shape
Activity 3: Create 3D numpy array

arr3d = np.random.randint(0, 255, size=(5, 5, 1))


arr3d

plt.imshow(arr2d, cmap= ‘gray’)


plt.show()

arr2d.shape
Grayscale Image

- Grayscale photos have only a single channel, read from 2D array.


- Each pixel is stored as 8 bits.
- Note: For Grayscale Images: Shape: (height, width) or (height, width, 1).
- Example: A single-channel grayscale image of size 5 x 5 would have a shape of (5, 5) or (5, 5, 1)
Activity 4: Create 3D numpy array

arr3d = np.random.randint(0, 255, size=(5, 5, 3))


arr3d

plt.imshow(arr2d)
plt.show()

arr2d.shape
RGB Image

• Colored Images have three channel, each read from 2D arrays.


• Note: For Color images, Shape: (height, width, channels)
• Example: For above RGB color image, have a shape of (8, 10, 3), where 3 corresponds to the
Red, Green, and Blue channels.
RGB Image

Colour Matcher - Computer Science Field Guide (csfieldguide.org.nz)


Images and Colours - Data Representation - Computer Science Field Guide
(csfieldguide.org.nz)
Activity 5: Separating Channels

arr3d_copy1 = arr3d.copy()

arr3d_copy1[:, :, 1] = 0 # Green
Channel
arr3d_copy1[:, :, 2] = 0 # Blue
Channel

plt.figure(figsize = (3,3))
plt.imshow(arr3d_copy1)
plt.show()
Image Handling
Functions
• imread(): read or load an image from a file into a program
Syntax:

• imshow(): display images


Syntax:

• imsave(): save an image to a file

Syntax:
Loading Image from a file
- Use imread() function to image from a file

import numpy as np
import matplotlib.pylab as plt
%matplotlib inline

my_image =
plt.imread(r"/shareddata/datasets/ngitcourse4/swathi/panda.jpeg")
print(my_image)

plt.imshow(my_image)
plt.show()
Image Manipulations

- List of few image manipulations


1. Cropping
2. Channel separation
3. Flipping
4. Conversion of Coloured image to Grayscale image
Image Manipulations
1. Cropping
- Focusing on a specific region of interest.
- Use imsave() function to save to another file

crop_image = my_image[60 : 150, 150 : 200, :]


plt.imshow(crop_image)
plt.show()
plt.imsave(‘image_cropped.jpg’, crop_image)
Image Manipulations
2. RGB Channels separation

# Make a copy of the image


my_image_copy = my_image.copy()

# Set the green and blue channels to zero on the copy


my_image_copy[:, :, 1] = 0 # Green channel
my_image_copy[:, :, 2] = 0 # Blue channel

# Display the resulting image with only the red channel


plt.imshow(my_image_copy)
plt.show()
# Horizontal flip
Image Manipulations h_flipped = my_image[:, ::-1]

3. Flipping image # Vertical flip


v_flipped = my_image[::-1, :]
- We can flip an image
horizontally or vertically. # Show both flips
plt.subplot(1, 2, 1)
- Horizontal flipping, also plt.imshow(h_flipped)
called left-to-right flipping plt.title('Horizontally flipped')
creates a mirror image of
the original plt.subplot(1, 2, 2)
plt.imshow(v_flipped)
plt.title('Vertically flipped')
plt.show()
Image Processing
• Image processing is a field of
study and a set of
techniques focused on the
manipulation, analysis, and
enhancement of images
using mathematical
algorithms and
computational methods.

• These techniques help


improve the quality of the
images making them
suitable for further analysis,
and enhancing the
performance of machine
learning models in various
PYTHON IMAGE HANDLING LIBRARIES

Visualization and plotting (includes basic image handling)

Pillow (PIL): Python Image Library

OpenCV (Open Source Computer Vision)


Python OpenCV Tutorial (pythonexamples.org)

Image processing for scientific computing


LOADING BULK OF IMAGES

You might also like