0% found this document useful (0 votes)
25 views45 pages

TT XLA Chapter1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views45 pages

TT XLA Chapter1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Make images

come alive w ith


scikit - image
I M A G E P R OC E S S I N G IN P YT H ON
What is image processing?
Operations on images and videos
to:

Enhance an image

Extract useful

information

Analyze it and make


decisions

IMAGE PROCESSING IN PYTHON


What is image processing?
Operations to on images and videos
to:

Enhance an image

Extract useful

information

Analyze it and make


decisions

IMAGE PROCESSING IN PYTHON


Applications
Medical image analysis

Arti cial intelligence

Image restoration and enhancement

Geospatial computing

Surveillance

Robotic vision

Automotive safety

And many more...

IMAGE PROCESSING IN PYTHON


Purposes
1. Visualization:
Objects that are not visible

2. Image sharpening and restoration


A be er image

3. Image retrieval
Seek for the image of interest

4. Measurement of pa ern
Measures various objects

5. Image Recognition
Distinguish objects in an
image

IMAGE PROCESSING IN PYTHON


Intro to scikit - image
Easy to use

Makes use of Machine Learning

Out of the box complex

algorithms

IMAGE PROCESSING IN PYTHON


What is an image ?

IMAGE PROCESSING IN PYTHON


What is an image ?

IMAGE PROCESSING IN PYTHON


Images in scikit -
image
from skimage import data
rocket_image =
data.rocket()

IMAGE PROCESSING IN PYTHON


RGB channels

IMAGE PROCESSING IN PYTHON


Grayscaled images

IMAGE PROCESSING IN PYTHON


RGB vs
Grayscale
from skimage import color
grayscale =
color.rgb2gray(original) rgb =
color.gray2rgb(grayscale)

IMAGE PROCESSING IN PYTHON


Visualizing images in the
co'ut wrse
Don orry about Matplotlib!

def show_image(image, title='Image',


cmap_type='gray'): plt.imshow(image,
cmap=cmap_type)
plt.title(title)
plt.axis('off')
plt.show()

Show_image(img)

IMAGE PROCESSING IN PYTHON


Visualizing images in the
co urse
from skimage import color
grayscale =
color.rgb2gray(original)

show_image(grayscale, "Grayscale")

IMAGE PROCESSING IN PYTHON


Let's practice !
I M A G E P R OC E S S I N G IN
P YT H O N
NumPy for
images
I MAG E P R OC E S S I N G I N P YT
H ON
NumPy for
images
Fundamentals of image
processing techniques
Flipping

Extract and analyze features

IMAGE PROCESSING IN PYTHON


Images as
NdArrays

# Loading the image using Matplotlib


madrid_image = plt.imread('/madrid.jpeg')

type(madrid_image)

<class 'numpy.ndarray'>

IMAGE PROCESSING IN PYTHON


Colors w ith
NumPy

IMAGE PROCESSING IN PYTHON


Colors w ith
N#uObtaining
mPy the red values of the image
red = image[:, :, 0]

# Obtaining the green values of the image


green = image[:, :, 1]

# Obtaining the blue values of the image


blue = image[:, :, 2]

IMAGE PROCESSING IN PYTHON


Colors w ith
NumPy

plt.imshow(red,
cmap="gray")
plt.title('Red')
plt.axis('off'
) plt.show()

IMAGE PROCESSING IN PYTHON


Shape
s

# Accessing the shape of the


image madrid_image.shape

(426, 640, 3)

IMAGE PROCESSING IN PYTHON


Si ze
s

# Accessing the shape of the


image madrid_image.size

817920

IMAGE PROCESSING IN PYTHON


Flipping images: v ertical
y# Flip the image in up direction
vertically_flipped =
np.flipud(madrid_image)

show_image(vertically_flipped, 'Vertically
flipped image')

IMAGE PROCESSING IN PYTHON


Flipping images: hori z ontal
y# Flip the image in left direction
horizontally_flipped =
np.fliplr(madrid_image)

show_image(horizontally_flipped,
'Horizontally flipped image')

IMAGE PROCESSING IN PYTHON


What is a histogram ?

IMAGE PROCESSING IN PYTHON


Color histograms

IMAGE PROCESSING IN PYTHON


Applications of histograms
Analysis

Thresholding

Brightness and contrast

Equalize an image

IMAGE PROCESSING IN PYTHON


Histograms in
Matplotlib

# Red color of the image


red = image[:, :, 0]

# Obtain the red histogram


plt.hist(red.ravel(), bins=256)

IMAGE PROCESSING IN PYTHON


Visualizing histograms w ith
Matplotlib
blue = image[:, :, 2]

plt.hist(blue.ravel(), bins=256)
plt.title('Blue Histogram')
plt.show()

IMAGE PROCESSING IN PYTHON


Let's practice !
I M A G E P R OC E S S I N G IN
P YT H O N
Getting started
w ith
thresholding
I MAG E P R OC E S S I N G IN P
YT H ON
Thresholdin
gartitioning an image into a foreground
P
and background

By making it black and w hite

We do so by se ing each pixel to:

255 (white) if pixel > thresh


value 0 (black) if pixel < thresh
value

IMAGE PROCESSING IN PYTHON


Thresholdin
gimplest method of image segmentation
S

Isolate objects
Object detection

Face detection

Etc .

IMAGE PROCESSING IN PYTHON


Thresholdin
gnly from grayscale images
O

IMAGE PROCESSING IN PYTHON


Appl y it
# Obtain the optimal threshold
value thresh = 127

# Apply thresholding to the


image binary = image > thresh

# Show the original and


thresholded show_image(image,
'Original') show_image(binary,
'Thresholded')

IMAGE PROCESSING IN PYTHON


Inverted thresholding
# Obtain the optimal threshold
value thresh = 127

# Apply thresholding to the


image inverted_binary = image
<= thresh

# Show the original and


thresholded show_image(image,
'Original')
show_image(inverted_binary,
'Inverted thresholded')

IMAGE PROCESSING IN PYTHON


Categories
Global or histogram based : good for uniform backgrounds

Local or adapti v e : for uneven background illumination

IMAGE PROCESSING IN PYTHON


Try more thresholding
algorithms
from skimage.filters import try_all_threshold

# Obtain all the resulting images


fig, ax = try_all_threshold(image,
verbose=False)

# Showing resulting
plots show_plot(fig,
ax)

IMAGE PROCESSING IN PYTHON


Try more thresholding
algorithms

IMAGE PROCESSING IN PYTHON


Optimal thresh
value
Global
Uniform backgro u nd
# Import the otsu threshold function
from skimage.filters import threshold_otsu

# Obtain the optimal threshold value


thresh = threshold_otsu(image)

# Apply thresholding to the image


binary_global = image > thresh

IMAGE PROCESSING IN PYTHON


Optimal thresh
value
Global
# Show the original and binarized image
show_image(image, 'Original')
show_image(binary_global, 'Global
thresholding')

IMAGE PROCESSING IN PYTHON


Optimal thresh
value
Local
Uneven backgro u nd
# Import the local threshold function
from skimage.filters import threshold_local

# Set the block size to 35


block_size = 35

# Obtain the optimal local


thresholding
local_thresh =
threshold_local(text_image
, block_size, offset=10)

# Apply local thresholding and obtain the binary image


binary_local = text_image > local_thresh

IMAGE PROCESSING IN PYTHON


Optimal thresh
value
Local
# Show the original and binarized image
show_image(image, 'Original')
show_image(binary_local, 'Local thresholding')

IMAGE PROCESSING IN PYTHON


Let's practice !
I M A G E P R OC E S S I N G IN
P YT H O N

You might also like