Computer Vision Lab Mannual
Computer Vision Lab Mannual
), India
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
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
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
img1 = [Link]("/content/[Link]")
from [Link] import cv2_imshow
cv2_imshow(img1)
[Link]
H = 225
W = 225
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
img6 = img1[:, :, 0]
img7 = img1[:, :, 1]
img8 = img1[:, :, 2]
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
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 :
Subtraction :
Difference :
Multiplication :
Division :
Implementation issues:
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India
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]
import numpy as np
img3 = [Link] (img1, img2)
## Plot img4 ##
[Link](img4)
[Link]("off")
[Link]("Subtraction")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India
## Plot img4 ##
[Link](img5)
[Link]("off")
[Link]("Multiplication")
## Plot img4 ##
[Link](img6)
[Link]("off")
[Link]("Division")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India
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
Theory:
Bitwise Operations
Bitwise operations are used in image manipulation to extract important parts. The following
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.
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.
Parameters:
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)
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:
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)
Logical NOT, also known as invert, is an operator that takes a binary or grayscale image as
input and generates its photographic negative.
Parameters:
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)
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
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:
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
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
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
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")
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
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
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
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)
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
[Link]("Sheared image")
Theory:
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India
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.
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
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
fig.add_subplot(1, 2, 2)
[Link](new_img)
[Link]("off")
[Link]("Convoluted Image")
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.
fig.add_subplot(1, 2, 2)
[Link](Zero_Padded_img)
[Link]("off")
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India
[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
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
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.
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
*************************************
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India
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 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
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
[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
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 #######
fig.add_subplot(1, 2, 2)
[Link](new_image)
[Link]("off")
[Link]("Max Filtered 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
[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 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
▪ 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.
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
[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
❖ 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
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
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
import cv2
import numpy as np
import cv2
import numpy as np
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India
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
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
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
# Define parameters.
r1 = 70
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India
s1 = 0
r2 = 140
s2 = 255
Output:
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India
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
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
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 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
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
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
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:
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
• Python3
import cv2
def canny_edge_detection(frame):
# Convert the frame to grayscale for edge detection
gray = [Link](frame, cv2.COLOR_BGR2GRAY)
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
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
if __name__ == "__main__":
main()
Output:
Sagar Institute of Research & Technology (SIRT), Bhopal (M.P.), India