fanfuhan OpenCV 教學093 ~ opencv-093-對象檢測(LBP特徵介紹)
fanfuhan OpenCV 教學093 ~ opencv-093-對象檢測(LBP特徵介紹)
資料來源: https://fanfuhan.github.io/
https://fanfuhan.github.io/2019/05/09/opencv-093/
GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV
局部二值模式(Local Binary Pattern)主要實現2D圖像紋理分析。其基本思想是用每個假設跟隨它周圍的相對相比較得到局部圖像結構,假設中心值值大於相互重疊值則則相鄰域點賦值變量1,否則賦值變量0,最終對每個事件點都會得到一個二進制八位的表示,大約11100111。假設3×3的窗口大小,這樣對每個點指向組合得到的值為值的空間為[0〜2 ^ 8]。這種結果稱為圖像的局部二值模式或者簡寫為了LBP。
C++
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; CascadeClassifier faceDetector; String haar_data_file = "D:/opencv-4.0.0/opencv/build/etc/lbpcascades/lbpcascade_frontalface_improved.xml"; int main(int artc, char** argv) { Mat frame, gray; vector<Rect> faces; VideoCapture capture(0); faceDetector.load(haar_data_file); namedWindow("frame", WINDOW_AUTOSIZE); while (true) { capture.read(frame); cvtColor(frame, gray, COLOR_BGR2GRAY); equalizeHist(gray, gray); faceDetector.detectMultiScale(gray, faces, 1.2, 1, 0, Size(30, 30), Size(400, 400)); for (size_t t = 0; t < faces.size(); t++) { rectangle(frame, faces[t], Scalar(0, 0, 255), 2, 8, 0); } char c = waitKey(10); if (c == 27) { break; } imshow("frame", frame); } waitKey(0); return 0; }
Python
""" 对象检测(LBP特征介绍) """ import cv2 as cv capture = cv.VideoCapture(0) detector = cv.CascadeClassifier("D:/software/opencv4/build/etc/lbpcascades/lbpcascade_frontalface_improved.xml") while True: ret, image = capture.read() if not ret: break faces = detector.detectMultiScale(image, scaleFactor=1.05, minNeighbors=1, minSize=(30, 30), maxSize=(300, 300)) for x, y, width, height in faces: cv.rectangle(image, (x, y), (x + width, y + height), (0, 0, 255), 2, cv.LINE_8, 0) cv.imshow("faces", image) c = cv.waitKey(50) if c == 27: break cv.destroyAllWindows()