Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

Image Processing:

 Image processing allows us to transform and manipulate thousands of


images at a time and extract useful insights from them.
 Python is one of the widely used programming languages for this purpose.

Image processing Algorithms:


Image processing Algorithms are of five types they are:

 Morphological Image Processing.


 Gaussian Image Processing.
 Fourier Transform in image processing.
 Edge Detection in image processing.
 Wavelet image processing
Morphological Image Processing:
 Morphological image processing tries to remove the imperfections from the
binary images because binary regions produced by simple thresholding can
be distorted by noise.
 It also helps in smoothing the image using opening and closing operations.
 Morphological operations can be extended to grayscale images.
 It consists of non-linear operations related to the structure of features of an
image.
 It depends on the related ordering of pixels but on their numerical values.
 This technique analyzes an image using a small template known
as structuring element which is placed on different possible locations in the
image and is compared with the corresponding neighbourhood pixels.
 A structuring element is a small matrix with 0 and 1 values.

The two fundamental operations of morphological image processing are Dilation


and Erosion.

Dilation: operation adds pixels to the boundaries of the object in an image.


Erosion: operation removes the pixels from the object boundaries. 
The number of pixels removed or added to the original image depends on the size
of the structuring element. 

what is a structuring element?


 Structuring element is a matrix consisting of only 0’s and 1’s that can have
any arbitary shape and size.
 It is positioned at all possible locations in the image and it is compared with
the corresponding neighbourhood of pixels.

The square structuring element ‘A’ fits in the object we want to select, the ‘B’
intersects the object and ‘C’ is out of the object.

The zero-one pattern defines the configuration of the structuring element. It’s
according to the shape of the object we want to select.

The center of the structuring element identifies the pixel being processed.
source

Dilation/ source

Erosion / source

Gaussian Image Processing:


 Gaussian blur which is also known as gaussian smoothing, is the result of
blurring an image by a Gaussian function.
 It is used to reduce image noise and reduce details. 

The basic gaussian function looks like:


Original filter Result

Fourier Transform in image processing:

 Fourier transform breaks down an image into sine and cosine


components.
 It has multiple applications like image reconstruction, image
compression, or image filtering. 

A sinusoid, it comprises of three things:

 Magnitude – related to contrast


 Spatial frequency – related to brightness
 Phase – related to color information
The formula for 2D discrete fourier transform is:

In the above formula, f(x,y) denotes the image.

The inverse fourier transform converts the transform back to image.

The formula for 2D inverse discrete fourier transform is:

Edge Detection in image processing:

 Edge detection is an image processing technique for finding the boundaries


of objects within images.
 It works by detecting discontinuities in brightness.
 The most common edge detection algorithm is sobel edge detection
algorithm.
 Sobel detection operator is made up of 3*3 convolutional kernels.
 A simple kernel Gx and a 90 degree rotated kernel Gy.
 Separate measurements are made by applying both the kernel separately
to the image.

  * denotes the 2D signal processing convolution operation.


 Resulting gradient can be calculated as:
Wavelet Image Processing:

 A wavelet is a mathematical function useful in digital signal processing


and image compression .
 The principles are similar to those of Fourier analysis

The 2D wavelet transform example:

Image File Formats:

 Image file formats are standardized means of organizing and storing digital


images.
 An image file format may store data in an uncompressed format, a
compressed format (which may be lossless or lossy), or a vector format.
Python provide library to determine the type of an image, on such library is
imghdr.

Value Image format

'rgb' SGI ImgLib Files

Image –
'gif' GIF 87a and 89a Files

'pbm' Portable Bitmap Files

'pgm' Portable Graymap Files

'ppm' Portable Pixmap Files

'tiff' TIFF Files

'rast' Sun Raster Files

'xbm' X Bitmap Files

'jpeg' JPEG data in JFIF or Exif formats

'bmp' BMP files

'png' Portable Network Graphics

Manipulation Operations:
 Image Manipulation refers to a process of bringing changes to a digitized
image for transforming it to a desired image.
 The changes are made possible by resorting to image processing.
 Image manipulation is utilized to create magazine covers and albums from
photographs.
 A single photograph may be modified to suit the requirement, or several
photographs can be combined to form a collage.
 Currently, there are numerous software applications ranging from
professional applications to basic imaging software.

The properties of Images:


In addition to showing an image, the Image class provides a number of other
properties and methods that can be used with images. Some of the key properties
include:

 filename: The name of the image file


 format: The file format such as JPG, GIF
 mode: The image mode such as RGB, RFBA, CMYK, or YCbCr
 size: The image size in pixels displayed as a width, height tuple
 width: The width of the image in pixels
 height: The height of the image in pixels
 is_animated: Indicates if the image is more than one frame. True if it is, False if
it is not. This attribute is not included in all images.

Python Imaging Library:


Python Imaging Library is a free and open-source additional library for
the Python programming language that adds support for opening, manipulating,
and saving many different image file formats.

Converting an Image to Black and White:


Converting an image to black and white involves two steps.
1.Read the source image as grey scale image.

2.Convert the grey scale image to binary with a threshold of your choice.

Example 1: Convert color image to black and white


In the following example, we will read the following color image using cv2.imread() as
grey scale image and then apply cv2.threshold() function on the image array.
Input Image:
Python Program:
import cv2
#read image
img_grey = cv2.imread('D:/original.png', cv2.IMREAD_GRAYSCALE)
# define a threshold, 128 is the middle of black and white in
grey scale
thresh = 128
# threshold the image
img_binary = cv2.threshold(img_grey, thresh, 255,
cv2.THRESH_BINARY)[1]
#save image
cv2.imwrite('D:/black-and-white.png',img_binary)

Output Image:

Converting an Image to Grayscale Image in python:


import cv2
image = cv2.imread('C:/Users/N/Desktop/Test.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('Original image',image)
cv2.imshow('Gray image', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

Input:

Output:

Blurring an Image:
 Blurring an image can be done by reducing the level of noise in the image
by applying a filter to an image.
 Image blurring is one of the important aspects of image processing.
 The ImageFilter class in the Pillow library provides several standard image
filters.

There are various techniques used to blur images They are:

 Simple blur
 Box blur
 Gaussian blur

Simple blur:
It applies a blurring effect on to the image as specified through a specific kernel
or a convolution matrix.

Syntax:

filter(ImageFilter.BLUR)

Example:

#Import required Image library


from PIL import Image, ImageFilter

#Open existing image


OriImage = Image.open('images/boy.jpg')
OriImage.show()

blurImage = OriImage.filter(ImageFilter.BLUR)
blurImage.show()
#Save blurImage
blurImage.save('images/simBlurImage.jpg')

Original image:
Blurred image:

Box blur:
In this filter, we use ‘radius’ as parameter. Radius is directly proportional to the
blur value.

Syntax:

ImageFilter.BoxBlur(radius)
Where,
 Radius − Size of the box in one direction.
 Radius 0 − means no blurring & returns the same image.
 RRadius 1 &minnus; takes 1 pixel in each direction, i.e. 9 pixels in total.
Example:
#Import required Image library
from PIL import Image,
#Open existing image
OriImage = Image.open('images/boy.jpg')
OriImage.show()
#Applying BoxBlur filter
boxImage = OriImage.filter(ImageFilter.BoxBlur(5))
boxImage.show()
#Save Boxblur image
boxImage.save('images/boxblur.jpg')

Output:

Original Image:

Blurred Image:
Gaussian Blur:
 This filter also uses parameter radius and does the same work as box blur
with some algorithmic changes.
 In short, changing the radius value, will generate different intensity of
‘Gaussianblur’ images.

Syntax

ImageFilter.GaussianBlur(radius=2)
Where,
 Radius – Blur radius

Example

#Import required Image library


from PIL import Image, ImageFilter
#Open existing image
OriImage = Image.open('images/boy.jpg')
OriImage.show()
#Applying GaussianBlur filter
gaussImage = OriImage.filter(ImageFilter.GaussianBlur(5))
gaussImage.show()
#Save Gaussian Blur Image
gaussImage.save('images/gaussian_blur.jpg')

Output:

Original Image:
Blurred Image:

Edge Detection:
 Edge detection is a technique of image processing used to identify points
in a digital image with discontinuities, simply to say, sharp changes in the
image brightness.
 These points where the image brightness varies sharply are called the
edges (or boundaries) of the image.

There are various methods in edge detection, and the following are some of
the most commonly used methods-

 Prewitt edge detection


 Sobel edge detection
 Laplacian edge detection
 Canny edge detection

Prewitt Edge Detection:


This method is a commonly used edge detector mostly to detect the horizontal
and vertical edges in images. The following are the Prewitt edge detection
filters.
Sobel Edge Detection:  

This uses a filter that gives more emphasis to the centre of the filter. It is one of
the most commonly used edge detectors and helps reduce noise and provides
differentiating, giving edge response simultaneously. The following are the
filters used in this method-

The following shows the before and after images of applying Sobel edge
detection-

Laplacian Edge Detection:


The Laplacian edge detectors vary from the previously discussed edge
detectors. This method uses only one filter (also called a kernel). In a single
pass, Laplacian edge detection performs second-order derivatives and hence
are sensitive to noise. To avoid this sensitivity to noise, before applying this
method, Gaussian smoothing is performed on the image.
Canny Edge Detection:
This is the most commonly used highly effective and complex compared to
many other methods. It is a multi-stage algorithm used to detect/identify a
wide range of edges. The following are the various stages of the Canny edge
detection algorithm-

 Convert the image to grayscale


 Reduce noise – as the edge detection that using derivatives is sensitive
to noise, we reduce it.
 Calculate the gradient – helps identify the edge intensity and direction.
 Non-maximum suppression – to thin the edges of the image.
 Double threshold –  to identify the strong, weak and irrelevant pixels in
the images.
 Hysteresis edge tracking – helps convert the weak pixels into strong
ones only if they have a strong pixel around them.

The following are the original minion image and the image after applying this
method.
Reducing the size of an Image:
 Syntax: Image.resize(size, resample=0)
 Parameters:
 size – The requested size in pixels, as a 2-tuple: (width, height).
 resample – An optional resampling filter. This can be one of PIL. Image.
NEAREST (use nearest neighbour), PIL. Image. ...
 Returns type: An Image object.

Image Used:

Program:
# Importing Image class from PIL module
from PIL import Image
# Opens a image in RGB mode
im = Image.open(r"C:\Users\System-Pc\Desktop\ybear.jpg")
# Size of the image in pixels (size of original image)
# (This is not mandatory)
width, height = im.size
# Setting the points for cropped image
left = 4
top = height / 5
right = 154
bottom = 3 * height / 5
# Cropped image of above dimension
# (It will not change original image)
im1 = im.crop((left, top, right, bottom))
newsize = (300, 300)
im1 = im1.resize(newsize)
# Shows the image in image viewer
im1.show()

Output:

You might also like