python opencv —— 背景提取(MOG、KNN)、識別與檢測(Haar Cascade)

來源:互聯網
上載者:User
注意 opencv 的座標軸,x 軸向右,和 width 對應,y 軸向下,和 height 對應; 1. MOG2 與 KNN

MOG:Mixture of Gaussian

import cv2cap = cv2.VideoCapture('./data/video/768x576.avi')knn_sub = cv2.createBackgroundSubtractorKNN()mog2_sub = cv2.createBackgroundSubtractorMOG2()while True:    ret, frame = cap.read()    if not ret:        break    mog_sub_mask = mog2_sub.apply(frame)    knn_sub_mask = knn_sub.apply(frame)    cv2.imshow('original', frame)    cv2.imshow('MOG2', mog_sub_mask)    cv2.imshow('KNN', knn_sub_mask)    key = cv2.waitKey(30) & 0xff    if key == 27 or key == ord('q'):        breakcap.release()cv2.destroyAllWindows()
2. 識別與檢測

Haar Cascade

import cv2import numpy as np# haar 級聯分類器,opencv 源檔案中data檔案夾下會有# https://github.com/opencv/opencv/tree/master/data/haarcascadesface_cascade = cv2.CascadeClassifier(    './data/classifiers/haarcascade_frontalface_default.xml')eye_cascade = cv2.CascadeClassifier('./data/classifiers/haarcascade_eye.xml')cap = cv2.VideoCapture(0)while True:    ret, img = cap.read()    if not ret:        break    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)    faces = face_cascade.detectMultiScale(gray, 1.3, 5)    for (x, y, w, h) in faces:        cv2.rectangle(img, (x, y), (x+w, y+h), color=(255, 0, 0), thickness=2)        roi_gray = gray[y:y+h, x:x+w]        roi_color = img[y:y+h, x:x+w]        eyes = eye_cascade.detectMultiScale(roi_gray)        try:            ex, ey, ew, eh = eyes[0]            cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh),                 color=(0, 255, 0), thickness=2)            ex1, ey1, ew1, eh1 = eyes[1]            cv2.rectangle(roi_color, (ex1, ey1), (ex1+ew1, ey1+eh1),                 color=(0, 255, 0), thickness=2)        except IndexError:            print('')    cv2.imshow('me', img)    key = cv2.waitKey(30)    if key == 27 or key == ord('q'):        breakcap.release()cv2.destroyAllWindows()
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.