0% found this document useful (0 votes)
42 views50 pages

TT XLA Chapter3

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)
42 views50 pages

TT XLA Chapter3

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/ 50

Image restoration

I M A G E P R OC E S S I N G IN P YT H
ON
Restore an
image

IMAGE PROCESSING IN PYTHON


Image reconstr u ction
Fixing damaged images

Text remo v ing

Logo remo v ing

Object remo v ing

IMAGE PROCESSING IN PYTHON


Image reconstr u ction
Inpainting
Reconstructing lost parts of images

Looking at the non-damaged


regions

IMAGE PROCESSING IN PYTHON


Image reconstr u ction

IMAGE PROCESSING IN PYTHON


Image reconstr u ction in scikit -
image
from skimage.restoration import inpaint

# Obtain the mask


mask = get_mask(defect_image)

# Apply inpainting to the damaged image using the mask


restored_image = inpaint.inpaint_biharmonic(defect_image,
mask,
multichannel=True)

# Show the resulting image

show_image(restored_image)

IMAGE PROCESSING IN PYTHON


Image reconstr u ction in scikit -
image
# Show the defect and resulting images
show_image(defect_image, 'Image to restore')
show_image(restored_image, 'Image
restored')

IMAGE PROCESSING IN PYTHON


M ask
s

IMAGE PROCESSING IN PYTHON


M ask
sdef get_mask(image):
''' Creates mask with three defect regions '''
mask = np.zeros(image.shape[:-1])

mask[101:106, 0:240] = 1

mask[152:154, 0:60] = 1
60:100] =
mask[153:155, 1 1
100:120] =
mask[154:156,
mask[155:156, 120:140] = 1

mask[212:217, 0:150] = 1
150:256] = 1
mask[217:222,
return mask

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
Noise
I M A G E P R OC E S S I N G IN
P YT H O N
Noise

IMAGE PROCESSING IN PYTHON


Noise

IMAGE PROCESSING IN PYTHON


Apply noise in scikit -
image
# Import the module and function
from skimage.util import
random_noise

# Add noise to the image


noisy_image =
random_noise(dog_image)

# Show original and resulting image


show_image(dog_image)
show_image(noisy_image, 'Noisy
image')

IMAGE PROCESSING IN PYTHON


Apply noise in scikit -
image

IMAGE PROCESSING IN PYTHON


Reducing noise

IMAGE PROCESSING IN PYTHON


Denoising types
Total variation (TV)

Bilateral

Wavelet denoising

Non-local means
denoising

IMAGE PROCESSING IN PYTHON


Denoising
Using total v ariation lter denoising

from skimage.restoration import denoise_tv_chambolle

# Apply total variation filter denoising


denoised_image =
denoise_tv_chambolle(noisy_image,
weight=0.1,
multichannel=True
)

# Show denoised image


show_image(noisy_image, 'Noisy image')
show_image(denoised_image, 'Denoised
image')
IMAGE PROCESSING IN PYTHON
Denoising
Total v ariation lter

IMAGE PROCESSING IN PYTHON


Denoising
Bilateral lter

from skimage.restoration import denoise_bilateral

# Apply bilateral filter denoising


denoised_image = denoise_bilateral(noisy_image,
multichannel=True)

# Show original and resulting images


show_image(noisy_image, 'Noisy image')
show_image(denoised_image, 'Denoised
image')

IMAGE PROCESSING IN PYTHON


Denoising
Bilateral lter

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
Superpixels &

segmentatio
n
I M A G E P R OC E S S I N G IN
P YT H ON
Segmentatio
n

IMAGE PROCESSING IN PYTHON


Segmentatio
n

IMAGE PROCESSING IN PYTHON


Image representation

IMAGE PROCESSING IN PYTHON


Superpixels

IMAGE PROCESSING IN PYTHON


Benefits of superpixels
More meaningful regions

Computational e ciency

IMAGE PROCESSING IN PYTHON


Segmentatio
n Supervised
Uns u per v ised

IMAGE PROCESSING IN PYTHON


Unsupervised segmentation
Simple Linear Iterati v e Cl u stering (SLIC)

IMAGE PROCESSING IN PYTHON


Unsupervised segmentation
(SLIC
# Import )the modules
from skimage.segmentation import slic
from skimage.color import label2rgb

# Obtain the segments


segments = segmentation.slic(image)

# Put segments on top of original image to compare


segmented_image = label2rgb(segments, image, kind='avg')

show_image(image)
show_image(segmented_image, "Segmented image")

IMAGE PROCESSING IN PYTHON


Unsupervised segmentation
(SLIC)

IMAGE PROCESSING IN PYTHON


More segments
# Import the modules
from skimage.segmentation import
slic from skimage.color import
label2rgb

# Obtain the segmentation with 300


regions segments = slic(image,
n_segments= 300)

# Put segments on top of original image to compare


segmented_image = label2rgb(segments, image,
kind='avg')

show_image(segmented_image)
IMAGE PROCESSING IN PYTHON
More segments

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
Finding conto u rs
I MAG E P R OC E S S I N G IN P
YT H ON
Finding conto u rs

Measure size Total points in domino tokens: 29.

Classify shapes

Determine the number of objects

IMAGE PROCESSING IN PYTHON


Binary
images

We can obtain a binary image


applying thresholding or using edge
detection

IMAGE PROCESSING IN PYTHON


Find conto u rs using scikit -
image THE IMAGE
PREPARING
Transform the image to 2D grayscale.

# Make the image grayscale


image = color.rgb2gray(image)

IMAGE PROCESSING IN PYTHON


Find conto u rs using scikit -
image THE IMAGE
PREPARING
Binarize the image

# Obtain the thresh value


thresh = threshold_otsu(image)

# Apply thresholding
thresholded_image = image > thresh

IMAGE PROCESSING IN PYTHON


Find conto u rs using scikit -
image
And then use find_contours() .

# Import the measure module


from skimage import
measure

# Find contours at a
constant value of 0.8
contours =
measure.find_contours(thres
holded_image, 0.8)

IMAGE PROCESSING IN PYTHON


Constant level
value

IMAGE PROCESSING IN PYTHON


The steps to spotting
conto u rsimport measure
from skimage
from skimage.filters import threshold_otsu

# Make the image grayscale


image = color.rgb2gray(image)
# Obtain the optimal thresh value of the
image thresh = threshold_otsu(image)

# Apply thresholding and obtain binary image


thresholded_image = image > thresh

# Find contours at a constant value of 0.8


contours =
measure.find_contours(thresholded_image,
0.8)

IMAGE PROCESSING IN PYTHON


The steps to spotting
conto
Res rs
ultinguin

IMAGE PROCESSING IN PYTHON


A conto u r ' s
shape
Contours: list of (n,2) -
ndarrays.
for contour in
contours:
print(contour.shape)

(433, 2)
(433, 2)
(401, 2)
(401, 2)
(123, 2)
(123, 2)
(59, 2)
(59, 2)
(59, 2)
(57, 2)
(57, 2)
(59, 2)
IMAGE PROCESSING IN PYTHON
A conto u r ' s
shape
for contour in
contours:
print(contour.shape)

(433 2)
,
(433 2) --> Outer border
,
(401 2)
,
(401 2)
,
(123 2)
,
(123 2)
,
(59, 2)
(59, 2)
(59, 2)
(57, 2)
(57, 2)
(59, 2)
(59, 2)
IMAGE PROCESSING IN PYTHON
A conto u r ' s
shape
for contour in
contours:
print(contour.shape)

(433, 2)
(433, 2) --> Outer
border
(401, 2)
(401, 2) --> Inner
border
(123, 2)
(123, 2)
(59, 2)
(59, 2)
(59, 2)
(57, 2)
(57, 2)
(59, 2)
(59, 2)
IMAGE PROCESSING IN PYTHON
A conto u r ' s
shape
for contour in
contours:
print(contour.shape)

(433, 2)
(433, 2) --> Outer
border
(401, 2)
(401, 2) --> Inner
border
(123, 2)
(123, 2) --> Divisory line of
tokens (59, 2)
(59, 2)
(59, 2)
(57, 2)
(57, 2)
(59, 2)
(59, 2)
IMAGE PROCESSING IN PYTHON
A conto u r ' s
shape
for contour in
contours:
print(contour.shape)

(433, 2)
(433, 2) --> Outer border
(401, 2)
(401, 2) --> Inner border
(123, 2)
(123, 2) --> Divisory line of
tokens (59, 2)
(59, 2)
(59, 2)
(57, 2) Number of dots:
(57, 2) 7.
(59, 2)
(59, 2) --> Dots

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