Python 使用Opencv實現目標檢測與識別的示例程式碼
阿新 • • 發佈:2020-09-10
在上章節講述到影象特徵檢測與匹配 ,本章節是講述目標檢測與識別。後者是在前者的基礎上進一步完善。
在本章中,我們使用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演算法訓練資料集,然後根據某影象與資料集進行匹配。
到此這篇關於Python 使用Opencv實現目標檢測與識別的示例程式碼的文章就介紹到這了,更多相關Opencv 目標檢測與識別內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!