You are on page 1of 6

1/7/2023 ASSIGNMENT NO

3
SUBMITTED TO: DR. SYED ADNAN
SUBMITTED BY: ALEEZA ANJUM
REG NO: 20-CS-101
SECTION: ALPHA

DEPARTMENT OF COMPUTER SCIENCE


UNIVERSITY OF ENGINEERING & TECHNOLOGY TAXILA
Q no 1: you are required to implement HOG descriptor and then take any picture of
any scene with a person and apply your implemented HOG to detect the person in
that scene. 
Discuss in a writ-up the role of gradient directions and magnitude in HOG. 
Ans:

CODE:

import cv2
import numpy as np

kernel = np.ones((3, 3))

# Compute the frame difference


def frame_diff(prev_frame, cur_frame, next_frame):
diff_frames1 = cv2.absdiff(next_frame, cur_frame)
# Absolute difference between current frame and previous frame
diff_frames2 = cv2.absdiff(cur_frame, prev_frame)
# Return the result of bitwise 'AND' between the above two resultant images
return cv2.bitwise_and(diff_frames1, diff_frames2)

# Capture the frame from webcam


def get_frame(cap):
ret, frame = cap.read()
# Resize the image
frame = cv2.resize(frame, None, fx=scaling_factor,
fy=scaling_factor, interpolation=cv2.INTER_AREA)
# Return the grayscale image
return cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)

if __name__ == '__main__':
cap = cv2.VideoCapture(0)
scaling_factor = 0.9
prev_frame = get_frame(cap)
cur_frame = get_frame(cap)
next_frame = get_frame(cap)
# Iterate until the user presses the ESC key
while True:
frame_difference = frame_diff(prev_frame, cur_frame, next_frame)
# frame_difference = cv2.dilate(src, kernel)
_, frame_th = cv2.threshold(frame_difference, 0, 255,
cv2.THRESH_TRIANGLE)
# frame_th = cv2.dilate(frame_th, kernel)
cv2.imshow("Object Movement", frame_difference)

cv2.imshow("Obje", frame_th)

# Update the variables


prev_frame = cur_frame
cur_frame = next_frame
next_frame = get_frame(cap)
# Check if the user pressed ESC
key = cv2.waitKey(5)
if key == 27:
break
cv2.destroyAllWindows()

OUTPUT:
Movement of hand is detected here

Movement of face is detected


Explanation:
Here we are calculating the HOG through live camera detection. Camera captures the
person and HOG code will detect which object is moving- either it’s a hand or a face.
We imported two libraries cv and numpy here.
Then we have used a kernel of 3 by 3.
Steps for HOG Features:
1- Take the input from camera. Resize the frame.
2- The gradient of image is calculated. The gradient is calculated from combining
magnitude and angle from the image. Considering the block of 3*3, first Gx and Gy of
image has been calculated from this formula:

3- Then

magnitude and angle of each pixel is calculated from following formula.


4- After obtaining the gradient of each pixel, the gradient matrices (magnitude and angle
matrix) are divided into 8x8 cells to form a block. For each block, a 9-point histogram is
calculated. A 9-point histogram develops a histogram with 9 bins and each bin has an angle
range of 20 degrees.

Representation of a 9 bin Histogram:


5-

The resultant matrix after the above calculations will have the shape of 16x8x9.
6- Once histogram computation is over for all blocks, 4 blocks from the 9 point histogram
matrix are clubbed together to form a new block (2x2). This clubbing is done in an
overlapping manner with a stride of 8 pixels.

7- Values of fb for each block is normalized by the L2 norm :

8- This normalization is done to reduce the effect of changes in contrast between images of
the same object. From each block. A 36 point feature vector is collected. In the horizontal
direction there are 7 blocks and in the vertical direction there are 15 blocks. So the total
length of HOG features will be:
7 x 15 x 36 = 3780.
HOG features of the selected image are obtained.

You might also like