0% found this document useful (0 votes)
11 views3 pages

SURF&HOG

Uploaded by

archit.muj
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)
11 views3 pages

SURF&HOG

Uploaded by

archit.muj
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/ 3

import cv2

import numpy as np
from skimage.feature import hog
from skimage import exposure
import matplotlib.pyplot as plt

image_path = 'gtx.jpg'
image = cv2.imread(image_path)

if image is None:
raise FileNotFoundError(f"Image not found at path: {image_path}")

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

hog_features, hog_image = hog(


gray_image,
orientations=9,
pixels_per_cell=(8, 8),
cells_per_block=(2, 2),
visualize=True,
)

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

plt.figure(figsize=(12, 6))

plt.subplot(1, 2, 1)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title("Original Image")
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(hog_image_rescaled, cmap='gray')
plt.title("HOG Visualization")
plt.axis('off')

plt.tight_layout()
plt.show()

print(f"HOG Features Shape: {hog_features.shape}")


HOG Features Shape: (359856,)

!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)

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('gtx.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Check if xfeatures2d is available


if hasattr(cv2, 'xfeatures2d'):
# Create SURF detector object
surf = cv2.xfeatures2d.SURF_create(hessianThreshold=400)

# Detect keypoints and compute descriptors


keypoints, descriptors = surf.detectAndCompute(gray, None)

# Draw keypoints
img_with_kp = cv2.drawKeypoints(img, keypoints, None, (255, 0, 0),
4)

# Show result
plt.figure(figsize=(10, 8))
plt.imshow(cv2.cvtColor(img_with_kp, cv2.COLOR_BGR2RGB))
plt.title('SURF Keypoints')
plt.axis('off')
plt.show()
else:
print("xfeatures2d module is not available. Check if opencv-
contrib-python is properly installed.")

----------------------------------------------------------------------
-----
AttributeError Traceback (most recent call
last)
<ipython-input-8-06816cfe00d0> in <cell line: 0>()
9 if hasattr(cv2, 'xfeatures2d'):
10 # Create SURF detector object
---> 11 surf = cv2.xfeatures2d.SURF_create(hessianThreshold=400)
12
13 # Detect keypoints and compute descriptors

AttributeError: module 'cv2.xfeatures2d' has no attribute


'SURF_create'

You might also like