fanfuhan OpenCV 教學100 ~ opencv-100-HOG特徵與行人 檢測/定位/標記
fanfuhan OpenCV 教學100 ~ opencv-100-HOG特徵與行人 檢測/定位/標記
資料來源: https://fanfuhan.github.io/
https://fanfuhan.github.io/2019/05/16/opencv-100/
GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV
HOG(定向直方圖)特徵在對象識別與模式匹配中是一種常見的特徵提取算法,是基於本地目標塊進行特徵直方圖提取的一種算法,對象局部的變形與光照影響有很好的穩定性,最初是用HOG特徵來來識別人像,通過HOG特徵提取+ SVM訓練,可以得到很好的效果,OpenCV已經有了。
C++
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main(int argc, char** argv) { Mat src = imread("D:/images/pedestrian.png"); if (src.empty()) { printf("could not load image..\n"); return -1; } imshow("input", src); HOGDescriptor *hog = new HOGDescriptor(); hog->setSVMDetector(hog->getDefaultPeopleDetector()); vector<Rect> objects; hog->detectMultiScale(src, objects, 0.0, Size(4, 4), Size(8, 8), 1.25); for (int i = 0; i < objects.size(); i++) { rectangle(src, objects[i], Scalar(0, 0, 255), 2, 8, 0); } imshow("result", src); waitKey(0); return 0; }
Python
""" HOG特征与行人检测 """ import cv2 as cv src = cv.imread("images/pedestrian.png") hog = cv.HOGDescriptor() hog.setSVMDetector(cv.HOGDescriptor_getDefaultPeopleDetector()) # detect people in image (rects, weights) = hog.detectMultiScale(src, winStride=(4, 4), padding=(8, 8), scale=1.25, 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.waitKey(0) cv.destroyAllWindows()