Python 使用Opencv實現目標檢測與識別

----------歡迎加入學習交流QQ羣:657341423


在上章節講述到圖像特徵檢測與匹配 ,本章節是講述目標檢測與識別。後者是在前者的基礎上進一步完善。
在本章中,我們使用HOG算法,HOG和SIFT、SURF同屬一種類型的描述符。功能代碼如下:

import cv2
def is_inside(o, i):
    ox, oy, ow, oh = o
    ix, iy, iw, ih = i
    # 如果符合條件,返回True,否則返回False
    return ox > ix and oy > iy and ox + ow < ix + iw and oy + oh < iy + ih

# 根據座標畫出人物所在的位置
def draw_person(img, person):
  x, y, w, h = person
  cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 255), 2)

# 定義HOG特徵+SVM分類器
img = cv2.imread("people.jpg")
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
found, w = hog.detectMultiScale(img, winStride=(8, 8), scale=1.05)

# 判斷座標位置是否有重疊
found_filtered = []
for ri, r in enumerate(found):
    for qi, q in enumerate(found):
        a = is_inside(r, q)
        if ri != qi and a:
            break
    else:
        found_filtered.append(r)
# 勾畫篩選後的座標位置
for person in found_filtered:
    draw_person(img, person)
# 顯示圖像
cv2.imshow("people detection", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

運行結果如圖所示:
這裏寫圖片描述
這個例子是使用HOG特徵進行SVM算法訓練,這部分已開始涉及到機器學習的方面,通過SVM算法訓練數據集,然後根據某圖像與數據集進行匹配。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章