0% found this document useful (0 votes)
15 views2 pages

CVPR Exp7

Uploaded by

Rahul Dash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

CVPR Exp7

Uploaded by

Rahul Dash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

4/6/25, 9:45 PM CVPR_EXP7.

ipynb - Colab

keyboard_arrow_down Experiment 7: HOG Feature Descriptors


import numpy as np
import cv2
import matplotlib.pyplot as plt
from google.colab.patches import cv2_imshow
import imutils
from skimage.feature import hog
from skimage import exposure

keyboard_arrow_down Computing HOG feature descriptors


!pip install opencv-contrib-python

Requirement already satisfied: opencv-contrib-python in /usr/local/lib/python3.11/dist-packages (4.11.0.86)


Requirement already satisfied: numpy>=1.21.2 in /usr/local/lib/python3.11/dist-packages (from opencv-contrib-python) (2.0.2)

# Load the image in grayscale


image = cv2.imread('/content/dan-kb-gFGL-k454ds-unsplash.jpg', cv2.IMREAD_GRAYSCALE)
image = cv2.resize(image, (256, 256))

# Compute HOG features with visualization


hog_features, hog_image = hog(image, pixels_per_cell=(8, 8),
cells_per_block=(2, 2),
visualize=True, block_norm='L2-Hys')

# Apply histogram equalization to enhance visibility


hog_image = exposure.rescale_intensity(hog_image, in_range=(0, 10))

# Display the original image and HOG visualization


plt.figure(figsize=(10, 5))

# Original Image
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title("Original Image")
plt.axis("off")

# HOG Image (Enhanced Contrast)


plt.subplot(1, 2, 2)
plt.imshow(hog_image, cmap='gray')
plt.title("HOG Features (Enhanced)")
plt.axis("off")

plt.show()

print("HOG Feature Descriptor Shape:", hog_features.shape)

HOG Feature Descriptor Shape: (34596 )


 

https://colab.research.google.com/drive/1MElBI1t4jMv2BsSAOMFH_l7a1PE-DGkA#scrollTo=nBbrzezeQP-D&printMode=true 1/2
4/6/25, 9:45 PM CVPR_EXP7.ipynb - Colab

keyboard_arrow_down Detecting Pedestrians Using HOG


# Load the image
image = cv2.imread('people.jpg')
image = cv2.resize(image, (0,0), fx=0.3, fy=0.3)

# Convert to grayscale (not necessary for HOG but can improve performance)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Initialize the HOG descriptor with a pre-trained SVM for pedestrian detection
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

# Detect pedestrians
rects, weights = hog.detectMultiScale(gray, winStride=(4, 4), padding=(8, 8), scale=1.05)

# Draw bounding boxes around detected pedestrians


for (x, y, w, h) in rects:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

# Show the result


cv2_imshow(image)

https://colab.research.google.com/drive/1MElBI1t4jMv2BsSAOMFH_l7a1PE-DGkA#scrollTo=nBbrzezeQP-D&printMode=true 2/2

You might also like