CV Lab Journal
CV Lab Journal
LAB JOURNAL
ET-2293 COMPUTER VISION
BATCH ET-A-1
Submitted by
Roll No: 8
GRNo: 12211487
Batch guide
Prof. Jyoti Madake
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Experiment No.1
Problem Statement:
AIM:
Objective(s) of Experiment:
1. Read and write the image from filesystem
2. Read and write the video path using specific path as well as webcam for further
processing.
Introduction:
To process the image the Image reading and writing is important. Image processing is a
method to perform some operations on an image, in order to get an enhanced image or to
extract some useful information from it. It is a type of signal processing in which input is an
image and output may be image or characteristics/features associated with that image.
Nowadays, image processing is among rapidly growing technologies. There are two types of
methods used for image processing namely, analogue and digital image processing. Analogue
image processing can be used for the hard copies like printouts and photographs. Image
analysts use various fundamentals of interpretation while using these visual techniques. Digital
image processing techniques help in manipulation of the digital images by using computers.
The three general phases that all types of data have to undergo while using digital technique
are pre-processing, enhancement, and display, information extraction.
2
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Flowchart:
3
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Reading an image
Displaying an image
4
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Output:
Output:
Read Image
5
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Writing an image
Input:
Output:
6
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Input:
Output:
7
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Record WebCam
Output:
Conclusions:
The image processing is supreme approach to improve the appearance of an image to a human
observer, to extract from an image quantitative information that is not readily apparent to the
eye, and to calibrate an image in photometric or geometric terms.
In this experiment, I have performed operations like reading an image, writing an image,
recording a video with and without audio and recording through web cam. The experiments
help me gain an understanding of the basic commands that are used to perform operations on
image. I also understood the different types of images like REDUCED_GRAYSCALE_2,
REDUCED_COLOR_2 etc. I also understood that the system treats the image as BGR which is
later converted to RGB for visualization.
8
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Experiment No. 2
Problem Statement:
AIM:
Objective(s) of Experiment:
Introduction:
Arithmetic Operations like Addition, Subtraction, and Bitwise Operations (AND, OR, NOT,
XOR) can be applied to the input images. These operations can be helpful in enhancing the
properties of the input images. The Image arithmetic’s are important for analyzing the input
image properties. The operated images can be further used as an enhanced input image, and
many more operations can be applied for clarifying, thresholding, dilating etc of the image.
Adding or subtracting a constant value to/from each image pixel value can be used to
increases/decrease its brightness. Blending Adding images together produces a composite
image of both input images. This can be used to produce blending effects using weighted
addition. Multiplication and division can be used as a simple means of contrast adjustment and
extension to addition/subtraction (e.g., reduce contrast to 25% = division by 4; increase
contrast by 50% = multiplication by 1.5).
9
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Flowchart:
10
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
11
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
<matplotlib.image.AxesImage at 0x267c30e7fb0>
12
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
k = 100
M = np.ones(resized.shape, dtype="uint8") * k
print(resized)
print(M)
add1 = cv2.add(resized, M)
add2 = resized + M
print("The addition with CV2", add1)
print("The addition with simple operator", add2)
print("Shape of input image", resized.shape)
#plt.imshow(resized,cmap='gray')
#plt.show()
plt.imshow(resized, cmap='gray')
plt.show()
plt.imshow(add1, cmap='gray')
plt.show()
plt.imshow(add2,cmap='gray')
plt.show()
[[255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 248 220 255 255 255 255]
[255 255 130 133 219 220 242 247 255 255]
[255 255 66 76 131 131 71 107 255 255]
[255 255 61 104 199 100 232 71 255 255]
[255 255 227 81 64 90 103 200 255 255]
[255 255 142 58 103 199 65 96 255 255]
[255 255 255 86 87 78 138 255 255 255]
[255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255]]
[[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]
[100 100 100 100 100 100 100 100 100 100]]
The addition with CV2 [[255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255]
[255 255 230 233 255 255 255 255 255 255]
[255 255 166 176 231 231 171 207 255 255]
[255 255 161 204 255 200 255 171 255 255]
[255 255 255 181 164 190 203 255 255 255]
[255 255 242 158 203 255 165 196 255 255]
[255 255 255 186 187 178 238 255 255 255]
[255 255 255 255 255 255 255 255 255 255]
[255 255 255 255 255 255 255 255 255 255]]
The addition with simple operator [[ 99 99 99 99 99 99 99 99 99 99]
[ 99 99 99 99 92 64 99 99 99 99]
[ 99 99 230 233 63 64 86 91 99 99]
13
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
14
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
path = "C:/Users/Ashish/Desktop/CVFolder/image.jfif"
img = cv2.imread(path, 1)
#img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
print(img.shape)
print(img)
15
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
width = 10
height = 10
dim = (width, height)
# resize image
resized = cv2.resize(img, dim)
print(resized.shape)
print(resized)
plt.imshow(resized)
(2035, 2048, 3)
[[[254 254 254]
[254 254 254]
[254 254 254]
...
[255 255 255]
[255 255 255]
[255 255 255]]
16
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
k = 150
M = np.ones(resized.shape, dtype="uint8") * k
print(resized)
print(M)
add1 = cv2.add(resized, M)
add2 = resized + M
print("The addition with CV2", add1)
print("The addition with simple operator", add2)
print("Shape of input image", resized.shape)
#plt.imshow(resized,cmap='gray')
#plt.show()
plt.imshow(resized)
plt.show()
plt.imshow(add1)
plt.show()
plt.imshow(add2)
plt.show()
17
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
18
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Subtraction Operation
path = "C:/Users/Ashish/Desktop/CVFolder/MM9.jpg"
img = cv2.imread(path, 1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
width = 10
height = 10
dim = (width, height)
# resize image
resized = cv2.resize(img, dim)
k = 100
M = np.ones(resized.shape, dtype="uint8") * k
sub = cv2.subtract(resized, M)
plt.imshow(resized)
plt.show()
plt.imshow(sub)
plt.show()
19
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Multiplication Operation
path = "C:/Users/Ashish/Desktop/CVFolder/MM9.jpg"
img = cv2.imread(path, 1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
width = 10
height = 10
20
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
21
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Division Operation
path = "C:/Users/Ashish/Desktop/CVFolder/MM9.jpg"
img = cv2.imread(path, 1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
k = int(input("Enter intensity of 2nd img pixels: "))
width = 10
height = 10
dim = (width, height)
# resize image
resized = cv2.resize(img, dim)
print(resized)
M = np.ones(resized.shape, dtype="uint8") * k
div = cv2.divide(resized, M)
print("division")
print(div)
plt.imshow(resized)
plt.show()
plt.imshow(div)
plt.show()
[[[ 5 3 25]
[ 63 54 114]
[103 97 165]
[ 22 11 19]
[ 38 29 37]
[ 13 0 11]
[ 10 2 9]
[ 19 5 20]
22
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
[ 2 0 3]
[ 0 0 4]]
[[ 21 16 23]
[ 56 36 37]
[ 46 29 32]
[ 16 3 3]
[ 0 0 0]
[ 4 0 1]
[ 3 0 2]
[ 37 26 30]
[ 55 38 46]
[ 72 53 58]]
division
[[[ 2 2 12]
[ 32 27 57]
[ 52 48 82]
[ 11 6 10]
[ 19 14 18]
[ 6 0 6]
[ 5 1 4]
[ 10 2 10]
[ 1 0 2]
[ 0 0 2]]
[[ 10 8 12]
[ 28 18 18]
[ 23 14 16]
[ 8 2 2]
[ 0 0 0]
[ 2 0 0]
[ 2 0 1]
[ 18 13 15]
[ 28 19 23]
[ 36 26 29]]
23
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Logical Operations:
import cv2
import numpy as np
import matplotlib.pyplot as plt
24
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
AND Operator
img1 = cv2.imread(r"C:\Users\Ashish\Desktop\CVFolder\square2.jpg")
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
img2 = cv2.imread(r"C:\Users\Ashish\Desktop\CVFolder\circle.jpg")
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
print(img1.shape)
print(img2.shape)
# resixe 10 x10
width = 10
height = 10
dim = (width, height)
# resize image
imgR1 = cv2.resize(img1, dim)
imgR2 = cv2.resize(img2, dim)
AND = cv2.bitwise_and(imgR1, imgR2)
plt.imshow(imgR1, cmap='gray')
plt.show()
print(imgR1)
plt.imshow(imgR2, cmap='gray')
plt.show()
print(imgR2)
plt.imshow(AND, cmap='gray')
plt.show()
print(AND)
(350, 350)
(350, 350)
25
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
26
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
27
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
28
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
plt.show()
print(imgR2)
plt.imshow(OR, cmap='gray')
plt.show()
print(OR)
29
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
30
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
[[251 251 251 255 255 255 254 251 251 251]
[251 95 255 255 255 255 255 249 254 251]
[251 255 255 255 24 248 255 248 249 251]
[255 255 255 255 255 255 255 25 25 249]
[255 255 255 255 255 255 25 25 25 249]
[255 255 255 254 249 25 25 25 25 249]
[255 255 255 249 249 25 25 25 25 249]
[253 255 255 25 255 253 249 249 249 253]
[251 27 255 25 25 249 249 249 251 251]
[251 251 251 255 25 249 249 254 251 251]]
XOR Image
img1 = cv2.imread(r"C:\Users\Ashish\Desktop\CVFolder\square.jpg")
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
img2 = cv2.imread(r"C:\Users\Ashish\Desktop\CVFolder\circle.jpg")
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
print(img1.shape)
print(img2.shape)
# resixe 10 x10
width = 10
height = 10
dim = (width, height)
# resize image
imgR1 = cv2.resize(img1, dim)
imgR2 = cv2.resize(img2, dim)
XOR = cv2.bitwise_xor(imgR1, imgR2)
plt.imshow(imgR1, cmap='gray')
plt.show()
31
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
print(imgR1)
plt.imshow(imgR2, cmap='gray')
plt.show()
print(imgR2)
plt.imshow(OR, cmap='gray')
plt.show()
print(OR)
(350, 350)
(350, 350)
32
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
33
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
[[251 251 251 255 255 255 254 251 251 251]
[251 95 255 255 255 255 255 249 254 251]
[251 255 255 255 24 248 255 248 249 251]
[255 255 255 255 255 255 255 25 25 249]
[255 255 255 255 255 255 25 25 25 249]
[255 255 255 254 249 25 25 25 25 249]
[255 255 255 249 249 25 25 25 25 249]
[253 255 255 25 255 253 249 249 249 253]
[251 27 255 25 25 249 249 249 251 251]
[251 251 251 255 25 249 249 254 251 251]]
NOT Operator
img1 = cv2.imread("square.jpg")
img1 = cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)
NOT = cv2.bitwise_not(img1)
width = 10
height = 10
dim = (width, height)
# resize image
imgR1 = cv2.resize(img1, dim)
imgR2 = cv2.resize(NOT, dim)
XOR = cv2.bitwise_xor(imgR1, imgR2)
plt.imshow(imgR1)
plt.show()
plt.imshow(imgR2)
plt.show()
34
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
35
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Conclusion:
The addition of two images of the same size results in a new image of the same size whose
pixels are to the sum of the pixels in the original images. The subtraction of two images detects
changes between input images. Multiplication and division are useful in contrast adjustment.
The division of two images correct non- homogeneous illumination and shadow removal. Also,
whenever division operation takes place there is a significant darkening of the images, so for a
large factor the image may become complete black. Also, using the individual pixel values, the
arithmatic operations and logical operations can be easily verified.
36
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Experiment No. 3
Problem Statement:
Geometric Transformations
AIM:
Objective(s) of Experiment:
To perform the geometric transformations of images.
Introduction:
Geometric transformations are widely used for image registration and the removal of
geometric distortion. Scaling is just resizing of the image. Rotation is a concept in
mathematics that is a motion of a certain space that preserves at least one point. Image rotation
is a common image processing routine with applications in matching, alignment, and other
image-based algorithms, it is also extensively in data augmentation, especially when it comes
to image classification.
Image transformation is a coordinate changing function, it maps some (x, y) points in one
coordinate system to points (x', y') in another coordinate system. Shear mapping is a linear
map that displaces each point in a fixed direction, it substitutes every point horizontally or
vertically by a specific value in proportion to its x or y coordinates, there are two types of
shearing effects. Image cropping is the removal of unwanted outer areas from an image, a lot
of the above examples introduced black pixels, you can easily remove them using cropping.
Common applications include construction of mosaics, geographical mapping, stereo and
video.
37
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Flowchart:
39
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Rotation Operation
# reads an input image
img = cv2.imread(r"Mario.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #BGR 2 RGB for plotting using
matplotlib
h, w = img.shape[:2]
center = (w//2, h//2)
ang = int(input("Enter degree of rotation: ")) #Positive value for anti-clockwise &
negative for clockwise
r_matrix = cv2.getRotationMatrix2D(center, ang, 1.0) #Generating rotational matrix
r = cv2.warpAffine(img, r_matrix, (w, h))
plt.imshow(img)
plt.show()
plt.imshow(r)
plt.show()
40
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Scaling Operation
# reads an input image
img = cv2.imread(r"Mario.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #BGR 2 RGB for plotting using
matplotlib
41
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
h, w = img.shape[:2]
scale = float(input("Enter scaling factor: ")) #scale>1:Img is scaled-up , scale<1:Img
is scaled-down
h = int(img.shape[0] * scale) #Scaling in y-axis
w = int(img.shape[1] * scale) #Scaling in x-axis
re = cv2.resize(img, (w, h)) #Resizing img with scaled values
plt.imshow(img)
plt.show()
plt.imshow(re)
plt.show()
42
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Cropping Operation
# reads an input image
img = cv2.imread(r"Mario.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #BGR 2 RGB for plotting using
matplotlib
h, w = img.shape[:2]
print(img.shape)
cxlow = int(input("Enter lower-x limit: ")) #Enter cropping limits
cxup = int(input("Enter upper-x limit: "))
cylow = int(input("Enter lower-y limit: "))
cyup = int(input("Enter upper-y limit: "))
crop = img[cylow:cyup, cxlow:cxup] #Only displaying the selected part of
img
plt.imshow(img)
plt.show()
plt.imshow(crop)
plt.show()
(1400, 1400, 3)
43
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Flipping Operation
# reads an input image
img = cv2.imread(r"Mario.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #BGR 2 RGB for plotting using
matplotlib
44
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
h, w = img.shape[:2]
flip1 = cv2.flip(img, 1) # left right flip
flip2 = cv2.flip(img, 0) # upside down
flip3 = cv2.flip(img, -1) #left right flip + upside down
plt.imshow(img)
plt.show()
plt.imshow(flip1)
plt.show()
plt.imshow(flip2)
plt.show()
plt.imshow(flip3)
plt.show()
45
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
46
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
47
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Shearing Operation
# reads an input image
img = cv2.imread(r"Mario.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #BGR 2 RGB for plotting using
matplotlib
48
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
h, w = img.shape[:2]
m = int(input("Enter 1 for horizontal and 2 for vertical shear: "))
if m==1:
k = float(input("Enter shearing factor: "))
sh_matrix = np.float32([[1,k,0], [0,1,0], [0,0,1]]) #Creating shearing matrix for
given factor
sheared = cv2.warpPerspective(img, sh_matrix, (int(w*(k+1)), h)) #Increasing width by
the same factor
if m==2:
k = float(input("Enter shearing factor: "))
sh_matrix = np.float32([[1,0,0], [k,1,0], [0,0,1]])
sheared = cv2.warpPerspective(img, sh_matrix, (w, int(h*(k+1)))) #Increasing height by
the same factor
plt.imshow(img)
plt.show()
plt.imshow(sheared)
plt.show()
Conclusions:
In these experiments, we performed the basics of image processing and transformation,
which are image translation, scaling, shearing, reflection, rotation, and cropping. Translation
and rotation is shifting image in horizontal direction and rotation. Scaling means adjusting
size. Shearing means skewing. Cropping means extracting a portion of image. Reflection
means flipping the image.
49
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Experiment No. 4
Problem Statement:
AIM:
Objective(s) of Experiment:
To enhance the image intensity using level slicing, log and power law transformation
Introduction:
Intensity Level Slicing highlights the particular range of gray levels in an image. It handles a
group of intensity levels in an image up to a specific range by reducing rest or by leaving them
alone.
Log transformation of an image means replacing all pixel values, present in the image, with
its logarithmic values. Log transformation is used for image enhancement as it expands dark
pixels of the image as compared to higher pixel values.
Power – Law transformations is used for enhancing images for different type of display
devices. The gamma of different display devices is different. For example, Gamma of CRT
lies in between of 1.8 to 2.5, that means the image displayed on CRT is dark.
Flowchart:
50
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
51
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
52
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
z = np.zeros((x,y))
for i in range(0,x):
for j in range(0,y):
if(gray[i][j]>50 and gray[i][j]<150):
z[i][j]=255
else:
z[i][j]=0
plt.imshow(z, 'gray')
plt.title('Sliced Original Image')
plt.show()
53
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
54
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
plt.show()
z = np.zeros((x,y))
for i in range(0,x):
for j in range(0,y):
if(gray[i][j]>50 and gray[i][j]<150):
z[i][j]=255
else:
z[i][j]=gray[i][j]
plt.imshow(z, 'gray')
plt.title('Sliced with background')
plt.show()
55
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Log Transform
image = cv2.imread(r"Bridge.jpg")
#image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
x,y = image.shape[:2]
plt.imshow(gray,'gray')
plt.title('Original Image')
plt.show()
c = 255/(np.log(1+np.max(gray)))
log = c*np.log(1+gray)
log = np.array(log,dtype=np.uint8)
plt.imshow(log, 'gray')
plt.show()
56
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
C:\Users\Ashish\AppData\Local\Temp\ipykernel_19928\2741588362.py:9: RuntimeWarning:
divide by zero encountered in log
log = c*np.log(1+gray)
C:\Users\Ashish\AppData\Local\Temp\ipykernel_19928\2741588362.py:10: RuntimeWarning:
invalid value encountered in cast
log = np.array(log,dtype=np.uint8)
Power Law
57
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
image = cv2.imread(r"Bridge.jpg")
#image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
x,y = image.shape[:2]
plt.imshow(gray,'gray')
plt.title('Original Image')
plt.show()
z = np.zeros((x,y))
gamma = float(input("Enter gamma value: "))
gamma1 = float(input("Enter input value"))
gamma2 = float(input("Enter value"))
c = 255/(np.log(1+np.max(gray)))
z = np.array(c*(gray**gamma), dtype = 'uint8')
z1 = np.array(c*(gray**gamma1), dtype= 'uint8')
z2 = np.array(c*(gray**gamma2), dtype = 'uint8')
print("gamma = ",gamma)
plt.imshow(z, 'gray')
plt.show()
plt.imshow(z1,'gray')
plt.show()
plt.imshow(z2, 'gray')
plt.show()
print(z)
print(z1)
print(z2)
C:\Users\Ashish\AppData\Local\Temp\ipykernel_19928\2293666078.py:15: RuntimeWarning:
divide by zero encountered in power
z2 = np.array(c*(gray**gamma2), dtype = 'uint8')
C:\Users\Ashish\AppData\Local\Temp\ipykernel_19928\2293666078.py:15: RuntimeWarning:
58
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
gamma = 0.2
gamma = 0.6
gamma = -0.3
59
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Experiment No. 5
Problem Statement:
AIM:
Objective(s) of Experiment:
Introduction:
An image histogram is a type of histogram that acts as a graphical representation of the tonal
distribution in a digital image. It plots the number of pixels for each tonal value. By looking
at the histogram for a specific image a viewer will be able to judge the entire tonal distribution
at a glance.
The horizontal axis of the graph represents the tonal variations, while the vertical axis
represents the number of pixels in that particular tone. The left side of the horizontal axis
represents the black and dark areas, the middle represents medium grey and the right-hand side
represents light and pure white areas. The vertical axis represents the size of the area that is
captured in each one of these zones.
61
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Flowchart:
62
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
import cv2
import matplotlib.pyplot as plt
import numpy as np
from skimage import exposure
from skimage.exposure import match_histograms
from matplotlib.colors import NoNorm
# reads an input image
img = cv2.imread(r"Apple.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
plt.imshow(gray,'gray')
plt.title('Original Image')
plt.show()
# find frequency of pixels in range 0-255, with 256 bins
histr = cv2.calcHist([gray],[0],None,[256],[0,255])
histr1 = cv2.calcHist([gray],[0],None,[16],[0,255])
# show the plotting graph of an image
plt.plot(histr)
plt.xlabel('number of bins')
plt.ylabel('Number of pixels')
plt.title('Histogram')
plt.show()
plt.plot(histr1)
plt.xlabel('number of bins')
plt.ylabel('Number of pixels')
plt.title('Histogram with reduced bins')
plt.show()
63
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
64
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
65
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Color Histogram
import cv2
import numpy as np
from matplotlib import pyplot as plt
66
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
67
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
68
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Linear Stretching
# reads an input image
img = cv2.imread(r"woods.jpg")
69
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
gray.shape
(382, 612)
plt.hist(gray.flatten()*255)
plt.title('Original Image Histogram')
gray.min()
2
70
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
gray.max()
255
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
71
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
72
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
73
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Histogram Equalization
#Apply histogram equalization
equl = cv2.equalizeHist(gray)
plt.imshow(equl,'gray')
plt.title('Equalized Image')
plt.show()
histr2 = cv2.calcHist([equl],[0],None,[256],[0,256])
plt.plot(histr2)
plt.xlabel('Intensity value')
plt.ylabel('Number of pixels')
plt.title('Equalised histogram')
plt.show()
74
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
plt.tight_layout()
plt.show()
fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(8, 8))
for i, img in enumerate((image, reference, matched)):
for c, c_color in enumerate(('red', 'green', 'blue')):
img_hist, bins = exposure.histogram(img[..., c],source_range='dtype)
axes[c, i].plot(bins, img_hist / img_hist.max())
img_cdf, bins = exposure.cumulative_distribution(img[..., c])
axes[c, i].plot(bins, img_cdf)
axes[c, 0].set_ylabel(c_color)
axes[0, 0].set_title('Source')
axes[0, 1].set_title('Reference')
axes[0, 2].set_title('Matched')
plt.tight_layout()
plt.show()
No of Channel is: 3
No of Channel is: 3
76
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
77
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
sharey=True)
for aa in (ax1, ax2, ax3):
aa.set_axis_off()
ax1.imshow(image)
ax1.set_title('Source')
ax2.imshow(reference)
ax2.set_title('Reference')
ax3.imshow(matched)
ax3.set_title('Matched')
plt.tight_layout()
plt.show()
fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(8, 8))
for i, img in enumerate((image, reference, matched)):
for c, c_color in enumerate(('red', 'green', 'blue')):
img_hist, bins = exposure.histogram(img[..., c],
source_range='dtype')
axes[c, i].plot(bins, img_hist / img_hist.max())
img_cdf, bins = exposure.cumulative_distribution(img[..., c])
axes[c, i].plot(bins, img_cdf)
axes[c, 0].set_ylabel(c_color)
axes[0, 0].set_title('Source')
axes[0, 1].set_title('Reference')
axes[0, 2].set_title('Matched')
plt.tight_layout()
plt.show()
No of Channel is: 3
No of Channel is: 3
78
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
# matplotlib expects RGB images so convert and then display the image
# with matplotlib
plt.figure()
plt.axis("off")
plt.imshow(cv2.cvtColor(image, cv2.COLOR_GRAY2RGB))
79
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
plt.title("Grayscale Histogram")
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
plt.plot(hist)
plt.xlim([0, 256])
# normalize the histogram
hist /= hist.sum()
# plot the normalized histogram
plt.figure()
plt.title("Grayscale Histogram (Normalized)")
plt.xlabel("Bins")
plt.ylabel("% of Pixels")
plt.plot(hist)
plt.xlim([0, 256])
plt.show()
80
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
81
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
# Define parameters.
r1 = 60
r2 = 190
s1 = 0
s2 = 255
82
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
83
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Conclusions:
Histogram image contrast has been enhanced and its histogram has also been equalized. There
is also one important thing to be noted here that during histogram equalization the overall shape
of the histogram changes, whereas in histogram stretching the overall shape of histogram
remains same.
84
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Experiment No. 6
Problem Statement:
Spatial Domain Filters and Frequency Domain Filtering
AIM:
Objective(s) of Experiment:
Enhancement operation of image using spatial domain filters
Introduction:
Spatial Filtering technique is used directly on pixels of an image. Mask is usually considered
to be added in size so that it has specific center pixel. This mask is moved on the image such
that the center of the mask traverses all image pixels.
● Low Pass Average Filter: A low-pass filter is a filter that passes signals with a
frequency lower than a selected cutoff frequency and attenuates signals with
frequencies higher than the cutoff frequency. The exact frequency response of
the filter depends on the filter design.
● Low Pass Median Filter: Low pass filtering (aka smoothing) is employed to
remove high spatial frequency noise from a digital image. The low-pass filters
usually employ moving window operator which affects one pixel of the image at
a time, changing its value by some function of a local region (window) of pixels
● High Pass Filter: A high pass filter tends to retain the high frequency information
within an image while reducing the low frequency information. The kernel of the
high pass filter is designed to increase the brightness of the center pixel relative
to neighboring pixels.
85
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
● High Boost Filter: The high boost filter can be used to enhance the high
frequency components. We can sharpen edges of a image through the
amplification and obtain a more clear image. The high boost filter is simply the
sharpening operator in image processing.
Frequency domain filtering is a powerful technique in image processing that involves
transforming an image from the spatial domain to the frequency domain, applying filters,
and then converting it back. This approach allows for advanced manipulation of image
features based on their frequency components. Its techniques involve:
1. Transforming the Image: Converting the image from the spatial domain to the frequency
domain using the Fast Fourier Transform (FFT).
2. Applying Filters: Utilizing various filters in the frequency domain to manipulate specific
frequency components of the image:
- Gaussian Blur Filter: Smooths the image by reducing high-frequency noise and details,
acting as a low-pass filter.
- High Pass Filter: Enhances edges and fine details by suppressing low-frequency
components.
- Low Pass Filter: Removes high-frequency noise and blurs the image by allowing only
low-frequency components to pass.
- Band Pass Filter: Isolates and passes a specific range of frequencies while blocking
others. - Band Stop Filter: Suppresses a specific range of frequencies to remove periodic
noise, allowing other frequencies to pass.
3. Transforming Back: Converting the filtered image back to the spatial domain using the
inverse Fast Fourier Transform (IFFT). This process allows for effective noise reduction,
feature enhancement, and frequency-specific image modifications
86
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Flowchart:
87
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
plt.show()
plt.imshow(out4, 'gray')
plt.title('Low Pass Average Filter - In built function')
plt.show()
plt.imshow(out5, 'gray')
plt.title('Low Pass Average Filter - In built function')
plt.show()
89
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
90
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
91
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
92
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
93
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
94
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
95
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
96
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
97
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
98
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
99
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
100
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
101
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
102
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
103
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
104
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
105
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
106
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
107
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
108
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
109
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
110
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
111
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
112
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
113
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
114
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
115
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
116
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
117
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
118
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
119
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
120
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
121
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
plt.imshow(out4, 'gray')
plt.title('Low Pass Median Filter')
plt.show()
122
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
123
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
124
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
125
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
126
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
size=20), pylab.axis('off')
pylab.subplot(2,3,2), pylab.imshow(gauss_kernel), pylab.title('Gaussian Kernel', size=20)
pylab.subplot(2,3,3), pylab.imshow(im1) # the imaginary part is an artifact
pylab.title('Output Image', size=20), pylab.axis('off')
pylab.subplot(2,3,4), pylab.imshow( (20*np.log10( 0.1 + fp.fftshift(freq))).astype(int))
pylab.title('Original Image Spectrum', size=20), pylab.axis('off')
pylab.subplot(2,3,5), pylab.imshow( (20*np.log10( 0.1 +
fp.fftshift(freq_kernel))).astype(int))
pylab.title('Gaussian Kernel Spectrum', size=20), pylab.subplot(2,3,6)
pylab.imshow( (20*np.log10( 0.1 + fp.fftshift(convolved))).astype(int))
pylab.title('Output Image Spectrum', size=20), pylab.axis('off')
pylab.subplots_adjust(wspace=0.2, hspace=0)
pylab.show()
128
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
129
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
im = cv2.imread('Bridge.jpg')
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
print(im.shape)
# (224, 225)
gauss_kernel = np.outer(signal.gaussian(11, 3), signal.gaussian(11, 3)) # 2D Gaussian kernel
of size 11x11 with σ = 3
im_blurred1 = signal.convolve(im, gauss_kernel, mode="same")
im_blurred2 = signal.fftconvolve(im, gauss_kernel, mode='same')
def wrapper_convolve(func):
def wrapped_convolve():
return func(im, gauss_kernel, mode="same")
return wrapped_convolve
130
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
wrapped_convolve = wrapper_convolve(signal.convolve)
wrapped_fftconvolve = wrapper_convolve(signal.fftconvolve)
times1 = timeit.repeat(wrapped_convolve, number=1, repeat=100)
times2 = timeit.repeat(wrapped_fftconvolve, number=1, repeat=100)
pylab.figure(figsize=(15,5))
pylab.gray()
pylab.subplot(131), pylab.imshow(im), pylab.title('Original Image',size=15), pylab.axis('off')
pylab.subplot(132), pylab.imshow(im_blurred1), pylab.title('convolve Output', size=15),
pylab.axis('off')
pylab.subplot(133), pylab.imshow(im_blurred2), pylab.title('ffconvolve Output',
size=15),pylab.axis('off')
(574, 860)
131
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
132
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
133
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
134
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
135
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
(<matplotlib.image.AxesImage at 0x1313ff25640>,
(-0.5, 605.5, 605.5, -0.5),
None)
from scipy import fftpack
im = cv2.imread('Rose.jpeg')
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
freq = fp.fft2(im)
(w, h) = freq.shape
half_w, half_h = int(w/2), int(h/2)
snrs_hp = []
lbs = list(range(1,25))
pylab.figure(figsize=(12,20))
for l in lbs:
freq1 = np.copy(freq)
freq2 = fftpack.fftshift(freq1)
freq2[half_w-l:half_w+l+1,half_h-l:half_h+l+1] = 0 # select all but the first lxl (low)
frequencies
im1 = np.clip(fp.ifft2(fftpack.ifftshift(freq2)).real,0,255) # clip pixel values after IFFT
#snrs_hp.append(signaltonoise(im1, axis=None))
pylab.subplot(6,4,l), pylab.imshow(im1, cmap='gray'), pylab.axis('off')
pylab.title('F = ' + str(l+1), size=20)
pylab.subplots_adjust(wspace=0.1, hspace=0)
pylab.show()
136
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
137
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
pylab.figure(figsize=(10,10))
pylab.imshow( (20*np.log10( 0.1 +
numpy.fft.fftshift(freq_gaussian))).astype(int))
pylab.show()
139
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
im = np.array(Image.open('Rose.jpeg').convert('L'))
freq = fp.fft2(im)
(w, h) = freq.shape
half_w, half_h = int(w/2), int(h/2)
snrs_lp = []
ubs = list(range(1,25))
pylab.figure(figsize=(12,20))
for u in ubs:
freq1 = np.copy(freq)
freq2 = fftpack.fftshift(freq1)
freq2_low = np.copy(freq2)
freq2_low[half_w-u:half_w+u+1,half_h-u:half_h+u+1] = 0
freq2 -= freq2_low # select only the first 20x20 (low) frequencies
im1 = fp.ifft2(fftpack.ifftshift(freq2)).real
#snrs_lp.append(signaltonoise(im1, axis=None))
pylab.subplot(6,4,u), pylab.imshow(im1, cmap='gray'), pylab.axis('off')
pylab.title('F = ' + str(u), size=20)
pylab.subplots_adjust(wspace=0.1, hspace=0)
pylab.show()
140
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
141
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
0.7513583129834916
142
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
143
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
pylab.tight_layout()
pylab.show()
(496, 512)
144
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Conclusions:
An image filter is a technique through which size, colors, shading and other characteristics of
an image are altered. In these experiments we used different filters and observed on different
image.
In this experiment, different noises are added to the image to analyze which filter is most
suitable for a type of a noise. Based on the results obtained, for the random noise or salt and
pepper noise (a type of random noise) median filter appears to be more suitable as the middle
value of the matrix is replaced with the median of all the values in the matrix.
For uniform noise, mean filter is the appropriate one as it gradually reduces the image whilst
keeping the image clear. For impulse noise, median filter is the right choice, even with a
smaller matrix the noise is reduced or suppressed to a great extent. Moreover, as the size of the
matrix increases, the noise reduces furthermore. However, it should be noted that increasing
the size of matrix too much may result in complete blurring of the image.
For the high pass filters, the large frequency changes are allowed and hence helps to capture
the coarse details like the shape. For these filters, normal kernel without dividing it by 9
(normalization) provides a better result as it makes the difference between the intensities more
prominent hence normalization in this case is not preferred. High boost simply extracts the
high pass image and then merges it with the original image to obtain a sharp image. In this
145
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
case kernel with normalization is preferred to control the intensity. If the result is required to
be extremely sharp normalization is avoided, but for better control over intensities,
normalization is used. The main point of interest in the frequency domain spectrum is the
center. If the center is brightly illuminated (high intensity), it indicates the presence of low-
frequency components, suggesting the use of a low-pass filter. This can be visualized as a bell-
shaped curve in the magnitude spectrum. Conversely, for high-frequency components, the
center region appears dark, while the surrounding areas are illuminated based on intensity,
indicating the use of a high-pass filter. This can be visualized as an inverted bell shape in the
magnitude spectrum.
146
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Experiment No. 7
Problem Statement:
Edge Detectors using Python
AIM:
Write a Python Code for the following Edge Detection Operators:
1. Roberts Operator
2. Prewitt’s Operator
3. Sobel Operator
4. Prewitt’s Compass Operators
5. Sobel Compass Operators
6. Canny Edge Detector
Objective(s) of Experiment:
To perform and study edge detection operators for image data.
Introduction:
Edge detection includes a variety of mathematical methods that aim at identifying points in
a digital image at which the image brightness changes sharply or, more formally, has
discontinuities. The points at which image brightness changes sharply are typically
organized into a set of curved line segments termed edges. The same problem of finding
discontinuities in 1D signals is known as step detection and the problem of finding signal
discontinuities over time is known as change detection. Edge detection is a fundamental tool
in image processing, machine vision and computer vision, particularly in the areas of feature
detection and feature extraction.
147
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Flowchart:
148
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
image = cv2.imread("Temple.jpeg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
img2 = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
x,y = gray.shape[:2]
plt.imshow(gray, 'gray')
plt.title('Input Image')
plt.show()
kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]])
kernely = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
plt.imshow(img_prewittx, 'gray')
plt.title('Prewitt Horizontal Edge Kernel')
plt.show()
plt.imshow(img_prewitty, 'gray')
plt.title('Prewitt Vertical Edge Kernel')
plt.show()
plt.imshow(img_prewitt, 'gray')
plt.title('Prewitt Both Edges Kernel')
plt.show()
149
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
150
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
image = cv2.imread(r"Temple.jpeg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
x,y = gray.shape[:2]
plt.imshow(gray, 'gray')
plt.title('Input Image')
plt.show()
151
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]])
kernely = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
plt.imshow(img_prewittx, 'gray')
plt.title('Prewitt Horizontal Edge Kernel')
plt.show()
plt.imshow(img_prewitty, 'gray')
plt.title('Prewitt Vertical Edge Kernel')
plt.show()
plt.imshow(img_prewitt, 'gray')
plt.title('Prewitt Both Edges Kernel')
plt.show()
152
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
153
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
image = cv2.imread(r"Temple.jpeg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
x,y = gray.shape[:2]
plt.imshow(gray, 'gray')
plt.title('Input Image')
plt.show()
plt.imshow(img_sobelx, 'gray')
plt.title('Sobel Horizontal Edge Kernel')
plt.show()
plt.imshow(img_sobely, 'gray')
plt.title('Sobel Vertical Edge Kernel')
plt.show()
plt.imshow(img_sobel, 'gray')
plt.title('Sobel Both Edges Kernel')
plt.show()
154
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
155
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
image = cv2.imread(r"Temple.jpeg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
x,y = gray.shape[:2]
plt.imshow(gray, 'gray')
plt.title('Input Image')
plt.show()
156
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
kernelx = np.array([[1,8,1],[0,0,0],[-1,-8,-1]])
kernely = np.array([[-1,0,1],[-8,0,8],[-1,0,1]])
plt.imshow(img_sobelx, 'gray')
plt.title('Sobel Horizontal Edge Kernel')
plt.show()
plt.imshow(img_sobely, 'gray')
plt.title('Sobel Vertical Edge Kernel')
plt.show()
plt.imshow(img_sobel, 'gray')
plt.title('Sobel Both Edges Kernel')
plt.show()
157
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
158
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
image = cv2.imread(r"Temple.jpeg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
x,y = gray.shape[:2]
plt.imshow(gray, 'gray')
plt.title('Input Image')
plt.show()
plt.imshow(roberts, 'gray')
plt.title('roberts Kernel')
plt.show()
159
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
image = cv2.imread(r"Temple.jpeg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
x,y = gray.shape[:2]
plt.imshow(gray, 'gray')
plt.title('Input Image')
plt.show()
160
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
#Masks
prewitt1 = np.array ([[-1,-1,-1],[1,-2,1],[1,1,1]])
prewitt2 = np.array ([[-1,-1,1],[-1,-2,1],[1,1,1]])
prewitt3 = np.array ([[-1,1,1],[-1,-2,1],[-1,1,1]])
prewitt4 = np.array ([[1,1,1],[-1,-2,1],[-1,-1,1]])
prewitt5 = np.array ([[1,1,1],[1,-2,1],[-1,-1,-1]])
prewitt6 = np.array ([[1,1,1],[1,-2,-1],[1,-1,-1]])
prewitt7 = np.array ([[1,1,-1],[1,-2,-1],[1,1,-1]])
prewitt8 = np.array ([[1,-1,-1],[1,-2,-1],[1,1,1]])
img2= cv2.GaussianBlur(gray,(5,5),0)#gaussian Image
img_prewitt1 = cv2.filter2D(img2, -1, prewitt1)
img_prewitt2 = cv2.filter2D(img2, -1, prewitt2)
img_prewitt3 = cv2.filter2D(img2, -1, prewitt3)
img_prewitt4 = cv2.filter2D(img2, -1, prewitt4)
img_prewitt5 = cv2.filter2D(img2, -1, prewitt5)
img_prewitt6 = cv2.filter2D(img2, -1, prewitt6)
img_prewitt7 = cv2.filter2D(img2, -1, prewitt7)
img_prewitt8 = cv2.filter2D(img2, -1, prewitt8)
fig=plt.figure(dpi=200)
fig.add_subplot(3,3,1)
plt.imshow(img_prewitt1, 'gray')
plt.title('Prewitt 1')
plt.axis('off')
fig.add_subplot(3,3,2)
plt.imshow(img_prewitt2, 'gray')
plt.title('Prewitt 2')
plt.axis('off')
fig.add_subplot(3,3,3)
plt.imshow(img_prewitt3, 'gray')
plt.title('Prewitt 3')
plt.axis('off')
fig.add_subplot(3,3,4)
plt.imshow(img_prewitt4, 'gray')
plt.title('Prewitt 4')
plt.axis('off')
fig.add_subplot(3,3,5)
plt.imshow(img_prewitt5, 'gray')
plt.title('Prewitt 5')
plt.axis('off')
fig.add_subplot(3,3,6)
plt.imshow(img_prewitt6, 'gray')
plt.title('Prewitt 6')
161
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
plt.axis('off')
fig.add_subplot(3,3,7)
plt.imshow(img_prewitt7, 'gray')
plt.title('Prewitt 7')
plt.axis('off')
fig.add_subplot(3,3,8)
plt.imshow(img_prewitt8, 'gray')
plt.title('Prewitt 8')
plt.axis('off')
plt.show()
#Adding all the elements together to form an image
prewitt_compass = img_prewitt1 + img_prewitt2 + img_prewitt3 + img_prewitt4 +
img_prewitt5 + img_prewitt6 + img_prewitt7 + img_prewitt8
plt.imshow(prewitt_compass, 'gray')
plt.title('prewitt_compass Kernel Filter')
plt.show()
162
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
image = cv2.imread(r"Temple.jpeg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
163
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
x,y = gray.shape[:2]
plt.imshow(gray, 'gray')
plt.title('Input Image')
plt.show()
sobel1 = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
sobel1 = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
sobel2 = np.array([[-2,-1,0],[-1,0,1],[0,1,2]])
sobel3 = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
sobel4 = np.array([[0,1,2],[-1,0,1],[-2,-1,0]])
sobel5 = np.array([[1,2,1],[0,0,0],[-1,-2,-1]])
sobel6 = np.array([[2,1,0],[1,0,-1],[0,-1,-2]])
sobel7 = np.array([[1,0,-1],[2,0,-2],[1,0,-1]])
sobel8 = np.array([[0,-1,-2],[1,0,-1],[2,1,0]])
#img_sobel1 = cv2.Sobel(img2,cv2.CV_8U,1,0,ksize=3)
#img_sobel2 = cv2.Sobel(img2,cv2.CV_8U,0,1,ksize=3)
#img_sobel3 = cv2.Sobel(img2,cv2.CV_8U,1,0,ksize=3)
#img_sobel4 = cv2.Sobel(img2,cv2.CV_8U,0,1,ksize=3)
#img_sobel5 = cv2.Sobel(img2,cv2.CV_8U,1,0,ksize=3)
#img_sobel6 = cv2.Sobel(img2,cv2.CV_8U,0,1,ksize=3)
#img_sobel7 = cv2.Sobel(img2,cv2.CV_8U,1,0,ksize=3)
#img_sobel8 = cv2.Sobel(img2,cv2.CV_8U,0,1,ksize=3)
plt.imshow(sobel_compass, 'gray')
plt.title('sobel_compass Kernel Filter')
plt.show()
164
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Gaussian Blur
# reads an input image
from skimage import feature, exposure
img = cv2.imread(r"Temple.jpeg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #BGR 2 RGB for plotting
using matplotlib
width = 100
165
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
height = 100
dim = (width, height)
# resize image
gray = cv2.resize(gray, dim)
166
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
167
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
plt.imshow(edge, 'gray')
plt.title('aperture = 5 Image')
plt.show()
plt.imshow(edge2, 'gray')
plt.title('aperture = 3Image')
plt.show()
168
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
plt.imshow(edge, 'gray')
plt.title('Input Image')
plt.show()
plt.imshow(edge2, 'gray')
plt.title('Input Image')
plt.show()
169
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
170
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
image = cv2.imread(r"Temple.jpeg")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
x,y = gray.shape[:2]
plt.imshow(gray, 'gray')
plt.title('Input Image')
plt.show()
plt.imshow(edge, 'gray')
plt.title('aperture = 5 Image')
171
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
plt.show()
plt.imshow(edge2, 'gray')
plt.title('aperture =3 Image')
plt.show()
172
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Threshold Analysis
# compute a "wide", "mid-range", and "tight" threshold for the edges
# using the Canny edge detector
# Syntax: cv2.Canny(image, T_lower, T_upper, aperture_size, L2Gradient)
wide = cv2.Canny(blurred, 10, 200)
mid = cv2.Canny(blurred, 30, 150)
tight = cv2.Canny(blurred, 240, 250)
173
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Conclusions:
In this experiment, we have performed and studied the edge detection of an image using
different operators, we can conclude that edge detectors are useful for finding boundaries of
object within images. It works by detecting sharp discontinuities in brightness. Edge detection
can be used for Image segmentation and data extraction. Sobel inherently uses noise reduction
with the help of gaussian blur, while for Prewitt, it needs to be used explicitly. When used
with weighted edges, the edges get more emphasized thus providing a better output. L2
gradient is used for magnitude of the edge. L2 provides a better output compared to the normal
L1 gradient. Aperture decides the amounts of details or edges to be highlighted in the image.
For a high aperture, more edges are observed. Threshold analysis can also be performed to
highlight the edges of a range of intensity.
174
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Experiment No. 8
Problem Statement:
Blob and corner Detectors using Python
AIM:
Write a Python Code for the following Blob and corner Detection Operators:
1.Laplacian of Gaussian(LoG)
2.Difference of Gaussian(DoG)
3.Determinant of Hessian(DoH)
4.Harris corner detectors
Objective(s) of Experiment:
To perform and study blob and corner detection operators for image data.
Introduction:
Blob and corner detection are critical techniques in image processing, used to identify and
extract important features from images. Blob detection targets regions within an image that
differ in intensity from their surroundings, making it useful for tasks like object recognition
and segmentation. Common methods include the Laplacian of Gaussian (LoG), Difference
of Gaussian (DoG), and Determinant of Hessian (DoH).Corner detection, meanwhile,
focuses on identifying points where edges meet, such as the corners of objects, which are
key for image matching and motion tracking. Techniques like the Harris Corner Detector are
widely employed for this purpose. Together, these methods provide a robust foundation for
various computer vision applications, enabling machines to interpret and analyze visual data
effectively.
175
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Flowchart:
176
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
sample_g.shape
(400, 400)
Binarization Of Image
fig, ax = plt.subplots(1,3,figsize=(15,5))
sample_b = sample_g > 25
ax[0].set_title('Grayscale Image',fontsize=20)
ax[0].imshow(sample_g,cmap='gray')
ax[1].plot(sample_g[300])
ax[1].set_ylabel('Pixel Value')
ax[1].set_xlabel('Width of Picture')
ax[1].set_title('Plot of 1 Line',fontsize=15)
ax[2].set_title('Binarized Image',fontsize=15)
ax[2].imshow(sample_b,cmap='gray')
177
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
<matplotlib.image.AxesImage at 0x2c5d691c140>
ax[0].imshow(sample_g,cmap='gray')
ax[1].imshow(sample_b, cmap='gray')
for blob in blobs:
y, x, area = blob
ax[1].add_patch(plt.Circle((x, y), area*np.sqrt(2), color='r',
fill=False))
ax[1].set_title('Using DOG',fontsize=15)
plt.tight_layout()
plt.show()
179
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
DoH
import cv2
import numpy as np
from skimage.io import imshow, imread
from skimage.color import rgb2gray
import matplotlib.pyplot as plt
180
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
sample.shape
print(np.isnan(sample).any(), np.isinf(sample).any())
print(np.iscomplex(sample).any())
False False
False
sample_b = sample_g > 25
Binarization of Image
181
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
182
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
183
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
# Rescale the HOG image to the range [0, 255] for display
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))
184
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
return h
186
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
KERNEL_SIZE = 3
THRESHOLD = 0.2
# Find maximums
h1 = h_fun(img1, KERNEL_SIZE)
m1 = find_max(h1, KERNEL_SIZE, THRESHOLD)
draw_points(img1_color, m1)
plt.show()
C:\Users\Ashish\AppData\Local\Temp\ipykernel_24548\2343967864.py:27:
DeprecationWarning: Please import `maximum_filter` from the `scipy.ndimage` namespace;
the `scipy.ndimage.filters` namespace is deprecated and will be removed in SciPy 2.0.0.
data_max = filters.maximum_filter(image, size)
187
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
KERNEL_SIZE = 3
THRESHOLD = 0.3
# Find maximums
h1 = h_fun(img1, KERNEL_SIZE)
m1 = find_max(h1, KERNEL_SIZE, THRESHOLD)
draw_points(img1_color, m1)
plt.show()
C:\Users\Ashish\AppData\Local\Temp\ipykernel_24548\2343967864.py:27:
DeprecationWarning: Please import `maximum_filter` from the `scipy.ndimage` namespace;
the `scipy.ndimage.filters` namespace is deprecated and will be removed in SciPy 2.0.0.
data_max = filters.maximum_filter(image, size)
KERNEL_SIZE = 3
THRESHOLD = 0.4
# Find maximums
h1 = h_fun(img1, KERNEL_SIZE)
m1 = find_max(h1, KERNEL_SIZE, THRESHOLD)
draw_points(img1_color, m1)
plt.show()
C:\Users\Ashish\AppData\Local\Temp\ipykernel_24548\2343967864.py:27:
DeprecationWarning: Please import `maximum_filter` from the `scipy.ndimage` namespace;
the `scipy.ndimage.filters` namespace is deprecated and will be removed in SciPy 2.0.0.
data_max = filters.maximum_filter(image, size)
188
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
KERNEL_SIZE = 5
THRESHOLD = 0.1
# Find maximums
h1 = h_fun(img1, KERNEL_SIZE)
m1 = find_max(h1, KERNEL_SIZE, THRESHOLD)
draw_points(img1_color, m1)
plt.show()
C:\Users\Ashish\AppData\Local\Temp\ipykernel_24548\2343967864.py:27:
DeprecationWarning: Please import `maximum_filter` from the `scipy.ndimage` namespace;
the `scipy.ndimage.filters` namespace is deprecated and will be removed in SciPy 2.0.0.
data_max = filters.maximum_filter(image, size)
189
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
KERNEL_SIZE = 5
THRESHOLD = 0.2
# Find maximums
h1 = h_fun(img1, KERNEL_SIZE)
m1 = find_max(h1, KERNEL_SIZE, THRESHOLD)
draw_points(img1_color, m1)
plt.show()
C:\Users\Ashish\AppData\Local\Temp\ipykernel_24548\2343967864.py:27:
DeprecationWarning: Please import `maximum_filter` from the `scipy.ndimage` namespace;
the `scipy.ndimage.filters` namespace is deprecated and will be removed in SciPy 2.0.0.
data_max = filters.maximum_filter(image, size)
KERNEL_SIZE = 5
THRESHOLD = 0.3
# Find maximums
h1 = h_fun(img1, KERNEL_SIZE)
m1 = find_max(h1, KERNEL_SIZE, THRESHOLD)
draw_points(img1_color, m1)
plt.show()
C:\Users\Ashish\AppData\Local\Temp\ipykernel_24548\2343967864.py:27:
DeprecationWarning: Please import `maximum_filter` from the `scipy.ndimage` namespace;
the `scipy.ndimage.filters` namespace is deprecated and will be removed in SciPy 2.0.0.
data_max = filters.maximum_filter(image, size)
190
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
KERNEL_SIZE = 7
THRESHOLD = 0.1
# Find maximums
h1 = h_fun(img1, KERNEL_SIZE)
m1 = find_max(h1, KERNEL_SIZE, THRESHOLD)
draw_points(img1_color, m1)
plt.show()
C:\Users\Ashish\AppData\Local\Temp\ipykernel_24548\2343967864.py:27:
DeprecationWarning: Please import `maximum_filter` from the `scipy.ndimage` namespace;
the `scipy.ndimage.filters` namespace is deprecated and will be removed in SciPy 2.0.0.
data_max = filters.maximum_filter(image, size)
191
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
KERNEL_SIZE = 7
THRESHOLD = 0.2
# Find maximums
h1 = h_fun(img1, KERNEL_SIZE)
m1 = find_max(h1, KERNEL_SIZE, THRESHOLD)
draw_points(img1_color, m1)
plt.show()
C:\Users\Ashish\AppData\Local\Temp\ipykernel_24548\2343967864.py:27:
DeprecationWarning: Please import `maximum_filter` from the `scipy.ndimage` namespace;
the `scipy.ndimage.filters` namespace is deprecated and will be removed in SciPy 2.0.0.
data_max = filters.maximum_filter(image, size)
KERNEL_SIZE = 7
THRESHOLD = 0.3
# Find maximums
h1 = h_fun(img1, KERNEL_SIZE)
m1 = find_max(h1, KERNEL_SIZE, THRESHOLD)
draw_points(img1_color, m1)
plt.show()
C:\Users\Ashish\AppData\Local\Temp\ipykernel_24548\2343967864.py:27:
DeprecationWarning: Please import `maximum_filter` from the `scipy.ndimage` namespace;
the `scipy.ndimage.filters` namespace is deprecated and will be removed in SciPy 2.0.0.
data_max = filters.maximum_filter(image, size)
192
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Conclusions:
In this experiment, we explored several key techniques for blob and corner detection have been
implemented. The Laplacian of Gaussian (LOG) was used to detect blobs by highlighting
regions of rapid intensity change, combining the smoothing (Gaussian) and edge detection
(Laplacian) depicting the edges and round regions within the image. Difference of Gaussian
(DoG), a faster approximation of LoG, successfully identified blobs by subtracting Gaussian-
blurred images, emphasizing differences at varying scales. The Determinant of Hessian (DoH)
method, although typically more computationally intensive, detected blob-like structures by
analyzing intensity changes across multiple directions.
Histogram of oriented gradients (HoG) technique was used object detection by capturing the
gradient
direction and magnitude within localized regions of an image. It represents the shape and
structure of objects by summarizing the distribution of gradient orientations, making it effective
for image classification.
For corner detection, the Harris Detection method was applied which highlights the significant
intensity variations, which is important for feature matching and object recognition. There are
two important parameters, kernel size and threshold. As the kernel size is increased, the detector
becomes more sensitive to the larger features but may smooth out smaller and fine details. On
the other hand, as the threshold is increased, the number of corners detected is reduced, because
it makes the detector more selective and less sensitive to the weak corners.
193
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Div: A Batch: 1
Date of performance: 03-09-2024
Experiment No. 9
Problem Statement:
SIFT - based Cats n Dogs classifier
AIM:
1.To develop a SIFT-based feature extraction process for classifying images of cats and dogs.
2.To use K-Nearest Neighbours (KNN) for classifying the extracted SIFT features.
3.To implement and evaluate clustering techniques like K-means for organizing SIFT features.
Objective(s) of Experiment:
To classify images of cats and dogs by extracting SIFT features, clustering them, and using
KNN classification.
Introduction:
SIFT (Scale-Invariant Feature Transform) is a popular algorithm in computer vision used for
detecting and describing local features in images. It is particularly useful because it is scale-
invariant, meaning it can detect the same key points even when an image is resized, rotated, or
transformed. SIFT works by identifying key points (features) in an image that remain consistent
under different viewing conditions. These key points are then described using a feature
descriptor, which provides a rich set of information that can be used for matching similar
objects or classifying images. Once SIFT features are extracted, they can be used for tasks like
object recognition and image classification. In classification tasks, algorithms like K-Nearest
Neighbours (KNN) are commonly used. KNN is a simple, non-parametric algorithm that
classifies new data points based on the majority class of their nearest neighbours in feature
space. It relies on the assumption that similar data points are close to each other, making it a
natural fit for organizing and classifying image features extracted through methods like SIFT.
By combining feature extraction with classification techniques like KNN, we can create robust
systems capable of distinguishing between different objects or categories, such as identifying
cats and dogs in images.
Flowchart:
194
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
#cleanup.clean('SIFT')
for i in temp:
count = 0
for filename in os.listdir(input0 + i):
img = cv2.imread(input0 + i + '/' + filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# pre processing - resize, contrast enhancement - Histogram equliazation
# Image blur- Gaussian
# Prewit / Sobel - structural
#initialise sift descriptor
sift = cv2.SIFT_create(nfeatures = 1000)
keypoints, descriptors = sift.detectAndCompute(gray, None)
sift_image = cv2.drawKeypoints(gray, keypoints, img)
# display image with keypoints
#convert the descriptor array into a dataframe format
out=pd.DataFrame(descriptors)
print("descriptor shape ",i, count, " : ", out.shape)
#append to the csv file
csv_data=out.to_csv('./Dataset/SIFT/SIFT_' + i + '.csv', mode='a', index=False)
count += 1
if count == 50:
break
print(i + ": " + str(count))
descriptor shape Cats 0 : (382, 128)
descriptor shape Cats 1 : (359, 128)
descriptor shape Cats 2 : (180, 128)
descriptor shape Cats 3 : (273, 128)
descriptor shape Cats 4 : (506, 128)
descriptor shape Cats 5 : (486, 128)
descriptor shape Cats 6 : (418, 128)
descriptor shape Cats 7 : (88, 128)
descriptor shape Cats 8 : (345, 128)
descriptor shape Cats 9 : (413, 128)
descriptor shape Cats 10 : (168, 128)
descriptor shape Cats 11 : (154, 128)
descriptor shape Cats 12 : (178, 128)
descriptor shape Cats 13 : (165, 128)
descriptor shape Cats 14 : (417, 128)
descriptor shape Cats 15 : (123, 128)
descriptor shape Cats 16 : (213, 128)
descriptor shape Cats 17 : (187, 128)
descriptor shape Cats 18 : (473, 128)
descriptor shape Cats 19 : (491, 128)
descriptor shape Cats 20 : (361, 128)
descriptor shape Cats 21 : (299, 128)
descriptor shape Cats 22 : (138, 128)
descriptor shape Cats 23 : (155, 128)
descriptor shape Cats 24 : (165, 128)
descriptor shape Cats 25 : (158, 128)
descriptor shape Cats 26 : (491, 128)
descriptor shape Cats 27 : (173, 128)
196
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
197
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
120
121
122
123
124
125
126
127
0
3
25
5
2
9
97
21
1
29
45
…
0
0
0
0
0
0
0
0
0
0
1
199
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
100
45
0
0
1
2
3
16
45
23
…
2
94
15
0
0
22
119
24
2
9
2
65
29
0
1
7
8
9
17
143
200
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
42
…
5
73
90
0
0
3
14
9
4
90
3
42
3
1
37
79
0
0
12
49
7
…
0
27
22
102
15
3
0
201
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
0
0
0
4
42
3
1
35
55
1
1
17
22
0
…
27
31
2
85
143
3
2
2
1
0
…
…
…
…
…
…
202
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
14190
5
111
110
0
0
0
0
0
89
166
…
1
16
18
203
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
28
1
0
0
0
11
17
14191
49
0
0
0
0
0
0
12
32
0
…
0
21
119
10
0
0
0
0
1
47
14192
44
204
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
86
22
7
0
0
2
27
16
28
…
0
0
2
2
0
0
0
0
0
0
14193
0
4
5
0
0
1
1
0
76
121
205
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
…
5
15
62
62
8
13
5
0
0
1
14194
3
0
0
0
0
0
0
0
42
2
…
9
20
33
61
26
4
0
0
206
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
8
20
14195 rows × 128 columns
data2
0
1
2
3
4
5
6
7
8
9
…
118
119
120
121
122
123
124
125
126
127
0
0
0
0
0
0
207
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
0
0
0
0
0
…
0
0
52
14
0
2
7
0
0
0
1
0
0
0
0
0
0
0
0
36
12
…
5
23
73
208
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
34
1
9
15
0
0
0
2
38
0
0
0
0
0
12
146
55
2
…
0
1
22
2
0
2
7
1
1
1
3
1
209
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
0
0
0
24
146
12
1
119
1
…
13
13
7
1
0
1
146
13
1
2
4
0
0
2
150
95
0
0
0
0
2
210
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
…
22
69
21
9
2
0
0
0
5
20
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
211
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
…
…
14954
17
0
0
1
13
2
3
58
10
2
…
22
33
40
3
2
9
93
56
1
4
14955
22
78
17
11
69
13
212
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
4
5
149
20
…
0
0
0
0
8
49
3
0
0
0
14956
109
40
3
0
0
0
0
23
110
10
…
6
25
35
67
213
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
2
1
87
125
14
6
14957
25
6
3
37
94
38
4
1
165
57
…
0
3
0
0
0
3
79
8
0
0
14958
53
43
214
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
6
9
8
9
6
12
46
100
…
6
38
5
0
0
0
0
1
20
20
14959 rows × 128 columns
data_final = pd.concat([data1,data2])
inertias = []
for i in range(1,35):
kmeans = KMeans(n_clusters=i)
kmeans.fit(data_final)
inertias.append(kmeans.inertia_)
215
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
kmeans3 = KMeans(n_clusters=10)
kmeans3.fit(pd.concat([data1, data2]))
joblib.dump(kmeans3, './Dataset/SIFT/Trained_Models/Kmeans_A')
['./Dataset/SIFT/Trained_Models/Kmeans_A']
kmeans3
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the
notebook. On GitHub, the HTML representation is unable to render, please try loading this page
with nbviewer.org.
KMeans?Documentation for KMeansiFitted
c=0
for i in temp:
data = []
path_to_folder = input0+i
print(path_to_folder)
for fname in os.listdir(path_to_folder):
path_to_file = path_to_folder+'/'+fname
img = cv2.imread(path_to_file)
sift = cv2.SIFT_create()
keypoints, descriptors = sift.detectAndCompute(img, None)
out1 = pd.DataFrame(descriptors)
array_double = np.array(out1, dtype=np.double)
a=kmeans3.predict(array_double)
216
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
# BVOW, BOW
hist=np.histogram(a,bins=10) #the range of 10 cluster assignments is divided into 6
intervals.
data.append(hist[0])
# csv_data = out1.to_csv(r'SIFT\SIFT_{}.csv'.format(i), mode='a', index=False)
Output = pd.DataFrame(data)
Output["Class"] = c
csv_data=Output.to_csv(r'./Dataset/SIFT/SIFT_Final_{}.csv'.format(i), mode='a',
index=False)
c += 1
C:/Users/Ashish/Desktop/CVFolder/Cats
C:/Users/Ashish/Desktop/CVFolder/Dog
Output_Initial= pd.concat([data1, data2])
len(a)
247
a # kmeans predicted cluster labels between 0 to 9
array([1, 2, 3, 7, 2, 2, 4, 8, 4, 2, 8, 2, 6, 3, 2, 6, 3, 2, 8, 4, 2, 1,
4, 3, 2, 3, 2, 2, 8, 7, 1, 7, 2, 0, 4, 0, 9, 9, 7, 2, 1, 9, 8, 7,
2, 9, 9, 2, 4, 4, 0, 6, 7, 9, 0, 2, 5, 7, 9, 5, 2, 2, 2, 3, 7, 9,
8, 2, 6, 2, 0, 0, 3, 2, 8, 1, 5, 0, 2, 0, 2, 8, 8, 2, 0, 5, 6, 3,
6, 8, 8, 9, 0, 4, 2, 4, 4, 6, 9, 0, 5, 0, 2, 2, 8, 2, 7, 2, 7, 6,
4, 2, 5, 5, 4, 6, 0, 1, 7, 0, 0, 9, 6, 4, 2, 3, 6, 0, 3, 8, 3, 6,
3, 0, 5, 7, 4, 2, 2, 5, 5, 7, 6, 3, 3, 3, 2, 5, 3, 9, 3, 9, 1, 1,
0, 7, 1, 2, 9, 9, 0, 5, 4, 4, 2, 7, 3, 7, 2, 7, 4, 2, 2, 9, 0, 3,
3, 3, 4, 9, 4, 9, 1, 9, 3, 3, 2, 2, 1, 8, 6, 3, 2, 8, 6, 7, 6, 4,
9, 8, 4, 0, 4, 4, 5, 0, 7, 2, 0, 5, 9, 8, 1, 4, 1, 6, 6, 0, 9, 3,
4, 9, 8, 6, 0, 6, 1, 5, 3, 3, 6, 6, 5, 2, 9, 9, 9, 2, 2, 2, 4, 4,
4, 7, 7, 1, 6])
hist # Clusters distributed in 10 bins
(array([25, 15, 48, 27, 28, 16, 23, 21, 18, 26], dtype=int64),
array([0. , 0.9, 1.8, 2.7, 3.6, 4.5, 5.4, 6.3, 7.2, 8.1, 9. ]))
len(data)
54
data # All features histograms are appended in data
[array([18, 19, 39, 21, 32, 15, 19, 18, 14, 12], dtype=int64),
array([36, 33, 73, 32, 39, 31, 41, 40, 22, 27], dtype=int64),
array([18, 23, 51, 25, 50, 17, 22, 23, 21, 21], dtype=int64),
array([25, 7, 46, 23, 29, 19, 20, 11, 21, 15], dtype=int64),
array([18, 9, 34, 19, 33, 19, 13, 12, 12, 16], dtype=int64),
array([39, 18, 42, 28, 35, 31, 28, 12, 28, 29], dtype=int64),
array([34, 17, 68, 35, 50, 34, 30, 21, 57, 33], dtype=int64),
array([18, 7, 11, 16, 23, 17, 16, 6, 28, 19], dtype=int64),
217
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
array([27, 29, 54, 35, 36, 39, 33, 22, 59, 48], dtype=int64),
array([34, 19, 24, 33, 41, 25, 30, 13, 54, 43], dtype=int64),
array([28, 30, 43, 47, 48, 30, 33, 21, 69, 64], dtype=int64),
array([49, 27, 36, 48, 43, 58, 48, 26, 37, 39], dtype=int64),
array([53, 81, 52, 40, 59, 53, 37, 33, 61, 61], dtype=int64),
array([52, 34, 49, 48, 43, 51, 58, 16, 27, 29], dtype=int64),
array([25, 12, 42, 34, 26, 28, 33, 12, 14, 32], dtype=int64),
array([17, 10, 33, 18, 23, 21, 25, 15, 16, 8], dtype=int64),
array([28, 19, 48, 25, 36, 25, 29, 18, 15, 11], dtype=int64),
array([46, 30, 54, 49, 56, 51, 44, 27, 32, 22], dtype=int64),
array([18, 13, 42, 24, 32, 25, 24, 12, 32, 25], dtype=int64),
array([33, 17, 38, 28, 10, 27, 27, 16, 26, 17], dtype=int64),
array([38, 23, 42, 32, 26, 34, 34, 14, 41, 39], dtype=int64),
array([22, 21, 24, 22, 40, 14, 18, 12, 54, 23], dtype=int64),
array([20, 5, 49, 29, 43, 21, 43, 18, 12, 14], dtype=int64),
array([40, 61, 40, 55, 72, 49, 42, 44, 55, 45], dtype=int64),
array([37, 12, 26, 49, 54, 32, 48, 5, 14, 15], dtype=int64),
array([13, 24, 6, 7, 8, 16, 7, 12, 11, 9], dtype=int64),
array([40, 26, 47, 32, 35, 33, 42, 26, 28, 18], dtype=int64),
array([17, 26, 17, 17, 13, 18, 14, 10, 13, 17], dtype=int64),
array([45, 28, 51, 45, 44, 39, 39, 30, 32, 22], dtype=int64),
array([18, 15, 34, 12, 20, 19, 16, 6, 7, 11], dtype=int64),
array([39, 17, 66, 43, 44, 43, 36, 25, 42, 29], dtype=int64),
array([11, 6, 51, 20, 21, 26, 16, 3, 27, 18], dtype=int64),
array([18, 6, 36, 17, 21, 18, 14, 6, 10, 20], dtype=int64),
array([22, 16, 41, 22, 29, 20, 19, 28, 23, 17], dtype=int64),
array([12, 15, 18, 17, 20, 15, 11, 20, 6, 1], dtype=int64),
array([13, 11, 20, 9, 32, 14, 19, 12, 9, 12], dtype=int64),
array([49, 18, 35, 60, 53, 40, 41, 13, 40, 36], dtype=int64),
array([25, 8, 27, 18, 32, 26, 27, 15, 40, 31], dtype=int64),
array([29, 33, 48, 52, 56, 37, 52, 22, 25, 15], dtype=int64),
array([53, 25, 64, 47, 51, 45, 49, 24, 45, 31], dtype=int64),
array([32, 18, 43, 29, 49, 36, 32, 23, 35, 37], dtype=int64),
array([45, 23, 61, 45, 46, 27, 34, 19, 35, 48], dtype=int64),
array([25, 17, 69, 28, 41, 23, 31, 14, 31, 31], dtype=int64),
array([43, 40, 27, 28, 22, 38, 34, 20, 34, 30], dtype=int64),
array([46, 25, 70, 38, 59, 25, 40, 36, 25, 26], dtype=int64),
array([24, 10, 34, 26, 36, 15, 34, 14, 40, 23], dtype=int64),
array([43, 25, 54, 42, 54, 41, 51, 27, 33, 31], dtype=int64),
array([12, 26, 73, 16, 57, 24, 23, 22, 41, 29], dtype=int64),
array([32, 27, 37, 22, 54, 37, 24, 20, 36, 12], dtype=int64),
array([21, 28, 51, 21, 51, 20, 21, 21, 42, 17], dtype=int64),
array([38, 6, 39, 26, 15, 33, 27, 5, 12, 20], dtype=int64),
array([26, 18, 46, 34, 38, 23, 36, 11, 54, 44], dtype=int64),
array([12, 12, 38, 17, 29, 7, 28, 13, 4, 9], dtype=int64),
array([25, 15, 48, 27, 28, 16, 23, 21, 18, 26], dtype=int64)]
1
dfp = pd.read_csv('./Dataset/SIFT/SIFT_Final_Cats.csv')
dfn = pd.read_csv('./Dataset/SIFT/SIFT_Final_Dog.csv')
218
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
df = pd.concat([dfp, dfn])
csv_data = df.to_csv('./Dataset/SIFT/SIFT_Final.csv')
df.shape
(104, 11)
X = df.drop(df.columns[[0, -1]], axis=1)
X.shape
(104, 9)
df = pd.read_csv('./Dataset/SIFT/SIFT_Final.csv')
X = df.drop(df.columns[[0, -1]], axis=1) # selects all rows (:) and all but the last column (:-1)
of the DataFrame df.
Y = df.iloc[:, -1] # selects all rows (:) and the last column (-1) of the DataFrame df.
219
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
6 0
66 1
17 0
56 1
28 0
48 0
98 1
35 0
Name: Class, dtype: int64
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
train_X = sc.fit_transform(x_train)
test_X = sc.transform(x_test)
train_X
array([[-0.78219695, -1.12840664, -1.32705455, -1.04375126, -0.82826934,
-0.58287821, -1.09410278, -0.86113728, -1.06198976, -1.02395088],
[-0.98550283, -1.00106775, -1.26468449, -0.90870628, -0.68910668,
-1.1381596 , -0.6765831 , -0.79574442, -0.52386308, -0.95617742],
[ 0.30210112, -0.30070388, -0.70335394, 0.10413107, 0.70251995,
-0.23582734, -0.11989019, -0.66495871, 1.14432962, 1.28034688],
[-1.52765186, -1.0647372 , 0.98063771, -1.44888619, -0.75868801,
-1.20756977, -1.233276 , -1.18810157, -1.16961509, -1.22727127],
[-0.30781654, -0.74638998, 0.41930716, 0.17165356, -0.34120002,
-0.02759682, 0.08886965, -0.73035156, -1.00817709, 0.53483878],
[ 1.04755603, -0.0460261 , 1.60433832, 0.91440094, 1.0504266 ,
-0.09700699, 0.15845626, -0.27260155, 0.12188893, 1.61921419],
[-0.78219695, -0.55538165, -0.07965333, -1.31384121, -0.75868801,
-0.65228838, -1.09410278, -1.12270872, -1.38486576, -0.88840396],
[-0.10451066, 0.39966 , 0.48167722, 1.04944592, 1.18958927,
0.11122353, 0.08886965, -0.14181584, 1.95151964, 2.70358961],
[ 0.70871289, 0.59066833, -1.20231443, 1.1844909 , -0.13245603,
0.59709475, 0.08886965, 0.25054131, 1.30576762, -0.07512239],
[-1.12104009, -1.19207608, -0.765724 , -0.63861632, -0.54994402,
-0.99933925, -0.60699648, -1.12270872, -0.25479974, -0.21066932],
[-0.30781654, -0.55538165, 0.79352752, -0.30100387, -0.20203736,
-0.8605189 , -0.60699648, -0.14181584, -0.79292642, 0.128198 ],
[ 0.505407 , 0.33599056, -1.01520425, 0.5767885 , -1.03701334,
-0.09700699, 1.06308224, 0.1197556 , 0.71382828, 0.128198 ],
[-1.18880872, -0.68272054, -0.70335394, -1.04375126, -0.61952535,
-1.1381596 , -1.16368939, -0.46878013, -0.36242508, -1.02395088],
[-0.64665969, -0.61905109, -0.57861382, -0.57109383, -1.10659467,
-0.92992908, -0.60699648, -0.73035156, -1.16961509, -1.22727127],
[ 0.09879523, -0.0460261 , -0.01728327, 0.5767885 , -0.68910668,
0.25004388, 1.20225546, 0.64289846, 0.22951427, -0.21066932],
[-1.12104009, -0.81005942, -0.95283418, -1.51640868, 0.07628797,
-0.99933925, -0.88534294, -0.73035156, -1.27724043, -0.82063049],
[ 0.64094426, -0.36437332, 0.41930716, -0.23348138, 0.28503196,
0.18063371, -0.25906342, -0.73035156, -0.25479974, 0.33151839],
220
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
221
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
222
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
# Save model
joblib.dump(model_knn, "./Dataset/SIFT/Trained_Models/modelA_knn")
# Predict
y_pred3 = model_knn.predict(x_test)
# Print metrics
print("KNN Classifier")
print("Train Accuracy:", model_knn.score(x_train, y_train))
print("Test Accuracy:", model_knn.score(x_test, y_test))
print("Precision Score:", precision_score(y_test, y_pred3, average='micro'))
print("Recall Score:", recall_score(y_test, y_pred3, average='micro'))
print("F1 Score:", f1_score(y_test, y_pred3, average='micro'))
# Confusion Matrix
print("Confusion Matrix:")
print(confusion_matrix(y_test, y_pred3))
224
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
# ROC Curve
RocCurveDisplay.from_estimator(model_knn, x_test, y_test)
KNN Classifier
Train Accuracy: 0.8554216867469879
Test Accuracy: 0.7619047619047619
Precision Score: 0.7619047619047619
Recall Score: 0.7619047619047619
F1 Score: 0.7619047619047619
Confusion Matrix:
[[ 6 4]
[ 1 10]]
<sklearn.metrics._plot.roc_curve.RocCurveDisplay at 0x1bbd5b68a40>
confusion_matrix(y_test, y_pred3)
array([[ 6, 4],
[ 1, 10]], dtype=int64)
DC=accuracy_score(y_test, y_pred3)*100
DC
76.19047619047619
from tabulate import tabulate
#from prettytable import PrettyTable
mydata = confusion_matrix(y_test, y_pred3)
225
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
# create header
head = ["Predicted No", "Predicted Yes"]
row=["Actual No","Actual Yes"]
# display table
print(tabulate(mydata, headers=head,showindex=row, tablefmt="grid"))
+------------+----------------+-----------------+
| | Predicted No | Predicted Yes |
+============+================+=================+
| Actual No | 6| 4|
+------------+----------------+-----------------+
| Actual Yes | 1| 10 |
+------------+----------------+-----------------+
dfc = pd.DataFrame(mydata)
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
cm = confusion_matrix(y_test, y_pred3, normalize='all')
cmd = ConfusionMatrixDisplay(cm, display_labels=['Cats','Dogs'])
cmd.plot()
<sklearn.metrics._plot.confusion_matrix.ConfusionMatrixDisplay at 0x1bbd5b410a0>
import joblib
# Perform a grid search over the parameter grid using 5-fold cross-validation
grid_search_knn = GridSearchCV(knn, param_grid, cv=5)
# Confusion Matrix
print("Confusion Matrix:")
print(confusion_matrix(y_test, y_pred3))
# ROC Curve
RocCurveDisplay.from_estimator(grid_search_knn, x_test, y_test)
Best hyperparameters: {'metric': 'euclidean', 'n_neighbors': 5, 'weights': 'uniform'}
Best cross-validated score: 0.8286764705882353
KNN with GridSearchCV
Train Accuracy: 0.8433734939759037
Test Accuracy: 0.8095238095238095
Precision Score: 0.8095238095238095
Recall Score: 0.8095238095238095
F1 Score: 0.8095238095238095
Confusion Matrix:
227
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
[[ 7 3]
[ 1 10]]
<sklearn.metrics._plot.roc_curve.RocCurveDisplay at 0x1bbd4f96d80>
def siftFeatures(path):
img = cv2.imread(path)
gray = cv2.resize(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), (128, 128))
data = modelKmeansA.predict(features)
hist = np.histogram(data,bins=10)
return pd.DataFrame([hist[0]])
228
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
import joblib
import os
import cv2
import matplotlib.pyplot as plt
if os.path.exists(model_path):
# Load the model
modelA = joblib.load(model_path)
else:
raise FileNotFoundError(f"Model file not found at {model_path}. Please check the path.")
229
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
modelA_proba
array([[1., 0.]])import joblib
import cv2
import matplotlib.pyplot as plt
import os
# Define paths for model and image
img_path = './Dog/dog10.jpeg'
model_path = './Dataset/SIFT/Trained_Models/modelA_knn_tuned'
# Check if the model file exists
if os.path.exists(model_path):
# Load the model
modelA = joblib.load(model_path)
else:
raise FileNotFoundError(f"Model file not found at {model_path}. Please check the path.")
# Check if the image file exists
if os.path.exists(img_path):
# Extract features and predict using the loaded model
features = featureReduction(siftFeatures(img_path))
modelA_pred = modelA.predict(features)
modelA_proba = modelA.predict_proba(features)
else:
raise FileNotFoundError(f"Image file not found at {img_path}. Please check the path.")
# Output the prediction
print("CAT" if modelA_pred else "DOG")
# Read and display the image
img = cv2.imread(img_path)
if img is None:
raise FileNotFoundError(f"Image file not found at {img_path}. Please check the path.")
230
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
modelA_proba
array([[0.8, 0.2]])
import joblib
import cv2
import matplotlib.pyplot as plt
import os
# Define paths for model and image
img_path = './Dog/dog30.jpeg'
model_path = './Dataset/SIFT/Trained_Models/modelA_knn_tuned' # Corrected the path
# Check if the model file exists
if os.path.exists(model_path):
# Load the model
modelA = joblib.load(model_path)
else:
raise FileNotFoundError(f"Model file not found at {model_path}. Please check the path.")
# Check if the image file exists
if os.path.exists(img_path):
# Extract features and predict using the loaded model
features = featureReduction(siftFeatures(img_path))
modelA_pred = modelA.predict(features)
modelA_proba = modelA.predict_proba(features)
231
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
else:
raise FileNotFoundError(f"Image file not found at {img_path}. Please check the path.")
# Output the prediction
print("CAT" if modelA_pred else "DOG")
# Read and display the image
img = cv2.imread(img_path)
if img is None:
raise FileNotFoundError(f"Image file not found at {img_path}. Please check the path.")
# Convert from BGR to RGB for correct plotting
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Display the image
plt.imshow(img_rgb)
plt.axis('off') # Hide axis
plt.show()
DOG
modelA_proba
array([[0.6, 0.4]])
import joblib
import cv2
import matplotlib.pyplot as plt
import os
# Define paths for model and image
img_path = './Cats/cat15.jpeg'
model_path = './Dataset/SIFT/Trained_Models/modelA_knn_tuned'
# Check if the model file exists
if os.path.exists(model_path):
# Load the model
modelA = joblib.load(model_path)
else:
232
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
raise FileNotFoundError(f"Model file not found at {model_path}. Please check the path.")
# Check if the image file exists
if os.path.exists(img_path):
# Extract features and predict using the loaded model
features = featureReduction(siftFeatures(img_path))
modelA_pred = modelA.predict(features)
modelA_proba = modelA.predict_proba(features)
else:
raise FileNotFoundError(f"Image file not found at {img_path}. Please check the path.")
# Output the prediction
print("CAT" if modelA_pred else "DOG")
233
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
modelA_proba
array([[0.6, 0.4]])
import joblib
import cv2
import matplotlib.pyplot as plt
import os
# Define paths for model and image
img_path = './Cats/cat24.jpeg'
model_path = './Dataset/SIFT/Trained_Models/modelA_knn_tuned'
# Check if the model file exists
if os.path.exists(model_path):
# Load the model
modelA = joblib.load(model_path)
else:
raise FileNotFoundError(f"Model file not found at {model_path}. Please check the path.")
# Check if the image file exists
if os.path.exists(img_path):
# Extract features and predict using the loaded model
features = featureReduction(siftFeatures(img_path))
modelA_pred = modelA.predict(features)
modelA_proba = modelA.predict_proba(features)
else:
raise FileNotFoundError(f"Image file not found at {img_path}. Please check the path.")
# Output the prediction
print("CAT" if modelA_pred else "DOG")
# Read and display the image
img = cv2.imread(img_path)
if img is None:
raise FileNotFoundError(f"Image file not found at {img_path}. Please check the path.")
# Convert from BGR to RGB for correct plotting
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
234
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
modelA_proba
array([[0.8, 0.2]])
Conclusion:
In this experiment, we successfully implemented an image classification system based on the
Scale-Invariant Feature Transform (SIFT) to differentiate between images of cats and dogs.
SIFT was chosen for feature extraction due to its effectiveness in identifying key points that are
stable under various transformations, such as scaling, rotation, and changes in lighting
conditions. This robustness makes SIFT particularly suitable for real-world scenarios where
image conditions can vary significantly. After extracting SIFT features, we applied K-means
clustering to group these features into distinct clusters. This clustering process not only reduced
the dimensionality of the feature space but also helped uncover patterns within the data. By
creating a "visual vocabulary" from these feature clusters, each image could be represented as a
histogram of cluster occurrences, effectively converting it into a feature vector that could be
used for classification with K-Nearest Neighbors (KNN). 10 clusters were used.
KNN, a simple yet powerful classifier, was then used to classify the images into two categories:
cats and dogs. The combined approach of SIFT for feature extraction, K-means for clustering,
and KNN for classification yielded promising results in distinguishing between cats and dogs,
demonstrating a high level of accuracy. This highlights the effectiveness of SIFT in feature
extraction and KNN in classification tasks within computer vision. However, there are some
limitations to this approach. For instance, the performance of K-means clustering depends on
the chosen number of clusters, and KNN requires careful tuning of the parameters to avoid
overfitting or underfitting. Moreover, the computational time required for SIFT feature
235
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
extraction and K-means clustering can be considerable, especially when dealing with large
ET3221: Computer
datasets. This Vision
indicates that while the SIFT-K-means-KNN pipeline is effective, it may not be
ideal for real-time applications unless optimizations or parallel processing techniques are
Name
applied.of the student: Ashish Rodi Roll No. 9
Div: A Batch: 1
Date of performance: 10-09-2024
Experiment No. 10
Problem Statement:
Human detection using HOG
AIM:
1. Resize the image to 64x128, calculate HOG features with smaller 8x8 cells, create the
feature vector, and use KNN for classification.
2. Resize the image to 64x128, compute HOG features using larger 32x32 cells, generate
the feature vector, and classify with KNN.
3. Blur the 64x128 image with Gaussian filtering, apply Prewitt X and Y edge detection,
extract HOG features, and classify using KNN.
4. Detect blobs in the 64x128 image using DoG, compute HOG features, create the feature
vector, and classify using KNN.
Objective(s) of Experiment:
To implement human detection using HOG feature descriptors and understand how HOG helps
in object detection by capturing shape and appearance information.
Introduction:
Human detection is a fundamental task in computer vision, useful in a variety of applications
such as surveillance, autonomous driving, and human-computer interaction. One of the most
effective methods for human detection is the Histogram of Oriented Gradients (HOG)
descriptor, which extracts features based on the gradients of an image's pixel intensities.
HOG works by dividing an image into small regions called cells, calculating the gradient
orientation for each pixel, and constructing histograms of gradient directions within each cell.
These histograms capture the shape and appearance of objects by emphasizing the edges and
contours in the image. The HOG feature descriptor, when combined with a classifier such as a
support vector machine (SVM), can be used to effectively detect objects, particularly humans,
in various images.
HOG is especially useful in human detection because it is robust to variations in lighting and
pose, which are common challenges in real-world environments. By detecting edges and
gradients, HOG helps in capturing the distinctive outline of humans, making it ideal for object
detection tasks like pedestrian recognition.
Flowchart:
236
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
237
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
warnings.filterwarnings('ignore')
from sklearn.cluster import KMeans
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.tree import DecisionTreeClassifier
import sklearn.metrics as metrics
from sklearn.metrics import accuracy_score
input0 = 'C:/Users/Ashish/Desktop/CVFolder/'
temp = ['1', '0']
count = 0
# Check if the output directory exists; if not, create it
output_dir = './Datasets/HOG/'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
238
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
# Convert HOG features into a DataFrame and transpose to have one row of features
out = pd.DataFrame(fd).T
print("Descriptor shape ", i, count, " : ", out.shape)
count += 1
if count == 50: # Limit to first 50 images
break
0: 50
data1 = pd.read_csv('./Datasets/HOG/HOG_1.csv', dtype='float64')
data2 = pd.read_csv('./Datasets/HOG/HOG_0.csv', dtype='float64')
print(data1.head())
print(data2.head())
print(data1.dtypes)
print(data2.dtypes)
0 1 2 3 4 5 6 \
0 0.258659 0.002152 0.025606 0.041955 0.124544 0.101784 0.186634
1 0.340736 0.103812 0.197419 0.000000 0.105524 0.033571 0.024925
2 0.028492 0.021237 0.037175 0.137302 0.331552 0.220111 0.026863
3 0.210885 0.038038 0.034926 0.114505 0.155678 0.135960 0.264799
4 0.375628 0.041870 0.065953 0.043860 0.270782 0.099272 0.139203
df = pd.concat([dfp, dfn])
csv_data = df.to_csv('./Datasets/HOG/HOG_Final.csv')
X_train
array([[0.26877325, 0.26877325, 0.26877325, ..., 0.00161489, 0.08702827,
0.27348235],
[0.01933716, 0. , 0. , ..., 0.15141459, 0.17664369,
0.12057064],
[0.45790657, 0. , 0. , ..., 0.03672928, 0.03871606,
241
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
0. ],
...,
[0.24940433, 0.00123831, 0.00656413, ..., 0.07061544, 0.02864682,
0.05245014],
[0.29962926, 0.12220875, 0. , ..., 0.00584205, 0.01448974,
0.29104826],
[0.20996119, 0.18297707, 0.18440202, ..., 0.02682564, 0.14452345,
0.10153207]])
X_test
array([[0.28068686, 0. , 0.00928159, ..., 0.27659436, 0.10303338,
0.00557759],
[0.34301887, 0. , 0.01781956, ..., 0.05961435, 0.07412865,
0.16114186],
[0.26669595, 0.05213381, 0.03408786, ..., 0.10750376, 0.20026886,
0.23614985],
...,
[0.29126872, 0.07604202, 0.03359546, ..., 0.2470542 , 0.2470542 ,
0.22815049],
[0.2688807 , 0.0853415 , 0.13952609, ..., 0.03989037, 0.17349667,
0.13803452],
[0.14365397, 0.00127804, 0.0762536 , ..., 0.17424213, 0.10331287,
0.04870216]])
y_train
array([1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0,
0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0,
1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1,
0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1], dtype=int64)
y_test
[1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0]
#KNN classifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score,
RocCurveDisplay
import joblib
joblib.dump(model_knn,"./Datasets/HOG/Trained_Models/modelA_knn")
y_pred3 = model_knn.predict(X_test)
print("KNN Classifier")
print("Train Accuracy:",model_knn.score(X_train, y_train))
print("Test Accuracy:",model_knn.score(X_test, y_test))
print("Precision Score: ",metrics.precision_score(y_test, y_pred3, pos_label='positive',
average='micro'))
242
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
<sklearn.metrics._plot.roc_curve.RocCurveDisplay at 0x1e21d94f9b0>
for i in temp:
folder_path = os.path.join(input0, i)
if not os.path.exists(folder_path):
print(f"Warning: The subfolder {folder_path} does not exist.")
continue
count = 0
# Check if the output directory exists; if not, create it
output_dir = './Datasets/HOG2/'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
244
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
# Convert HOG features into a DataFrame and transpose to have one row of features
out = pd.DataFrame(fd).T
print("Descriptor shape ", i, count, " : ", out.shape)
count += 1
if count == 50: # Limit to first 50 images
break
df = pd.concat([dfp, dfn])
csv_data = df.to_csv('./Datasets/HOG2/HOG_Final.csv')
df
100 rows × 108 columns
X_train
247
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
248
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
# Prewitt kernels
prewitt_x = np.array([
[-1, 0, 1],
[-1, 0, 1],
[-1, 0, 1]
])
prewitt_y = np.array([
[-1, -1, -1],
[0, 0, 0],
[1, 1, 1]
])
def apply_prewitt_edge_detection(image):
# Apply Prewitt operator
edges_x = convolve(image, prewitt_x)
edges_y = convolve(image, prewitt_y)
# Compute the magnitude of gradients
edges = np.sqrt(edges_x**2 + edges_y**2)
return edges
count = 0
# Check if the output directory exists; if not, create it
output_dir = './Datasets/HOG3/'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
250
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
continue
# Resize image to a standard size (64x128) to ensure consistent feature vector length
resized_img = cv2.resize(img, (64, 128))
# Convert to grayscale
gray = cv2.cvtColor(resized_img, cv2.COLOR_BGR2GRAY)
# Convert HOG features into a DataFrame and transpose to have one row of features
out = pd.DataFrame(fd).T
print("Descriptor shape ", i, count, " : ", out.shape)
count += 1
if count == 50: # Limit to the first 50 images
break
print(f"{i}: {count}")
251
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
252
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
df = pd.concat([dfp, dfn])
csv_data = df.to_csv('./Datasets/HOG3/HOG_Final.csv')
df
X_train
array([[0.2209127 , 0.13149631, 0.1310974 , ..., 0.26443285, 0.26443285,
0.26443285],
[0.24531284, 0.12817822, 0.13990779, ..., 0.05215404, 0.13892704,
0.07834952],
[0.22650987, 0.15092507, 0.13507013, ..., 0.04238259, 0.04488483,
0.02453033],
...,
[0.2155882 , 0.1684096 , 0.08829851, ..., 0.10465179, 0.03230634,
0.14772919],
[0.23780212, 0.03834988, 0.18296075, ..., 0.14817041, 0.16469105,
0.1732593 ],
[0.23056962, 0.1192733 , 0.19271609, ..., 0.1762829 , 0.16652858,
0.11020062]])
X_test
array([[0.22460075, 0.20807512, 0.12964375, ..., 0.17852044, 0.18853596,
0.07264616],
[0.22063051, 0.12942505, 0.22063051, ..., 0.13071956, 0.17024796,
0.13097218],
[0.20563139, 0.12902862, 0.06734444, ..., 0.21048011, 0.16722286,
0.09023312],
...,
[0.23037201, 0.1716012 , 0.22422652, ..., 0.15069234, 0.18747883,
0.08020848],
[0.21217868, 0.07423544, 0.18174443, ..., 0.18857197, 0.09719393,
0.08561356],
[0.1990332 , 0.19094804, 0.15076064, ..., 0.09255979, 0.06207166,
0.13051169]])
y_train
array([1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0,
0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0,
1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1,
0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1], dtype=int64)
254
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
y_test
[1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0]
#KNN classifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score,
RocCurveDisplay
import joblib
joblib.dump(model_knn,"./Datasets/HOG3/Trained_Models/modelA_knn")
y_pred3 = model_knn.predict(X_test)
print("KNN Classifier")
print("Train Accuracy:",model_knn.score(X_train, y_train))
print("Test Accuracy:",model_knn.score(X_test, y_test))
print("Precision Score: ",metrics.precision_score(y_test, y_pred3, pos_label='positive',
average='micro'))
print("Recall Score: ",metrics.recall_score(y_test, y_pred3, pos_label='positive',
average='micro'))
print("F1 Score: ",metrics.f1_score(y_test, y_pred3, pos_label='positive', average='micro'))
print("Confusion Matrix: ")
print(confusion_matrix(y_test, y_pred3,))
<sklearn.metrics._plot.roc_curve.RocCurveDisplay at 0x1911ba16870>
255
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
256
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
count = 0
# Check if the output directory exists; if not, create it
output_dir = './Datasets/HOG4/'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
257
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
cells_per_block=(2, 2),
visualize=True,
block_norm='L2-Hys'
)
# Convert HOG features into a DataFrame and transpose to have one row of features
out = pd.DataFrame(fd).T
print("Descriptor shape ", i, count, " : ", out.shape)
count += 1
if count == 50: # Limit to first 50 images
break
259
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
X_train
array([[0.31940152, 0. , 0.0461378 , ..., 0.24152244, 0. ,
0.13396017],
[0.01210291, 0. , 0. , ..., 0.27773573, 0.06211159,
0.09996591],
[0. , 0. , 0. , ..., 0.03672928, 0.03871606,
0. ],
...,
[0.28572148, 0. , 0.16910751, ..., 0.20302401, 0.00693289,
0. ],
[0.18326481, 0.05943829, 0. , ..., 0.23846514, 0. ,
0.04399283],
[0.27196109, 0. , 0.27196109, ..., 0.33763659, 0. ,
0. ]])
X_test
array([[0.26926665, 0.10292101, 0.12367615, ..., 0.05209534, 0. ,
0. ],
[0.34301887, 0. , 0.01781956, ..., 0.07851 , 0.15598416,
0.02553455],
[0.26669595, 0.05213381, 0.03408786, ..., 0.23440643, 0. ,
0. ],
...,
[0.299901 , 0. , 0.19373461, ..., 0.19731137, 0.2214648 ,
0.25376244],
[0.26524393, 0. , 0.26524393, ..., 0.07753482, 0.11683825,
0.06237846],
[0.14365397, 0.00127804, 0.0762536 , ..., 0. , 0. ,
0. ]])
y_train
array([1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0,
0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0,
1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1,
0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1], dtype=int64)
y_test
[1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0]
#KNN classifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score,
RocCurveDisplay
import joblib
joblib.dump(model_knn,"./Datasets/HOG4/Trained_Models/modelA_knn")
y_pred3 = model_knn.predict(X_test)
262
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
print("KNN Classifier")
print("Train Accuracy:",model_knn.score(X_train, y_train))
print("Test Accuracy:",model_knn.score(X_test, y_test))
print("Precision Score: ",metrics.precision_score(y_test, y_pred3, pos_label='positive',
average='micro'))
print("Recall Score: ",metrics.recall_score(y_test, y_pred3, pos_label='positive',
average='micro'))
print("F1 Score: ",metrics.f1_score(y_test, y_pred3, pos_label='positive', average='micro'))
print("Confusion Matrix: ")
print(confusion_matrix(y_test, y_pred3,))
<sklearn.metrics._plot.roc_curve.RocCurveDisplay at 0x1913feb92e0>
263
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Result
# Define metrics for each approach
metrics = {
'Approach 1': {'Accuracy': 0.6, 'Precision': 0.6, 'Recall': 0.6, 'F1-Score': 0.6},
'Approach 2': {'Accuracy': 0.7, 'Precision': 0.7, 'Recall': 0.7, 'F1-Score': 0.7},
'Approach 3': {'Accuracy': 0.6, 'Precision': 0.6, 'Recall': 0.6, 'F1-Score': 0.6},
'Approach 4': {'Accuracy': 0.5, 'Precision': 0.5, 'Recall': 0.5, 'F1-Score': 0.5}
}
# Convert to DataFrame
df_metrics = pd.DataFrame(metrics).T
print(df_metrics)
264
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Testing
# Define paths for model and image
img_path = './0/17. '
model_path = './Datasets/HOG2/Trained_Models/modelA_knn'
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.tight_layout()
plt.show()
Not Human
266
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
267
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
else:
raise FileNotFoundError(f"Image file not found at {img_path}. Please check the path.")
plt.tight_layout()
plt.show()
Not Human
268
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
269
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
else:
raise FileNotFoundError(f"Image file not found at {img_path}. Please check the path.")
plt.tight_layout()
plt.show()
Not Human
270
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
271
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
else:
raise FileNotFoundError(f"Image file not found at {img_path}. Please check the path.")
plt.tight_layout()
plt.show()
Human
Conclusion:
272
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
We explored various methods for human detection using Histogram of Oriented Gradients
(HOG) features combined with K-Nearest Neighbors (KNN) classification. Each approach
provided unique insights into how different feature extraction and preprocessing techniques
affect classification performance. In the first approach, we used HOG features with smaller 8x8
cells, resulting in a detailed representation of the image's gradient orientations. This fine-
grained method captures intricate edge information, potentially enhancing the classifier's ability
to differentiate between human figures and other objects. However, the increased feature vector
dimensionality can lead to higher computational costs and necessitates careful parameter tuning
to prevent overfitting. The second approach involved using larger 32x32 cells for HOG feature
extraction. This coarser representation reduces the dimensionality of the feature vector, thereby
lowering computational load and potentially speeding up processing. However, this may result
in the loss of finer details critical for accurate human detection. Therefore, finding the right
balance between cell size and the need for detailed information is key to optimizing HOG
performance. In the third approach, we applied Gaussian blurring followed by Prewitt edge
detection before extracting HOG features. Blurring reduces noise and smooths variations in the
image, which can enhance edge detection accuracy. Prewitt edge detection emphasizes
gradients in both the horizontal and vertical directions, complementing the HOG features by
improving edge clarity. This combined method could potentially offer greater robustness against
noise and variations in the images, thereby improving human detection reliability. The fourth
approach involved using Difference of Gaussians (DoG) for blob detection prior to HOG
feature extraction. Blob detection helps identify regions of interest, focusing the feature
extraction on areas more likely to contain relevant information, such as human figures. This
method may enhance classification accuracy by providing more relevant features for KNN.
Based on the accuracies, the different images were also analyzed to verify human detection.
Also, out of the 4 models implemented, the HOG2 meaning the one which uses the 32x32 cell
size showed the highest accuracy.
273
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Div: A Batch: 1
Date of performance: 10-09-2024
Experiment No. 11
Problem Statement: To classify images of different types of terrain (dirt, paved, and stone-
paved) by extracting texture features using the Gray-Level Co-occurrence Matrix (GLCM)
and Haralick features, and training a classifier based on these features.
Objective(s) of Experiment: To perform and study texture classification using GLCM and
Haralick features.
Introduction:
In computer vision, texture analysis is crucial in identifying and classifying objects based on
their surface characteristics. Methods like the Gray-Level Co-occurrence Matrix (GLCM)
are widely used for detecting and describing distinctive texture features in images. GLCM
provides a statistical method for examining the spatial relationship between pixels, making it
an excellent choice for texture-based classification.
GLCM is used to extract texture features from each image. It computes how often pairs of
pixels with specific values and spatial relationships occur in a given direction and distance
in an image. From the GLCM, texture descriptors such as energy, contrast, correlation,
dissimilarity, and homogeneity are calculated. These descriptors capture the local structure
and pattern of the image and are invariant to changes in lighting and scaling.
Flowchart
275
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
import numpy as np
import pandas as pd
import os
import csv
import cv2
from matplotlib import pyplot as plt
import joblib
from sklearn import preprocessing
from skimage.filters import sobel
from skimage.measure import shannon_entropy
import warnings
warnings.filterwarnings('ignore')
from sklearn.cluster import KMeans
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.tree import DecisionTreeClassifier
import sklearn.metrics as metrics
from sklearn.metrics import accuracy_score
################################################################
#START ADDING DATA TO THE DATAFRAME
#Full image
#GLCM = graycomatrix(img, [1], [0, np.pi/4, np.pi/2, 3*np.pi/4])
GLCM = graycomatrix(gray, [1], [0])
GLCM_Energy = graycoprops(GLCM, 'energy')[0]
df['Energy'] = GLCM_Energy
276
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
df['Corr'] = GLCM_corr
GLCM_diss = graycoprops(GLCM, 'dissimilarity')[0]
df['Diss_sim'] = GLCM_diss
GLCM_hom = graycoprops(GLCM, 'homogeneity')[0]
df['Homogen'] = GLCM_hom
GLCM_contr = graycoprops(GLCM, 'contrast')[0]
df['Contrast'] = GLCM_contr
277
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
df['Homogen4'] = GLCM_hom4
GLCM_contr4 = graycoprops(GLCM4, 'contrast')[0]
df['Contrast4'] = GLCM_contr4
df['Corr8'] = GLCM_corr8
GLCM_diss8 = graycoprops(GLCM8, 'dissimilarity')[0]
df['Diss_sim8'] = GLCM_diss8
GLCM_hom8 = graycoprops(GLCM8, 'homogeneity')[0]
df['Homogen8'] = GLCM_hom8
278
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
return image_dataset
count = 1
for i in temp:
out = feature_extractor(i,count)
count+=1
#append to the csv file
csv_data=out.to_csv('./Data/GLCM/GLCM_' + '.csv', mode='a', index=False,
header=(count==2))
data = pd.read_csv('./Data/GLCM/GLCM_.csv')
X = data.drop(columns=['cls']) # Features (drop the 'cls' column)
y = data['cls'] # Labels (classes)
# Split the data into 80% training and 20% testing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=38)
Results
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score,
RocCurveDisplay
import seaborn as sns
model_knn = KNeighborsClassifier(n_neighbors=3)
# Calculate precision, recall, and F1 scores (using 'micro' average for multi-class)
print("Precision Score: ", precision_score(y_test, y_pred, average='micro'))
print("Recall Score: ", recall_score(y_test, y_pred, average='micro'))
279
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
df['Contrast2'] = GLCM_contr2
df['Diss_sim6'] = GLCM_diss6
GLCM_hom6 = graycoprops(GLCM6, 'homogeneity')[0]
df['Homogen6'] = GLCM_hom6
GLCM_contr6 = graycoprops(GLCM6, 'contrast')[0]
df['Contrast6'] = GLCM_contr6
281
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
df['Homogen7'] = GLCM_hom7
GLCM_contr7 = graycoprops(GLCM7, 'contrast')[0]
df['Contrast7'] = GLCM_contr7
return df.values.flatten()
Testing
random_image_path = 'C:/Users/Ashish/Desktop/CVFolder/Paved/img1.jpg' # Change to the
actual image path
# 1: Paved 2: Asphalt 3: Dirt
# Extract features from the random image
image_features = extract_features_from_image(random_image_path)
# Scale the image features using the same scaler used for training
# Scale the image features using the same scaler used for training
283
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
png
random_image_path = 'C:/Users/Ashish/Desktop/CVFolder/Asphalt/im9.jpg' # Change to the
actual image path
# 1: Paved 2: Asphalt 3: Dirt
# Scale the image features using the same scaler used for training
284
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
# Scale the image features using the same scaler used for training
285
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
png
# Scale the image features using the same scaler used for training
286
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Conclusion:
The experiment explained and gave insights on how GLCM technique (Gray level
Co-occurrence matrix) along with the Haralicks features can help in texture detection.
Different parameters like Correlation, entropy, energy etc were analyzed. This experiment also
utilized the concepts of KNN classifier for predicting the correct road type (asphalt,
paved or dirt). The model was trained on 80% of the images and then random
images were used to check if the model was able to predict the correct road type.
287
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Experiment No. 12
Problem Statement:
Face Expression Recognition using Gabor Filter and Local Binary Pattern (LBP).
AIM:
Write a Python Code for identifying the following face expressions
1. Angry
2. Fear
3. Happy
4. Sad
Objective of Experiment:
To apply Gabor Filter and LBP to identify different facial expressions.
Introduction:
A Gabor filter is a linear filter used in image processing, especially for texture analysis and
feature extraction. It is named after the scientist Dennis Gabor. A Gabor filter captures both
spatial frequency (how quickly the intensity values change in an image) and orientation (the
direction of edges) and is sensitive to specific frequencies and orientations in an image. By
applying Gabor filters at different scales and orientations, it is possible to capture patterns at
various levels of detail in different directions.
Local Binary Pattern (LBP) is a simple and effective texture descriptor used for image analysis.
It works by converting an image into a binary pattern that represents local texture information
by comparing the intensity of a central pixel with its surrounding neighbors.
LBP is rotation-invariant and computationally efficient.
288
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
Flowchart:
a = cv2.cvtColor(a, cv2.COLOR_BGR2GRAY)
ksize = 3 #Use size that makes sense to the image and fetaure size. Large may not be
good.
#On the synthetic image it is clear how ksize affects imgae (try 5 and 50)
sigma = 2 #Large sigma on small features will fully miss the features.
theta = 2*np.pi/4 #/4 shows horizontal 3/4 shows other horizontal. Try other
contributions
lamda = 1*np.pi /4 #1/4 works best for angled.
gamma=0.4 #Value of 1 defines spherical. Calue close to 0 has high aspect ratio
#Value of 1, spherical may not be ideal as it picks up features from other regions.
phi = 0 #Phase offset. I leave it to 0.
ktype=cv2.CV_32F)
fimg = cv2.filter2D(a, cv2.CV_8UC3, kernel)
name = 'C:/Users/Ashish/Desktop/CVFolder/images/gabor_v/' + x
+'/'+str(currentframe) + '.png'
print ('Creating...' + name)
cv2.imwrite(name, fimg)
currentframe=currentframe+1
i=i+1
if i==1000:
break
gabor_vertical(folder_name)
C:/Users/Ashish/Desktop/CVFolder/images/train/happy/11869.jpg
v/happy/404.png
1187.jpg
C:/Users/Ashish/Desktop/CVFolder/images/train/happy/1187.jpg
v/happy/405.png
11870.jpg
C:/Users/Ashish/Desktop/CVFolder/images/train/happy/11870.jpg
v/happy/406.png
11871.jpg
C:/Users/Ashish/Desktop/CVFolder/images/train/happy/11871.jpg
v/happy/407.png
a = cv2.cvtColor(a, cv2.COLOR_BGR2GRAY)
radius = 3
# Number of points to be considered as neighbourers
no_points = 8 * radius
291
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
# Take only the first 18 bins (adjust this as per your needs)
j = hist[:18].tolist() # This will ensure you get only the first 18 bins
j.append(label)
# taking only first 18 bins
with open(r'C:/Users/Ashish/Desktop/CVFolder/images/gabor_v.csv', 'a',newline='') as
f:
writer = csv.writer(f)
writer.writerow(j)
i=i+1
if i==1000:
break
label = label + 1
gabor_path_v = 'C:/Users/Ashish/Desktop/CVFolder/images/gabor_v/'
lbp_v(gabor_path_v)
data_v = pd.read_csv('C:/Users/Ashish/Desktop/CVFolder/images/gabor_v.csv' , dtype =
'float64')
a = cv2.cvtColor(a, cv2.COLOR_BGR2GRAY)
radius = 3
# Number of points to be considered as neighbourers
no_points = 8 * radius
# Take only the first 18 bins (adjust this as per your needs)
292
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
j = hist[:18].tolist() # This will ensure you get only the first 18 bins
j.append(label)
# taking only first 18 bins
with open(r'C:/Users/Ashish/Desktop/CVFolder/images/gabor_h.csv', 'a',newline='') as
f:
writer = csv.writer(f)
writer.writerow(j)
i=i+1
if i==1000:
break
label = label + 1
gabor_path_h = 'C:/Users/Ashish/Desktop/CVFolder/images/gabor_h/'
lbp_h(gabor_path_h)
combined_csv_path = 'C:/Users/Ashish/Desktop/CVFolder/images/combined_gabor.csv'
first_file = True
293
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
7999 4
Name: Class, Length: 8000, dtype: int64
Y_1
0 0
1 0
2 0
3 0
4 0
..
7995 3
7996 3
7997 3
7998 3
7999 3
Name: Class, Length: 8000, dtype: int64
X_train,X_test,y_train,y_test = train_test_split(X , Y , train_size = 0.8 , random_state = 0)
X_train
y_train
1001 2
7360 4
5234 2
7390 4
6841 3
..
4931 1
3264 4
1653 2
2607 3
2732 3
Name: Class, Length: 6400, dtype: int64
X_test
y_test
3069 4
1675 2
6385 3
543 1
3213 4
..
7716 4
4766 1
4096 1
1595 2
5023 2
Name: Class, Length: 1600, dtype: int64
294
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
KNN Classifier
#KNN classifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score,
RocCurveDisplay
model_knn = KNeighborsClassifier(n_neighbors = 3)
model_knn.fit(X_train, y_train)
joblib.dump(model_knn,"C:/Users/Ashish/Desktop/CVFolder/images/Trained_Models/
modelGabor_LBP_knn")
y_pred3 = model_knn.predict(X_test)
print("KNN Classifier")
print("Train Accuracy:",model_knn.score(X_train, y_train))
print("Test Accuracy:",model_knn.score(X_test, y_test))
print("Precision Score: ",metrics.precision_score(y_test, y_pred3, pos_label='positive',
average='micro'))
print("Recall Score: ",metrics.recall_score(y_test, y_pred3, pos_label='positive',
average='micro'))
print("F1 Score: ",metrics.f1_score(y_test, y_pred3, pos_label='positive', average='micro'))
KNN Classifier
Train Accuracy: 0.60953125
Test Accuracy: 0.3125
Precision Score: 0.3125
Recall Score: 0.3125
F1 Score: 0.3125
# Perform a grid search over the parameter grid using 5-fold cross-validation
grid_search_knn = GridSearchCV(knn, param_grid, cv=5)
joblib.dump(grid_search_knn,"C:/Users/Ashish/Desktop/CVFolder/images/Trained_Models/
modelA_Gabor_LBP_tunned")
y_pred3 = grid_search_knn.predict(X_test)
print("KNN")
print("Train Accuracy:",grid_search_knn.score(X_train, y_train))
print("Test Accuracy:",grid_search_knn.score(X_test, y_test))
print("Precision Score: ",metrics.precision_score(y_test, y_pred3, pos_label='positive',
average='micro'))
print("Recall Score: ",metrics.recall_score(y_test, y_pred3, pos_label='positive',
average='micro'))
print("F1 Score: ",metrics.f1_score(y_test, y_pred3, pos_label='positive', average='micro'))
Best hyperparameters: {'metric': 'euclidean', 'n_neighbors': 17, 'weights': 'distance'}
Best score: 0.32171875
KNN
Train Accuracy: 0.99859375
Test Accuracy: 0.31875
Precision Score: 0.31875
Recall Score: 0.31875
F1 Score: 0.31875
XGBoost Classifier
X_train,X_test,y_train,y_test = train_test_split(X , Y_1 , train_size = 0.8 , random_state = 0)
from sklearn.metrics import accuracy_score, classification_report
model_XGBoost = XGBClassifier()
model_XGBoost.fit(X_train , y_train)
joblib.dump(model_XGBoost,'C:/Users/Ashish/Desktop/CVFolder/images/Trained_Models/
modelGabor_LBP_XGBoost')
predictions = model_XGBoost.predict(X_test)
296
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
SVM classifier
svm_clf = SVC(kernel = 'linear')
svm_clf.fit(X_train, y_train)
joblib.dump(svm_clf,'C:/Users/Ashish/Desktop/CVFolder/images/Trained_Models/
modelGabor_LBP_SVM')
svm_predictions = svm_clf.predict(X_test)
Comparing all 3 classifiers, it is clear that hyper-tunned KNN performs best. then XGBoost
classifier and then SVM classifier
Testing
def gabor_v_test(img_path):
path= img_path
print(path)
a=cv2.imread(path)
a = cv2.cvtColor(a, cv2.COLOR_BGR2GRAY)
ksize = 3 #Use size that makes sense to the image and fetaure size. Large may not be good.
#On the synthetic image it is clear how ksize affects imgae (try 5 and 50)
sigma = 2 #Large sigma on small features will fully miss the features.
theta = 2*np.pi/4 #/4 shows horizontal 3/4 shows other horizontal. Try other contributions
lamda = 1*np.pi /4 #1/4 works best for angled.
gamma=0.4 #Value of 1 defines spherical. Calue close to 0 has high aspect ratio
#Value of 1, spherical may not be ideal as it picks up features from other regions.
phi = 0 #Phase offset. I leave it to 0.
name = 'C:/Users/Ashish/Desktop/CVFolder/images/Gabor_LBP_test/v/0.png'
print ('Creating...' + name)
cv2.imwrite(name, fimg)
297
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
def gabor_h_test(img_path):
path= img_path
print(path)
a=cv2.imread(path)
a = cv2.cvtColor(a, cv2.COLOR_BGR2GRAY)
ksize = 3 #Use size that makes sense to the image and fetaure size. Large may not be good.
#On the synthetic image it is clear how ksize affects imgae (try 5 and 50)
sigma = 2 #Large sigma on small features will fully miss the features.
theta = 8*np.pi/4 #/4 shows horizontal 3/4 shows other horizontal. Try other contributions
lamda = 1*np.pi /4 #1/4 works best for angled.
gamma=0.4 #Value of 1 defines spherical. Calue close to 0 has high aspect ratio
#Value of 1, spherical may not be ideal as it picks up features from other regions.
phi = 0 #Phase offset. I leave it to 0.
name = 'C:/Users/Ashish/Desktop/CVFolder/images/Gabor_LBP_test/h/0.png'
print ('Creating...' + name)
cv2.imwrite(name, fimg)
def lbp_v_test(gabor_file_path):
path= gabor_file_path
print(path)
a=cv2.imread(path)
a = cv2.cvtColor(a, cv2.COLOR_BGR2GRAY)
radius = 3
# Number of points to be considered as neighbourers
no_points = 8 * radius
# Take only the first 18 bins (adjust this as per your needs)
j = hist[:18].tolist() # This will ensure you get only the first 18 bins
# taking only first 18 bins
with open(r'C:/Users/Ashish/Desktop/CVFolder/images/Gabor_LBP_test/gabor_v_test.csv',
'a',newline='') as f:
writer = csv.writer(f)
writer.writerow(j)
298
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
def lbp_h_test(gabor_file_path):
path= gabor_file_path
print(path)
a=cv2.imread(path)
a = cv2.cvtColor(a, cv2.COLOR_BGR2GRAY)
radius = 3
# Number of points to be considered as neighbourers
no_points = 8 * radius
# Take only the first 18 bins (adjust this as per your needs)
j = hist[:18].tolist() # This will ensure you get only the first 18 bins
# taking only first 18 bins
with open(r'C:/Users/Ashish/Desktop/CVFolder/images/Gabor_LBP_test/gabor_v_test.csv',
'a',newline='') as f:
writer = csv.writer(f)
writer.writerow(j)
def test_csv_combine(file1,file2):
combined_csv_path = './combined_gabor_test.csv'
first_file = True
299
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
img = cv2.imread(img_path)
img = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)
data_test = pd.read_csv('C:/Users/Ashish/Desktop/CVFolder/images/Gabor_LBP_test/
gabor_v_test.csv')
data_test
model_KNN_tunned =
joblib.load('C:/Users/Ashish/Desktop/CVFolder/images/Trained_Models/
modelA_Gabor_LBP_tunned')
predictions = model_KNN_tunned.predict(data_test)
class_mapping = {0: "angry", 1: "fear", 2: "happy", 3: "sad"}
predicted_class_name = class_mapping.get(predictions[0], "Unknown")
print(f"The predicted class for the new image is: {predicted_class_name}")
plt.imshow(img , 'gray')
plt.show()
The predicted class for the new image is: fear
png
data_test = pd.read_csv('C:/Users/Ashish/Desktop/CVFolder/images/Gabor_LBP_test/
gabor_v_test.csv')
data_test
model_KNN_tunned =
joblib.load('C:/Users/Ashish/Desktop/CVFolder/images/Trained_Models/
modelA_Gabor_LBP_tunned')
predictions = model_KNN_tunned.predict(data_test)
class_mapping = {0: "angry", 1: "fear", 2: "happy", 3: "sad"}
predicted_class_name = class_mapping.get(predictions[0], "Unknown")
print(f"The predicted class for the new image is: {predicted_class_name}")
plt.imshow(img , 'gray')
plt.show()
The predicted class for the new image is: fear
Conclusion:
In this experiment, I explored two new techniques: the Gabor filter and the Local Binary Pattern
(LBP) feature descriptor. The Gabor filter is useful for extracting spatial frequency and
orientation information from images, while LBP is a texture descriptor known for its ability to
be rotation-invariant. However, I found that the classifiers using these techniques didn’t perform
well, with test accuracies around 30% across different classifiers. This could be due to the poor
quality of the dataset, where the texture variations might not be distinct enough to differentiate
between different emotions. I believe that increasing the image resolution could potentially
improve the results by providing more pixels for the classifiers to better capture subtle texture
differences.
301
Bansilal Ramnath Agarwal Charitable Trust’s
Vishwakarma Institute of Technology, Pune-37
(An Autonomous Institute Affiliated to Savitribai Pune University)
302