参考链接:
Datawhale 计算机视觉基础-图像处理(下)-Task04 HOG特征描述算子-行人检测
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
if __name__ == '__main__':
src = cv.imread("img/winner.jpg")
hog = cv.HOGDescriptor()
cv.imshow("input", src)
hog.setSVMDetector(cv.HOGDescriptor_getDefaultPeopleDetector())
# Detect people in the image
(rects, weights) = hog.detectMultiScale(src,
winStride=(2, 4),
padding=(8, 8),
scale=1.2,
useMeanshiftGrouping=False)
for (x, y, w, h) in rects:
cv.rectangle(src, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv.imshow("hog-detector", src)
cv.imwrite("hog-detector.jpg", src)
cv.waitKey(0)
cv.destroyAllWindows()
原图:
结果图:
可视化hog特征检测:
注意参考链接里feature_hog()的visualse=True参数拼写应该为visualize。
from skimage import feature, exposure
from matplotlib import pyplot as plt
import cv2
image = cv2.imread('img/cartoons1.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
fd, hog_image = feature.hog(image, orientations=9, pixels_per_cell=(8, 8),
cells_per_block=(2, 4), visualize=True)
# Rescale histogram for better display
hog_image_rescaled = exposure.rescale_intensity(hog_image, in_range=(0, 10))
cv2.namedWindow("img",cv2.WINDOW_NORMAL)
cv2.imshow('img', image)
cv2.namedWindow("hog",cv2.WINDOW_NORMAL)
cv2.imshow('hog', hog_image_rescaled)
cv2.waitKey(0)==ord('q')