fanfuhan OpenCV 教學105 ~ opencv-105-使用HOG進行對象檢測
fanfuhan OpenCV 教學105 ~ opencv-105-使用HOG進行對象檢測
資料來源: https://fanfuhan.github.io/
https://fanfuhan.github.io/2019/05/21/opencv-105/
GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV
對於已經訓練好的HOG+SVM的模型,我們可以通過開窗實現對象檢測,從而完成自定義對象檢測。以電錶檢測為例,這樣我們就實現HOG+SVM對象檢測全流程。OpenCV中實現對每個窗口像素塊預測,需要首先加載SVM模型文件,然後使用predict方法實現預測。這種方法的缺點就是開窗檢測是從左到右、從上到下,是一個高耗時的操作,所以步長選擇一般會選擇HOG窗口默認步長的一半,這樣可以減少檢測框的數目,同時在predict時候會發現多個重複框,求取它們的平均值即可得到最終的檢測框。
Python
""" 使用HOG进行对象检测 """ import cv2 as cv import numpy as np image = cv.imread("images/elec_watch/test/scene_01.jpg") test_image = cv.resize(image, (0, 0), fx=0.2, fy=0.2) cv.imshow("input", test_image) gray = cv.cvtColor(test_image, cv.COLOR_BGR2GRAY) print(test_image.shape) h, w = test_image.shape[:2] svm = cv.ml.SVM_load("svm_data.dat") sum_x = 0 sum_y = 0 count = 0 hog = cv.HOGDescriptor() for row in range(64, h-64, 4): for col in range(32, w-32, 4): win_roi = gray[row-64:row+64,col-32:col+32] hog_desc = hog.compute(win_roi, winStride=(8, 8), padding=(0, 0)) one_fv = np.zeros([len(hog_desc)], dtype=np.float32) for i in range(len(hog_desc)): one_fv[i] = hog_desc[i][0] one_fv = np.reshape(one_fv, [-1, len(hog_desc)]) result = svm.predict(one_fv)[1] if result[0][0] > 0: sum_x += (col-32) sum_y += (row-64) count += 1 x = sum_x // count y = sum_y // count cv.rectangle(test_image, (x, y), (x+64, y+128), (0, 0, 255), 2, 8, 0) cv.imshow("result", test_image) cv.waitKey(0) cv.destroyAllWindows()