0% found this document useful (0 votes)
6 views66 pages

Computer Vision Lab Mannual

The document outlines a series of experiments for a Computer Vision lab at Sagar Institute of Research & Technology, focusing on various image processing techniques including reading images, performing arithmetic and logical operations, geometric transformations, and edge detection. Each experiment includes specific aims, theoretical background, and coding examples using Python and OpenCV. The document serves as a comprehensive guide for BTech students in the Artificial Intelligence and Machine Learning department to understand and implement fundamental computer vision concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views66 pages

Computer Vision Lab Mannual

The document outlines a series of experiments for a Computer Vision lab at Sagar Institute of Research & Technology, focusing on various image processing techniques including reading images, performing arithmetic and logical operations, geometric transformations, and edge detection. Each experiment includes specific aims, theoretical background, and coding examples using Python and OpenCV. The document serves as a comprehensive guide for BTech students in the Artificial Intelligence and Machine Learning department to understand and implement fundamental computer vision concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.

), India

Department of Artificial Intelligence and Machine Learning

List of Experiment Computer Vision (CV) Lab


AL-701
BTech-VII Semester
1. To read and display the images:
A. To identify and change spatial resolution (Binary, Grayscale and
Color).
B. To identify and its change intensity resolution (Binary, Grayscale and
Color).
C. To identify different channels of the images.
D. To decompose image in different channels (R, G, and B).
2. To study and perform arithmetic operations of images:
A. Addition operation
B. Subtraction operation
C. Multiplication operation
D. Division operation
E. To perform the arithmetic operations in different channels (R-G, B-R,
B-G) of color images.
3. To study and perform logical operations of images:
A. NOT operation
B. AND operation
C. OR operation
D. XOR operation
4. To study and perform geometric operations on images:
A. Resize (Shrink/Zoom) Images
B. Rotation operation
C. Translation operation
D. Shearing operation
5. To study and perform linear neighborhood operations on images:
A. With different convolution kernels.
B. With convolution kernels of different size.
C. With various padding operations (zero, 1’s, line, mirror)
6. To study and perform non-linear neighborhood operations on images:
A. Min
B. Max
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

C. Median
D. With various padding operations (zero, 1’s, line, mirror)
7. To study and perform spatial domain image denoising using:
A. Linear filters for Impulse and Gaussian noise of various densities.
B. Non-linear filters for Impulse and Gaussian noise of various densities.
8. To study and perform Image Binarization and mask formation using different
threshold values. Further application of mask using:
A. Multiplication operation
B. Addition operation
C. AND operation
D. OR operation
9. To study and perform morphological operations on images:
A. Erosion
B. Dilation
C. Opening
E. Closing
10. To study and perform edge detection using:
A. Sobel
B. Prewitt
C. Canny
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Computer Vision (CV) Lab


AL701
Experiment No-1
Aim:
To read and display the images:
i. To identify and change spatial resolution (Binary, Grayscale and Color).
ii. To identify and its change intensity resolution (Binary, Grayscale and Color).
iii. To identify different channels of the images.
To decompose image in different channels (R, G, and B).

Theory:

An image is defined as a two-dimensional function, F(x,y), where x and y are spatial coordinates,
and the amplitude of F at any pair of coordinates (x,y) is called the intensity of that image at
that point. When x,y, and amplitude values of F are finite, we call it a digital image.
In other words, an image can be defined by a two-dimensional array specifically arranged in
rows and columns.

Digital Image is composed of a finite number of elements, each of which elements have a
particular value at a particular [Link] elements are referred to as picture elements,image
elements,and pixels.A Pixel is most widely used to denote the elements of a Digital Image.
Types of an image
1. BINARY IMAGE– The binary image as its name suggests, contain only two pixel
elements i.e 0 & 1,where 0 refers to black and 1 refers to white. This image is also
known as Monochrome.
2. BLACK AND WHITE IMAGE– The image which consist of only black and white
color is called BLACK AND WHITE IMAGE.
3. 8 bit COLOR FORMAT– It is the most famous image [Link] has 256 different
shades of colors in it and commonly known as Grayscale Image. In this format, 0
stands for Black, and 255 stands for white, and 127 stands for gray.
4. 16 bit COLOR FORMAT– It is a color image format. It has 65,536 different
colors in [Link] is also known as High Color Format. In this format the distribution of
color is not as same as Grayscale image.
A 16 bit format is actually divided into three further formats which are Red, Green and Blue.
That famous RGB format.
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Image as a Matrix
As we know, images are represented in rows and columns we have the following syntax in which
images are represented:

The right side of this equation is digital image by definition. Every element of this matrix is
called image element, picture element, or pixel.

Code

#!pip install opencv-python#


import cv2

install opencv first

img1 = [Link]("/content/[Link]")
from [Link] import cv2_imshow
cv2_imshow(img1)

import [Link] as plt


[Link](img1)
[Link]("off")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

[Link]
H = 225
W = 225

## resizing of image ###


img4 = [Link] (img1, (100, 100))
img5 = [Link](img1, (50,50))

fig = [Link](figsize=(10, 7))


fig.add_subplot(1, 3, 1)
[Link](img1)
[Link]("off")
[Link]("Original Image")

fig.add_subplot(1, 3, 2)
[Link](img4)
[Link]("off")
[Link]("Size=100X100")

fig.add_subplot(1, 3, 3)
[Link](img5)
[Link]("off")
[Link]("Size=50X50")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

img6 = img1[:, :, 0]
img7 = img1[:, :, 1]
img8 = img1[:, :, 2]

fig = [Link](figsize=(10, 7))


fig.add_subplot (1, 3, 1)
[Link]("off")
[Link](img6)
[Link]("R_Gray")

fig.add_subplot (1, 3, 2)
[Link]("off")
[Link](img7)
[Link]("G_Gray")

fig.add_subplot (1, 3, 3)
[Link]("off")
[Link](img8)
[Link]("B_Gray")

fig=[Link](figsize=(10, 7))
fig.add_subplot (1, 3, 1)
[Link]("off")
[Link](img1[:, :, 0], cmap='Reds', vmin=0, vmax=255)
[Link]("Red_Channel")

fig.add_subplot (1, 3, 2)
[Link]("off")
[Link](img1[:, :, 1], cmap='Greens', vmin=0, vmax=255)
[Link]("Green_Channel")

fig.add_subplot (1, 3, 3)
[Link]("off")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

[Link](img1[:, :, 2], cmap='Blues', vmin=0, vmax=255)


[Link]("Blue_Channel")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Computer Vision (CV) Lab


Experiment No-2
Aim:
To study and perform arithmetic operations of images:
i. Addition operation
ii. Subtraction operation
iii. Multiplication operation
iv. Division operation
v. To perform the arithmetic operations in different channels (R-G, B-R, B-G) of color
images.

Theory:

The important requirement in image arithmetic is that all (input and output) the images are of the
same size MxM.

Arithmetic operations are done pixelwise. Let p = A(x,y) and q = B(x,y) be the pixel values to be
operated on and r =I(x,y) be the result of the operation.

Addition :

I(x,y) = A(x,y) + B(x,y) → r = p + q

Subtraction :

I(x,y) = A(x,y) - B(x,y) → r = p - q

Difference :

I(x,y) = |A(x,y) - B(x,y)| → r = |p - q|

Multiplication :

I(x,y) = A(x,y) X B(x,y) → r = p x q

Division :

I(x,y) = A(x,y) / B(x,y) → r = p / q

Implementation issues:
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Digital images are stored as b - bit images. Hence, the range of values a pixel can take is restricted
to the range [ 0, 1,... (2b -1)]. With b= 8 this range is [0,1,...255]. The closed interval poses a
problem when performing arithmetic operations in practice, as the results are not guaranteed to be
within this interval.

Coding

import cv2
img1=[Link]("/content/[Link]")
img2=[Link]("/content/[Link]")

First off all know the shape of an img1 and img2 to perform the
arithmatic operation

[Link]

[Link]

from [Link] import cv2_imshow


# google colab is not supporting [Link] #
# so at first we have to import cv2_imshow from google colab as above
#
cv2_imshow(img1)

Because of large size img2 is resized according to the size of img1

img2 = [Link](img2, (225, 225))


[Link]
cv2_imshow(img2)
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Perform Addition operation img3

import numpy as np
img3 = [Link] (img1, img2)

## plot img3 using matplotlib package ##


import [Link] as plt
[Link](img3)

## To remove axis in the image plot use [Link]("off") ##


## For showing the title use [Link]("title")
[Link](img3)
[Link]("off")
[Link]("Addition")

## perform Subtraction Operation ##


img4 = [Link](img1, img2)

## Plot img4 ##
[Link](img4)
[Link]("off")
[Link]("Subtraction")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

## perform Multiplication Operation ##


img5 = [Link](img1, img2)

## Plot img4 ##
[Link](img5)
[Link]("off")
[Link]("Multiplication")

## perform Division Operation ##


img6 = [Link](img1, img2)

## Plot img4 ##
[Link](img6)
[Link]("off")
[Link]("Division")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Plot multiple subfigures or images in one frame (figure)

fig = [Link](figsize=(10, 7))


fig.add_subplot(2, 3, 1)
[Link](img1)
[Link]('off')
[Link]("Babbon")

fig.add_subplot(2, 3, 2)
[Link](img2)
[Link]('off')
[Link]("Cameraman")

fig.add_subplot(2, 3, 3)
[Link](img3)
[Link]('off')
[Link]("Addition Result")

fig.add_subplot(2, 3, 4)
[Link](img4)
[Link]('off')
[Link]("Subtraction Result")

fig.add_subplot(2, 3, 5)
[Link](img5)
[Link]('off')
[Link]("Multiplication Result")

fig.add_subplot(2, 3, 6)
[Link](img6)
[Link]('off')
[Link]("Division Result")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning


Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Computer Vision (CV) Lab


Experiment No-3
Aim:
To study and perform logical operations of images:
A. NOT operation
B. AND operation
C. OR operation
D. XOR operation

Theory:

Bitwise Operations

Bitwise operations are used in image manipulation to extract important parts. The following

Bitwise operations are used in this article:

1. AND

2. OR

3. NOT

4. XR

Bitwise operations are also useful for image masking. These operations can be used to
enable image creation. These operations can help to improve the properties of the input
images.
NOTE: Bitwise operations should only be performed on input images of the same
dimensions.

AND Bitwise Operation of Image


Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

The AND operator (and the NAND operator in a similar fashion) typically takes two binary
or integer graylevel images as input and produces a third image whose pixel values are just
those of the first image ANDed with the corresponding pixels from the secon d. This
operator can be modified to produce the output by taking a single input image and ANDing
each pixel with a predetermined constant value.

Syntax: cv2.bitwise_and(Image1, Image2, destination, mask)

Parameters:

1. Image1: First Input Image numpy array


2. Image1: Second Input Image numpy array
3. destination: Output array
4. mask: Operation mask image

Code :

import cv2
import numpy as np
img1 = [Link]('[Link]')
img2 = [Link]('[Link]')
dest_and = cv2.bitwise_and(img2, img1, mask = None)
[Link]('Bitwise And', dest_and)
[Link](0)

OR Bitwise Operation of Image

The OR operator typically takes two binary or greyscale images as input and outputs a third
image whose pixel values are the first image’s pixel values ORed with the corresponding
pixels from the second. A variant of this operator takes a single input image and O Rs each
pixel with a constant value to generate the output.
Syntax: cv2.bitwise_or(source1, source2, destination, mask)

Parameters:

1. source1: First Input numpy Image array


Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

2. source2: Second Input numpy Image array


3. destination: Output array image
4. mask: Operation mask, input / output 8-bit single-channel mask.

Code :

import cv2
import numpy as np
img1 = [Link]('[Link]')
img2 = [Link]('[Link]')
dest_or = cv2.bitwise_or(img1, img2, mask = None)
[Link]('Bitwise OR', dest_or)
[Link](0)

NOT Bitwise Operation of Image

Logical NOT, also known as invert, is an operator that takes a binary or grayscale image as
input and generates its photographic negative.

Syntax: cv2.bitwise_not(Image1,Destination, mask)

Parameters:

1. Image1: Input Image array.


2. Destination: Output array image
3. mask: Operation mask

Code:

import cv2
import numpy as np
img1 = [Link]('[Link]')
dest_not = cv2.bitwise_not(img1, mask = None)
[Link]('Bitwise Not', dest_not)
[Link](0)

XOR Bitwise Operation of Image

The operation is carried out simply and in a single pass. It is critical that all of the input
pixel values being processed have the same number of bits, or else unexpected results may
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

occur. When the pixel values in the input images are not simple 1-bit numbers, the XOR
operation is typically (but not always) performed bitwise on each corresponding bit in the
pixel values.
Syntax: cv2.bitwise_xor(source1, source2, destination, mask)

Parameters:

1. source1: First Input Image array (Single-channel, 8-bit or floating-point)


2. source2: Second Input Image array (Single-channel, 8-bit or floating-point)
3. destination: Output image array
4. mask: Operation mask, input/ output 8-bit single-channel mask.

Code:

import cv2
import numpy as np
img1 = [Link]('[Link]')
img2 = [Link]('[Link]')
dest_or = cv2.bitwise_xor(img1, img2, mask = None)
[Link]('Bitwise XOR', dest_xor)
[Link](0)
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Note

Many applications use processed images taken from the same scene at different points, such
as noise reduction by adding successive images of the same scene or motion detection by
subtracting two successive images. Logical operators are frequently used to combine two
(mostly binary) images. In the case of integer images, the logical operator is typically used
bitwise. Then, for example, we can use a binary mask to select a specific region of an
image.
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Computer Vision (CV) Lab


(AL701)
Experiment No-4
Aim:
To study and perform geometric operations on images:

A. Rotation operation
B. Translation operation
C. Shearing operation

Theory:

(i) Translation
Image translation refers to the rectilinear shift of an object i.e. an image from one location to
another. If we know the amount of shift in horizontal and the vertical direction, say (t x, ty) then
we can make a transformation matrix e.g.

where tx denotes the shift along the x-axis and ty denotes shift along the y-axis i.e. the number
of pixels by which we need to shift about in that direction.
Now, we can use the [Link]() function to implement these translations. This function
requires a 2×3 array. The Numpy array should be of float type.
import cv2
import numpy as np
import [Link] as plt
img1 = [Link]('/content/[Link]')
# Store height and width of the image
height, width = [Link][:2]
quarter_height, quarter_width = height / 4, width / 4
T = np.float32([[1, 0, quarter_width], [0, 1, quarter_height]])
# We use warpAffine to transform
# the image using the matrix, T
img_translation = [Link](img1, T, (width, height))
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

fig=[Link](figsize=(10, 3))
fig.add_subplot(1,2,1)
[Link](img1)
[Link]("off")
[Link]("Original image")

fig.add_subplot(1,2,2)
[Link](img_translation)
[Link]("off")
[Link]("Translated Image")

Advantages/application of image translation are:

• Hiding a part of the image


• Cropping an image
• Shifting an image
• Animating an image using image translations in loop.
(ii) Rotation
Images can be rotated to any degree clockwise or otherwise. We just need to define rotation
matrix listing rotation point, degree of rotation and the scaling factor.
• The cv2.getRotationMatrix2D() function is used to create a rotation matrix for an
image. It takes the following arguments:
• The center of rotation for the image.
• The angle of rotation in degrees.
• The scale factor.
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

• The [Link]() function is used to apply a transformation matrix to an


image. It takes the following arguments:
• The python image to be transformed.
• The transformation matrix.
• The output image size.
• The rotation angle can be positive or negative. A positive angle rotates the image
clockwise, while a negative angle rotates the image counterclockwise.
• The scale factor can be used to scale the image up or down. A scale factor of 1
will keep the image the same size, while a scale factor of 2 will double the size of
the python image.
# Import the necessary Libraries
import cv2
import numpy as np
import [Link] as plt
img2 = [Link]('/content/[Link]')
rows, cols = [Link][:2]
M = np.float32([[1, 0, 0], [0, -1, rows], [0, 0, 1]])
img_rotation = [Link](img2,
cv2.getRotationMatrix2D((cols/2, rows/2),
30, 0.6), (cols,
rows))
# Create subplots

fig=[Link](figsize=(10, 3))
fig.add_subplot(1,2,1)
[Link](img2)
[Link]("off")
[Link]("Original image")

fig.add_subplot(1,2,2)
[Link](img_rotation)
[Link]("off")
[Link]("Rotated Image")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

(iii) Image Shearing


The shear() function is an inbuilt function in the Python Wand Image Magick library which is
used to slide one edge of an image along the X or Y axis to create a parallelogram. The X-direction
shear slides an edge along the X-axis, while a Y direction shear slides an edge along the Y-axis.
The shear angle is used to set shear of the image.

Shearing deals with changing the shape and size of the 2D object along x-axis and y-axis. It is
similar to sliding the layers in one direction to change the shape of the 2D [Link] is an ideal
technique to change the shape of an existing object in a two dimensional plane. In a two-
dimensional plane, the object size can be changed along X direction as well as Y direction.
x-Shear: In x shear, the y co-ordinates remain the same but the x co-ordinates changes. If P (x,
y) is the point then the new points will be P’(x’, y’) given as –
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Matrix Form:

y-Shear:
In y shear, the x co-ordinates remain the same but the y co-ordinates changes. If P(x, y) is the
point then the new points will be P’(x’, y’) given as –

Matrix Form:

x-y Shear:
In x-y shear, both the x and y co-ordinates changes. If P(x, y) is the point then the new points
will be P’(x’, y’) given as –

Matrix Form:

Example:
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Given a triangle with points (1, 1), (0, 0) and (1, 0). Find out the new coordinates of the object
along x-axis, y-axis, xy-axis. (Applying shear parameter 4 on X-axis and 1 on Y-axis.).

Given,
Old corner coordinates of the triangle = A (1, 1), B(0, 0),
C(1, 0)
Shearing parameter along X-axis (Shx) = 4
Shearing parameter along Y-axis (Shy) = 1

Along x-axis:
A'=(1+4*1, 1)=(5, 1)
B'=(0+4*0, 0)=(0, 0)
C'=(1+4*0, 0)=(1, 0)

Along y-axis:
A''=(1, 1+1*1)=(1, 2)
B''=(0, 0+1*0)=(0, 0)
C''=(1, 0+1*1)=(1, 1)

Along xy-axis:
A'''=(1+4*1, 1+1*1)=(5, 2)
B'''=(0+4*0, 0+1*0)=(0, 0)
C'''=(1+4*0, 0+1*1)=(1, 1)

########### Python Code ####################

import numpy as np
import [Link] as plt
import cv2
img = [Link]('/content/[Link]')
rows, cols = [Link][:2]
M = np.float32([[1, 0.5, 0], [0, 1, 0], [0, 0, 1]])
sheared_img = [Link](img, M, (int(cols*1.5), int(rows*1.5)))
from [Link] import cv2_imshow
fig=[Link](figsize=(10, 3))
fig.add_subplot(1,2,1)
[Link](img)
[Link]("off")
[Link]("Original image")

fig.add_subplot(1,2,2)
[Link](sheared_img)
[Link]("off")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

[Link]("Sheared image")

Theory:
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Computer Vision (CV) Lab


AL701
Experiment No-5

Aim:
To study and perform linear neighborhood operations on images:
A. With different convolution kernels.
B. With convolution kernels of different size.
C. With various padding operations (zero, 1’s, line, mirror)

Neighborhood operations are a generalization of the point operations. A pixel in the processed
image now depends not only on the corresponding pixel in the input image but also its neighboring
pixels. This generalization also allows for defining linear as well nonlinear filtering operations.

Convolution Operation
Given an input image 𝑓(𝑥, 𝑦) an output image 𝑔(𝑥, 𝑦) is computed by applying some operation
on a local neighbourhood 𝑁 of each pixel in the image f. This can be visualized as follows: a
window or mask is placed at every pixel location in 𝑓(𝑥, 𝑦) and some operation is performed on
the pixels within the window. The window is moved to the next pixel location and the process is

repeated. Thus,
Where 𝐻𝑁 is the neighborhood operator of size N and g is the output image.
Linear operations:

Linear operations can be represented as a convolution operation between 𝑓(𝑥, 𝑦) and a window
function 𝑤(𝑥, 𝑦) as follows.

w is a window function which in practice is in the form of a matrix of size 𝑠 × 𝑡; 𝑎 = (𝑠 −


1)/2 𝑎𝑛𝑑 𝑏 = (𝑡 − 1)/2. An example of 3x3 𝑤(𝑥, 𝑦) is shown below.
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

An example of convolution operation is shown below:


Note that 𝑤(0,0) is the coefficient or weight of the window function at the centre which is the
position of the current pixel in 𝑓(𝑥, 𝑦). Varying the weights 𝑤(𝑖, 𝑗), results in different type of
linear neighborhood operations. The above operations are generally termed as filtering operations
and w is referred to as a filter.

A popular filter is one which performs local averaging or smoothing of the image. This is a low
pass filter.
Table of various popular kernels
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

The key characteristic of this filter is that 𝑤(𝑖, 𝑗) > 0 for every (𝑖, 𝑗). An additional constraint is
generally imposed on the weights to sum to 1. Below are some examples.
Code:
################ Convolution Operation in an Image ####################
img4 = [Link]("/content/[Link]")
img4 = [Link](img4, (224,224))
img4 = [Link](img4, cv2.COLOR_BGR2RGB)
from PIL import Image
import numpy as np
def apply_convolution(img:[Link], kernel:[Link]):
# Get the height, width, and number of channels of the image
height,width,c =[Link][0],[Link][1],[Link][2]
# Get the height, width, and number of channels of the kernel
kernel_height,kernel_width = [Link][0],[Link][1]
# Create a new image of original img size minus the border
# where the convolution can't be applied
new_img = [Link]((height-kernel_height+1,width-kernel_width+1,3))
# Loop through each pixel in the image
# But skip the outer edges of the image
for i in range(kernel_height//2, height-kernel_height//2-1):
for j in range(kernel_width//2, width-kernel_width//2-1):
# Extract a window of pixels around the current pixel
window = img[i-kernel_height//2 : i+kernel_height//2+1,j-
kernel_width//2 : j+kernel_width//2+1]
# Apply the convolution to the window and set the result as the value of
the current pixel in the new image
new_img[i, j, 0] = int((window[:,:,0] * kernel).sum())
new_img[i, j, 1] = int((window[:,:,1] * kernel).sum())
new_img[i, j, 2] = int((window[:,:,2] * kernel).sum())
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

# Clip values to the range 0-255


new_img = [Link](new_img, 0, 255)
return new_img.astype(np.uint8)
if __name__ == "__main__":
# kernel for edge detection
kernel = [Link]([[-1,-1,-1], [-1,8,-1], [-1,-1,-1]])
# kernel for vertical edge detection
#kernel = [Link]([[-1,0,1],[-1,0,1],[-1,0,1]])
# kernel for horizontal edge detection
# kernel = [Link]([[-1,-1,-1],[0,0,0],[1,1,1]])
# Kernel for box blur
# kernel = [Link]([[1/9,1/9,1/9],[1/9,1/9,1/9],[1/9,1/9,1/9]])
# Open the image and convert it to an array
# Try to put your own picture!
img = [Link]('/content/[Link]')
or_img = [Link](img)
new_img = apply_convolution(img4, kernel)

fig = [Link](figsize=(6, 4))


fig.add_subplot(1, 2, 1)
[Link](img4)
[Link]("off")
[Link]("Original Image")

fig.add_subplot(1, 2, 2)
[Link](new_img)
[Link]("off")
[Link]("Convoluted Image")

For different kernels convolution operation has to be performed


Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Image padding:
Image padding is an essential technique in image processing which is used to
maintain data consistency at the edges of an image. It involves adding layers of pixels to the
image, ensuring uniform processing when applying filters and other operations. This
prevents information loss and supports the desired output dimensions, making it essential
for precise image analysis and enhancement.

Zero padding

Zero padding adds black borders around the image; black borders are achieved by adding pixels
with intensity value 0 around the image. The thickness of the borders depends on the type of
filter kernel used.

Zero padding with a one-pixel thick boundary


It’s the simplest form of padding and is often used when we want to maintain the original size of
the data while adding a border of uniform values. It defines a function zero_padding that adds
zero padding to an input array based on the specified padding size. In this case, it applies zero
padding with a size of 1 to input_array and prints the resulting array with zeros added around its
edges.
######################## Zero Padding Operation ######################
######################## Import libraries ##########################
import cv2
import [Link] as plt
import numpy as np
################## Zero padding in a 2-D array #######################
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

image_array = [Link]([[1, 2, 3, 4, 5],


[6, 7, 0, 1, 2],
[3, 4, 5, 6, 7],
[0, 1, 2, 3, 4],
[5, 6, 7, 0, 1]])
def zero_padding(arr, padding_size):
return [Link](arr, pad_width=padding_size, mode='constant',
constant_values=(0))
# Apply zero padding with a padding size of 1
zero_padded_arr = zero_padding(image_array, 1)
print("\nZero Padded Array:")
print(zero_padded_arr)

Zero Padded Array:


[[0 0 0 0 0 0 0]
[0 1 2 3 4 5 0]
[0 6 7 0 1 2 0]
[0 3 4 5 6 7 0]
[0 0 1 2 3 4 0]
[0 5 6 7 0 1 0]
[0 0 0 0 0 0 0]]
######################## Zero Padding in an Image ######################
# read image
img1 = [Link]('/content/[Link]')
img1 = [Link](img1, (224, 224))
# Make black border
Zero_Padded_img = [Link](img1, 20, 20, 20, 20,
cv2.BORDER_CONSTANT, None, value = 0)

fig = [Link](figsize=(6, 4))


fig.add_subplot(1, 2, 1)
[Link](img1)
[Link]("off")
[Link]("Original Image")

fig.add_subplot(1, 2, 2)
[Link](Zero_Padded_img)
[Link]("off")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

[Link]("Zero_Padded Image")

Mirror padding

Mirror padding, also known as symmetric padding or reflective padding, adds a boundary around
the image by mirror-reflecting the image on the original image border. The thickness of the
boundary can be adjusted. The pixels near the vertical and horizontal edges act as the line of
reflection that adds new boundary pixels directly above/below and left/right of the image. For the
remaining boundary pixels, the corner pixels act as the line of reflection.
Note: The boundary pixel values become the edge over which the values are reflected to get the
padding boundary values.

Mirror padding is applicable when the areas near the border contain important image details, and
we want to extend those details. It reflects or mirrors the values at the edges to fill the padded
regions. This helps in maintaining the continuity of patterns and textures at the borders. Mirror
padding is useful when the border regions have intricate patterns or features that we want to extend
without creating abrupt changes or artifacts.
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

################## Mirror padding in 2-D array#######################


import numpy as np
image_array = [Link]([[1, 2, 5, 9, 6],
[4, 7, 0, 1, 2],
[3, 4, 5, 6, 7],
[0, 1, 2, 3, 4]])

def mirror_padding(arr, padding_size):


return [Link](arr, pad_width=padding_size, mode='reflect')
# Apply mirror padding with a padding size of 1
mirror_padded_arr = mirror_padding(image_array, 2)
print("\nMirror Padded Array:")
print(mirror_padded_arr)

Mirror Padded Array:


[[5 4 3 4 5 6 7 6 5]
[0 7 4 7 0 1 2 1 0]
[5 2 1 2 5 9 6 9 5]
[0 7 4 7 0 1 2 1 0]
[5 4 3 4 5 6 7 6 5]
[2 1 0 1 2 3 4 3 2]
[5 4 3 4 5 6 7 6 5]
[0 7 4 7 0 1 2 1 0]]

######################## Mirror Padding in an Image ######################


# read image
img1 = [Link]('/content/[Link]')
img1 = [Link](img1, (224, 224))
# Make black border
Mirror_Pad_img = [Link](img1, 20, 20, 20, 20,
cv2.BORDER_REFLECT)

fig = [Link](figsize=(6, 4))


fig.add_subplot(1, 2, 1)
[Link](img1)
[Link]("off")
[Link]("Original Image")

fig.add_subplot(1, 2, 2)
[Link](Mirror_Pad_img)
[Link]("off")
[Link]("Mirror_Padded Image")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Replicate padding:
Replicate padding adds the closest values outside the boundary, ensuring that values outside the
boundary are set equal to the nearest image border value. Replicate padding is used when we want
to preserve the values at the edges of our data while extending it. It effectively replicates the values
of the nearest edge pixel to fill the padded regions. This type of padding is suitable when the areas
near the border of the image or data have a relatively constant or uniform pattern that we want to
maintain. For example, if we have an image with a solid colour border, replicate padding would
be a good choice to extend that solid colour to the padded regions, preserving the appearance of a
continuous border.

################## Replicate padding in 2-D array #######################


import numpy as np
image_array = [Link]([[1, 2, 3, 4, 5],
[6, 7, 0, 1, 2],
[3, 4, 5, 6, 7],
[0, 1, 2, 3, 4]])
def replicate_padding(arr, padding_size):
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

return [Link](arr, pad_width=padding_size, mode='edge')


# Apply replicate padding with a padding size of 1
replicate_padded_arr = replicate_padding(image_array, 1)
print("\nReplicate Padded Array:")
print(replicate_padded_arr)

Replicate Padded Array:


[[1 1 2 3 4 5 5]
[1 1 2 3 4 5 5]
[6 6 7 0 1 2 2]
[3 3 4 5 6 7 7]
[0 0 1 2 3 4 4]
[0 0 1 2 3 4 4]]

######################## Replicate Padding in an Image


######################
# read image
img1 = [Link]('/content/[Link]')
img1 = [Link](img1, (224, 224))
# Make black border
Repli_pad_img = [Link](img1, 10, 10, 10, 10,
cv2.BORDER_REPLICATE)

fig = [Link](figsize=(6, 4))


fig.add_subplot(1, 2, 1)
[Link](img1)
[Link]("off")
[Link]("Original Image")

fig.add_subplot(1, 2, 2)
[Link](Repli_pad_img)
[Link]("off")
[Link]("Replicate_Padded Image")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

*************************************
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Computer Vision (CV) Lab


AL 701
Experiment No-6

Aim: To study and perform non-linear neighborhood operations on images:


(A)Min
(B) Max
(C) Median
(D)With various padding operations (zero, 1’s, line, mirror)

Theory:

Non-Linear Filter

Using some non-linear function from the source pixel value. The idea is to replace the target
pixel value with its neighbour pixels value from some ordering mechanism or function.
There are many types of Non-Linear Filter but in this article, I will show you just 3 of them
• Minimum Filter
• Maximum Filter
• Median Filter

Minimum and maximum are the simplest non-linear operators. The former scans each pixel in an
image and replaces it with the lowest ranking value in its neighborhood. Maximum does the same,
except that the replacing value is the highest-ranking pixel in the neighborhood.

Minimum Filter
This algorithm is to select the lowest pixel value from the neighbours’ pixels around the target
then replace it.

The Procedure of minimum filter:


▪ The window is overlaid on the upper left corner of the image, and the minimum value is
determined by sorting the pixels values (ascending order).
▪ This value (minimum) is put into the output image corresponding to the center location of
the window.
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

▪ The window is then sliding one pixel over, and the process is repeated when the end of the
row is reached, the window is slide back to the left side of the image and down one row,
and the process is repeated.
▪ This process continues until the entire image has been processed.

Note:
the outer rows and columns are not replaced. And these “wasted” rows and columns are often filled
with zeros (or cropped off the image). For example, with 3X3 mask, we lose one outer row and
column, a 5X5 mask we lose two rows and columns

############# Import Important Libraries #######


from PIL import Image, ImageFilter
import [Link] as plt

#### read image using pillow library


img1 = [Link]("/content/[Link]")
#### resize image to 224x224
img1 = [Link]((224, 224))

#### Apply min filtering


new_image1 = [Link]([Link](size = 3))

#### Plot original image and min filtering


fig = [Link](figsize=(6, 4))
fig.add_subplot(1, 2, 1)
[Link](img1)
[Link]("off")
[Link]("Original Image")

fig.add_subplot(1, 2, 2)
[Link](new_image1)
[Link]("off")
[Link]("Min Filtered Image")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

############## Minimum filtering operation with different padding ###################


############# Import Important Libraries #######
import cv2
from PIL import Image, ImageFilter
import [Link] as plt

#### read image using pillow library


img1 = [Link]("/content/[Link]")
img1 = [Link](img1, cv2.COLOR_BGR2RGB)
#### resize image to 224x224
img1 = [Link](img1, (224, 224))

######### apply padding (Zero padding, Mirror padding, and Replicate


paddding)
img1_pad1 = [Link](img1,20, 20, 20, 20, cv2.BORDER_CONSTANT,
None, value=0)
img1_pad2 = [Link](img1,20, 20, 20, 20, cv2.BORDER_REFLECT)
img1_pad3 = [Link](img1,20, 20, 20, 20, cv2.BORDER_REPLICATE)

########### converet array as image for the respective padded images


img1_new1 = [Link](img1_pad1)
img1_new2 = [Link](img1_pad2)
img1_new3 = [Link](img1_pad3)

### apply min filtering


result1 = img1_new1.filter([Link](size = 3))
result2 = img1_new2.filter([Link](size = 3))
result3 = img1_new3.filter([Link](size = 3))
#### Plot all the results
fig = [Link](figsize=(15, 10))
fig.add_subplot(3, 3, 1)
[Link](img1)
[Link]("off")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

[Link]("Original Image")

fig.add_subplot(3, 3, 2)
[Link](img1_pad1)
[Link]("off")
[Link]("Zero Padded Image")

fig.add_subplot(3, 3, 3)
[Link](img1_pad2)
[Link]("off")
[Link]("Mirror Padded Image")

fig.add_subplot(3, 3, 4)
[Link](img1_pad3)
[Link]("off")
[Link]("Replicate Padded Image")

fig.add_subplot(3, 3, 5)
[Link](result1)
[Link]("off")
[Link]("Zero padded with Min Filtering Image")

fig.add_subplot(3, 3, 6)
[Link](result2)
[Link]("off")
[Link]("Mirror padded with Min Filtering Image")

fig.add_subplot(3, 3, 7)
[Link](result3)
[Link]("off")
[Link]("Replicate padded with Min Filtering Image")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Maximum Filter
This algorithm also similar to minimum filter but pick the highest one.
The Procedure of minimum filter:
▪ The window is overlaid on the upper left corner of the image, and the maximum value is
determined by sorting the pixels values (ascending order).
▪ This value (maximum) is put into the output image corresponding to the center location of
the window.
▪ The window is then sliding one pixel over, and the process is repeated when the end of the
row is reached, the window is slide back to the left side of the image and down one row,
and the process is repeated.
▪ This process continues until the entire image has been processed.
############## Maximum filtering operation with different padding ###################
#### import the necesarry libraries #######

import [Link] as plt


from PIL import Image, ImageFilter
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

######## Read Image #####


# creating a image object
img1 = [Link](r"/content/[Link]")
img1 = [Link]((224, 224))

new_image = [Link]([Link](size = 3))

fig = [Link](figsize=(6, 4))


fig.add_subplot(1, 2, 1)
[Link](img1)
[Link]("off")
[Link]("Original Image")

fig.add_subplot(1, 2, 2)
[Link](new_image)
[Link]("off")
[Link]("Max Filtered Image")

############## Maximum filtering operation with different padding ###################


############# Import Important Libraries #######
import cv2
from PIL import Image, ImageFilter
import [Link] as plt

#### read image using pillow library


img1 = [Link]("/content/[Link]")
img1 = [Link](img1, cv2.COLOR_BGR2RGB)
#### resize image to 224x224
img1 = [Link](img1, (224, 224))
######### apply padding (Zero padding, Mirror padding, and Replicate
paddding)
img1_pad1 = [Link](img1,20, 20, 20, 20, cv2.BORDER_CONSTANT,
None, value=0)
img1_pad2 = [Link](img1,20, 20, 20, 20, cv2.BORDER_REFLECT)
img1_pad3 = [Link](img1,20, 20, 20, 20, cv2.BORDER_REPLICATE)
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

########### converet array as image for the respective padded images


img1_new1 = [Link](img1_pad1)
img1_new2 = [Link](img1_pad2)
img1_new3 = [Link](img1_pad3)

### apply min filtering


result1 = img1_new1.filter([Link](size = 3))
result2 = img1_new2.filter([Link](size = 3))
result3 = img1_new3.filter([Link](size = 3))

#### Plot all the results


fig = [Link](figsize=(15, 10))
fig.add_subplot(3, 3, 1)
[Link](img1)
[Link]("off")
[Link]("Original Image")

fig.add_subplot(3, 3, 2)
[Link](img1_pad1)
[Link]("off")
[Link]("Zero Padded Image")

fig.add_subplot(3, 3, 3)
[Link](img1_pad2)
[Link]("off")
[Link]("Mirror Padded Image")

fig.add_subplot(3, 3, 4)
[Link](img1_pad3)
[Link]("off")
[Link]("Replicate Padded Image")

fig.add_subplot(3, 3, 5)
[Link](result1)
[Link]("off")
[Link]("Zero padded with Max Filtering Image")

fig.add_subplot(3, 3, 6)
[Link](result2)
[Link]("off")
[Link]("Mirror padded with Max Filtering Image")

fig.add_subplot(3, 3, 7)
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

[Link](result3)
[Link]("off")
[Link]("Replicate padded with Max Filtering Image")

Median Filter

The median filter is actually a specific form of a rank filter, where the ith pixel intensity in the
sorted list of neighborhood pixels is chosen as the output.

The Procedure of median filter:

▪ The window is overlaid on the upper left corner of the image, and the median value is
determined.
▪ This value (median) is put into the output image (buffer) corresponding to the center
location of the window.
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

▪ The window is then sliding one pixel over, and the process is repeated when the end of the
row is reached, the window is slide back to the left side of the image and down one row,
and the process is repeated.
▪ This process continues until the entire image has been processed.

For more clarity of the advantage of this filter, here is the example that used in salt and pepper
pictures.

###### Median Filtering


#### import the necesarry libraries #######

import [Link] as plt


from PIL import Image, ImageFilter
######## Read Image #####
# creating a image object
img1 = [Link](r"/content/[Link]")
img1 = [Link]((224, 224))

new_image = [Link]([Link](size = 3))

fig = [Link](figsize=(6, 4))


fig.add_subplot(1, 2, 1)
[Link](img1)
[Link]("off")
[Link]("Original Image")

fig.add_subplot(1, 2, 2)
[Link](new_image)
[Link]("off")
[Link]("Median Filtered Image")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

########################## Median filtering with different paddings #################


############# Import Important Libraries #######
import cv2
from PIL import Image, ImageFilter
import [Link] as plt

#### read image using pillow library


img1 = [Link]("/content/[Link]")
img1 = [Link](img1, cv2.COLOR_BGR2RGB)
#### resize image to 224x224
img1 = [Link](img1, (224, 224))

######### apply padding (Zero padding, Mirror padding, and Replicate


paddding)
img1_pad1 = [Link](img1,20, 20, 20, 20, cv2.BORDER_CONSTANT,
None, value=0)
img1_pad2 = [Link](img1,20, 20, 20, 20, cv2.BORDER_REFLECT)
img1_pad3 = [Link](img1,20, 20, 20, 20, cv2.BORDER_REPLICATE)

########### converet array as image for the respective padded images


img1_new1 = [Link](img1_pad1)
img1_new2 = [Link](img1_pad2)
img1_new3 = [Link](img1_pad3)

### apply min filtering


result1 = img1_new1.filter([Link](size = 3))
result2 = img1_new2.filter([Link](size = 3))
result3 = img1_new3.filter([Link](size = 3))

#### Plot all the results

fig = [Link](figsize=(15, 10))


fig.add_subplot(3, 3, 1)
[Link](img1)
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

[Link]("off")
[Link]("Original Image")

fig.add_subplot(3, 3, 2)
[Link](img1_pad1)
[Link]("off")
[Link]("Zero Padded Image")

fig.add_subplot(3, 3, 3)
[Link](img1_pad2)
[Link]("off")
[Link]("Mirror Padded Image")

fig.add_subplot(3, 3, 4)
[Link](img1_pad3)
[Link]("off")
[Link]("Replicate Padded Image")

fig.add_subplot(3, 3, 5)
[Link](result1)
[Link]("off")
[Link]("Zero padded with Median Filtering Image")

fig.add_subplot(3, 3, 6)
[Link](result2)
[Link]("off")
[Link]("Mirror padded with Median Filtering Image")

fig.add_subplot(3, 3, 7)
[Link](result3)
[Link]("off")
[Link]("Replicate padded with Median Filtering Image")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

❖ The median filter is a nonlinear filter (order filter) used to remove noise from images.
❖ The median filter is also used to preserve edge properties while reducing the noise.
❖ These filters are based on as specific type of image statistics called order statistics.
❖ Typically, these filters operate on small sub image, “Window”, and replace the center pixel
value (similar to the convolution process).
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Computer Vision (CV) Lab


Al 701
Experiment No-7

Aim:
To study and perform spatial domain image denoising using:
A. Linear filters for Impulse and Gaussian noise of various densities.
B. Non-linear filters for Impulse and Gaussian noise of various densities.

Theory:

Intensity transformations are applied on images for contrast manipulation or image thresholding.
These are in the spatial domain, i.e. they are performed directly on the pixels of the image at
hand, as opposed to being performed on the Fourier transform of the image. The following are
commonly used intensity transformations:
1. Image Negatives (Linear)
2. Log Transformations
3. Power-Law (Gamma) Transformations
4. Piecewise-Linear Transformation Functions
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Spatial Domain Processes – Spatial domain processes can be described using the equation:g(x,
y) = T[f(x, y)]wheref(x, y)is the input image, T is an operator on f defined over a neighbourhood
of the point (x, y), andg(x, y)is the output.

Image Negatives – Image negatives are discussed in this article. Mathematically, assume that
an image goes from intensity levels 0 to (L-1). Generally, L = 256. Then, the negative
transformation can be described by the expression s = L-1-r where r is the initial intensity level
and s is the final intensity level of a pixel. This produces a photographic negative.

Log Transformations –
Mathematically, log transformations can be expressed as s = clog(1+r). Here, s is the output
intensity, r>=0 is the input intensity of the pixel, and c is a scaling constant. c is given by 255/(log
(1 + m)), where m is the maximum pixel value in the image. It is done to ensure that the final
pixel value does not exceed (L-1), or 255. Practically, log transformation maps a narrow range
of low-intensity input values to a wide range of output values. Consider the following input
image

.
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Below is the code to apply log transformation to the image.

import cv2
import numpy as np

# Open the image.


img = [Link]('[Link]')

# Apply log transform.


c = 255/([Link](1 + [Link](img)))
log_transformed = c * [Link](1 + img)

# Specify the data type.


log_transformed = [Link](log_transformed, dtype = np.uint8)

# Save the output.


[Link]('log_transformed.jpg', log_transformed)

Below is the log-transformed output.

Power-Law (Gamma) Transformation –


Power-law (gamma) transformations can be mathematically expressed as . Gamma
correction is important for displaying images on a screen correctly, to prevent bleaching or
darkening of images when viewed from different types of monitors with different display
settings. This is done because our eyes perceive images in a gamma-shaped curve, whereas
cameras capture images in a linear fashion. Below is the Python code to apply gamma correction.

import cv2
import numpy as np
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

# Open the image.


img = [Link]('[Link]')

# Trying 4 gamma values.


for gamma in [0.1, 0.5, 1.2, 2.2]:

# Apply gamma correction.


gamma_corrected = [Link](255*(img / 255) ** gamma, dtype = 'uint8')

# Save edited images.


[Link]('gamma_transformed'+str(gamma)+'.jpg', gamma_corrected)

Below are the gamma-corrected outputs for different values of gamma. Gamma = 0.1:

Gamma = 0.5:
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Gamma = 1.2:

Gamma = 2.2: As can be observed from the outputs as well as the graph, gamma>1 (indicated
by the curve corresponding to ‘nth power’ label on the graph), the intensity of pixels decreases
i.e. the image becomes darker. On the other hand, gamma<1 (indicated by the curve
corresponding to 'nth root' label on the graph), the intensity increases i.e. the image becomes
lighter.
Piecewise-Linear Transformation Functions –
These functions, as the name suggests, are not entirely linear in nature. However, they are linear
between certain x-intervals. One of the most commonly used piecewise-linear transformation
functions is contrast stretching. Contrast can be defined as:
Contrast = (I_max - I_min)/(I_max + I_min)
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

This process expands the range of intensity levels in an image so that it spans the full intensity
of the camera/display. The figure below shows the graph corresponding to the contrast

stretching.
With (r1, s1), (r2, s2) as parameters, the function stretches the intensity levels by essentially
decreasing the intensity of the dark pixels and increasing the intensity of the light pixels. If r1 =
s1 = 0 and r2 = s2 = L-1, the function becomes a straight dotted line in the graph (which gives
no effect). The function is monotonically increasing so that the order of intensity levels between
pixels is preserved. Below is the Python code to perform contrast stretching.

import cv2
import numpy as np

# Function to map each intensity level to output intensity level.


def pixelVal(pix, r1, s1, r2, s2):
if (0 <= pix and pix <= r1):
return (s1 / r1)*pix
elif (r1 < pix and pix <= r2):
return ((s2 - s1)/(r2 - r1)) * (pix - r1) + s1
else:
return ((255 - s2)/(255 - r2)) * (pix - r2) + s2

# Open the image.


img = [Link]('[Link]')

# Define parameters.
r1 = 70
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

s1 = 0
r2 = 140
s2 = 255

# Vectorize the function to apply it to each value in the Numpy array.


pixelVal_vec = [Link](pixelVal)

# Apply contrast stretching.


contrast_stretched = pixelVal_vec(img, r1, s1, r2, s2)

# Save edited image.


[Link]('contrast_stretch.jpg', contrast_stretched)

Output:
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Computer Vision (CV) Lab


AL 701
Experiment No-8

Aim:
To study and perform Image Binarization and mask formation using different
threshold values. Further application of mask using:
A. Multiplication operation
B. Addition operation
C. AND operation
D. OR operation

Theory:

Thresholding
Thresholding is one of the segmentation techniques that generates a binary image (a binary image
is one whose pixels have only two values – 0 and 1 and thus requires only one bit to store pixel
intensity) from a given grayscale image by separating it into two regions based on a threshold
value. Hence pixels having intensity values greater than the said threshold will be treated as
white or 1 in the output image and the others will be black or 0.
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Suppose the above is the histogram of an image f(x,y). We can see one peak near level 40 and
another at 180. So there are two major groups of pixels – one group consisting of pixels having
a darker shade and the others having a lighter shade. So there can be an object of interest set in
the background. If we use an appropriate threshold value, say 90, will divide the entire image
into two distinct regions.

In other words, if we have a threshold T, then the segmented image g(x,y) is computed as shown
below:

So the output segmented image has only two classes of pixels – one having a value of 1 and
others having a value of 0.

If the threshold T is constant in processing over the entire image region, it is said to be global
thresholding. If T varies over the image region, we say it is variable thresholding.

Multiple-thresholding classifies the image into three regions – like two distinct objects on a
background. The histogram in such cases shows three peaks and two valleys between them. The
segmented image can be completed using two appropriate thresholds T 1 and T2.
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

where a, b and c are three distinct intensity values.

From the above discussion, we may intuitively infer that the success of intensity thresholding is
directly related to the width and depth of the valleys separating the histogram modes. In turn,
the key factors affecting the properties of the valleys are the separation between peaks, the noise
content in the image, and the relative sizes of objects and backgrounds. The more widely the two
peaks in the histogram are separated, the better thresholding and hence image segmenting
algorithms will work. Noise in an image often degrades this widely-separated two-peak
histogram distribution and leads to difficulties in adequate thresholding and segmenting. When
noise is present, it is appropriate to use some filter to clean the image and then apply
segmentation. The relative object sizes play a role in determining the accuracy of segmentation.

Global Thresholding

When the intensity distribution of objects and background are sufficiently distinct, it is possible
to use a single or global threshold applicable over the entire image. The basic global thresholding
algorithm iteratively finds the best threshold value so segmenting.

The algorithm is explained below.

1. Select an initial estimate of the threshold T.


2. Segment the image using T to form two groups G 1 and G2: G1 consists of all pixels
with intensity values > T, and G 2 consists of all pixels with intensity values ≤ T.
3. Compute the average intensity values m 1 and m2 for groups G1 and G2.σ
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

4. Compute the new value of the threshold T as T = (m 1 + m2)/2


5. Repeat steps 2 through 4 until the difference in the subsequent value of T is smaller
than a pre-defined value δ.
6. Segment the image as g(x,y) = 1 if f(x,y) > T and g(x,y) = 0 if f(x,y) ≤ T.
This algorithm works well for images that have a clear valley in their histogram. The larger the
value of δ, the smaller will be the number of iterations. The initial estimate of T can be made
equal to the average pixel intensity of the entire image.

The above simple global thresholding can be made optimum by using Otsu’s method. Otsu’s
method is optimum in the sense that it maximizes the between-class variance. The basic idea is
that well-thresholded classes or groups should be distinct with respect to the intensity values of
their pixels and conversely, a threshold giving the best separation between classes in terms of
their intensity values would be the best or optimum threshold.

Variable Thresholding
There are broadly two different approaches to local thresholding. One approach is to partition
the image into non-overlapping rectangles. Then the techniques of global thresholding or Otsu’s
method are applied to each of the sub-images. Hence in the image partitioning technique, the
methods of global thresholding are applied to each sub-image rectangle by assuming that each
such rectangle is a separate image in itself. This approach is justified when the sub-image
histogram properties are suitable (have two peaks with a wide valley in between) for the
application of thresholding techniques but the entire image histogram is corrupted by noise and
hence is not ideal for global thresholding.

The other approach is to compute a variable threshold at each point from the neighborhood pixel
properties. Let us say that we have a neighborhood S xy of a pixel having coordinates (x,y). If the
mean and standard deviation of pixel intensities in this neighborhood be m xy and σxy , then the
threshold at each point can be computed as:

where a and b are arbitrary constants. The above definition of the variable threshold is just an
example. Other definitions can also be used according to the need.
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

The segmented image is computed as:

Moving averages can also be used as thresholds. This technique of image thresholding is the
most general one and can be applied to widely different cases.
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

Computer Vision (CV) Lab


Experiment No-10

Aim: To study and perform edge detection using:


A. Sobel
B. Prewitt
C. Canny

Theory:

The Sobel operator, sometimes called the Sobel–Feldman operator or Sobel filter, is used
in image processing and computer vision, particularly within edge detection algorithms where it
creates an image emphasising edges. It is named after Irwin Sobel and Gary M. Feldman,
colleagues at the Stanford Artificial Intelligence Laboratory (SAIL). Sobel and Feldman presented
the idea of an "Isotropic 3 × 3 Image Gradient Operator" at a talk at SAIL in 1968.[1] Technically,
it is a discrete differentiation operator, computing an approximation of the gradient of the image
intensity function. At each point in the image, the result of the Sobel–Feldman operator is either
the corresponding gradient vector or the norm of this vector. The Sobel–Feldman operator is based
on convolving the image with a small, separable, and integer-valued filter in the horizontal and
vertical directions and is therefore relatively inexpensive in terms of computations. On the other
hand, the gradient approximation that it produces is relatively crude, in particular for high-
frequency variations in the image.

Edge detection is one of the fundamental image-processing tasks used in various Computer
Vision tasks to identify the boundary or sharp changes in the pixel intensity. It plays a crucial
role in object detection, image segmentation and feature extraction from the image. In Real-time
edge detection, the image frame coming from a live webcam or video is continuously captured
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

and each frame is processed by edge detection algorithms which identify and highlight these
edges continuously.
In this article, we will use the Canny edge detection algorithms of OpenCV to detect the edges
in real-time. It is one of the most widely used edge detection algorithms. Let’s understand the
Canny edge detection in depth.
Canny Edge Detection
Canny Edge Detection is one of the most widely used edge detection algorithms developed by
John F. Canny in 1986. It works in four stages i.e Noise reduction, Finding the intensity gradient,
non-maximum suppression, and Hysteresis thresholding.
Let’s understand each stage one by one:
Step 1: Noise Reduction
First, the noise in the image is reduced by blurring the image. To blur the image, we apply the
Gaussian filter. It applies a weighted average to each pixel, which reduces the sensitivity with
the slight changes in intensity caused by noise. The larger the kernel size, the more significant
the smoothing effect. The formula for a 2D Gaussian kernel is given by:

Step 2: Finding the intensity gradient


In this step, the intensity gradient of the image is found, which helps to locate the areas of sudden
intensity shifts that correspond to edges in the image. The technique, known as Sobel algorithms
are used to compute the first derivative of the image in both the horizontal (Gx) and vertical (Gy)
directions. The horizontal gradient (Gx) and vertical gradient (Gy) are given by the following
convolution operations:

The following equations can be used to calculate the gradient’s intensity (G) and direction
(θ) at each pixel:
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

The gradient direction will be always perpendicular to the edges.

Step 3: Non-maximum Suppression


We use non-maximum suppression to get thin edges. Each pixel is looked at in the gradient
direction to see if there is a local maximum. If it isn’t, the pixel is suppressed (its value is set to
0) because it probably isn’t a component of an edge.
Step 4: Hysteresis Thresholding
Hysteresis thresholding is used to finalize the edges. We set the minVal and maxVal threshold
values. Pixels with gradient magnitudes above maxVal are considered strong edges, while
those below minVal are considered non-edges and discarded. Pixels with magnitudes between
minVal and maxVal are considered weak edges.
To identify the final edges, we follow the strong edges and consider weak edges connected to
them as part of the edge. If a weak edge is not connected to any strong edge, it is discarded as
noise.
Implementations:
Canny Edge Detection

• Python3

import cv2

def canny_edge_detection(frame):
# Convert the frame to grayscale for edge detection
gray = [Link](frame, cv2.COLOR_BGR2GRAY)

# Apply Gaussian blur to reduce noise and smoothen edges


blurred = [Link](src=gray, ksize=(3, 5), sigmaX=0.5)
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

# Perform Canny edge detection


edges = [Link](blurred, 70, 135)

return blurred, edges

Code explantions
The frame is a single image frame (numpy array) on which we will perform edge detection using
the Canny edge detection method.
# Convert the frame to grayscale for edge detection
gray = [Link](frame, cv2.COLOR_BGR2GRAY)
This line converts the input frame from the BGR colour space to grayscale. The Canny edge
detection method works on grayscale images, so we first convert the frame to grayscale
using [Link]() the function.
# Apply Gaussian blur to reduce noise and smoothen edges
blurred = [Link](src=gray, ksize=(3, 5), sigmaX=0.5)
This line applies Gaussian blur to the grayscale image. The [Link]() function helps
to reduce noise and smoothens the edges, which is useful for obtaining better edge detection
results. The (3, 5) argument represents the size of the Gaussian kernel used for blurring
and 0 indicates the standard deviation in the X and Y directions.
# Perform Canny edge detection
edges = [Link](blurred, 70, 135)
This line performs the actual Canny edge detection on the blurred grayscale image.
The [Link]() the function takes three arguments: the input image (blurred), the lower
threshold (70), and the upper threshold (135). Pixels with gradient magnitude below the lower
threshold are considered not edges, and pixels with gradient magnitude above the upper
threshold are considered strong edges. Pixels with gradient magnitude between the two
thresholds are considered weak edges.
Real-Time Edge Detection
• Python3
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

def main():
# Open the default webcam
cap = [Link](0)

while True:
# Read a frame from the webcam
ret, frame = [Link]()
if not ret:
print('Image not captured')
break

# Perform Canny edge detection on the frame


blurred, edges = canny_edge_detection(frame)

# Display the original frame and the edge-detected frame


#[Link]("Original", frame)
[Link]("Blurred", blurred)
[Link]("Edges", edges)

# Exit the loop when 'q' key is pressed


if [Link](1) & 0xFF == ord('q'):
break

# Release the webcam and close the windows


[Link]()
[Link]()

Apply the Real-Time Edge Detection


• Python3

if __name__ == "__main__":
main()

Output:
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India

Department of Artificial Intelligence and Machine Learning

You might also like