TM_CCORR_NORMED 👍
res = cv2.matchTemplate(src, templ, cv2.TM_CCOEFF_NORMED)
# 0 ~ 1 사이의 실수 0 ~ 255사이 값으로 스케일링
res_norm = cv2.normalize(res, None, 0, 255,
cv2.NORM_MINMAX, cv2.CV_8U)
_, maxv, _, maxloc = cv2.minMaxLoc(res_norm)
(th, tw) = templ.shape[:2]
cv2.rectangle(src, maxloc, (maxloc[0] + tw, maxloc[1] + th),
(0, 0, 255), 2)
얼굴이 아닌 영역 빠르게 걸러 내는 방식
단계
1단계 : 특징 한 개 사용
2단계 : 특징 다섯 개 사용
→ 하나라도 만족 안 하면 얼굴 아님
3단계 : 특징 20개 사용
classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
if classifier.empty():
print('XML load failed!')
exit()
faces = classifier.detectMultiScale(src)
for (x, y, w, h) in faces:
cv2.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 2)
face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_classifier = cv2.CascadeClassifier('haarcascade_eye.xml')
if face_classifier.empty() or eye_classifier.empty():
print('XML load failed!')
exit()
faces = face_classifier.detectMultiScale(src)
for (x, y, w, h) in faces:
cv2.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 2)
faceROI = src[y : y+h, x : x+w]
eyes = eye_classifier.detectMultiScale(faceROI)
for (x2, y2, w2, h2) in eyes:
center = (int(x2 + w2/2), int(y2 + h2/2))
cv2.circle(faceROI, center, int(w2/2),
(255, 0, 0), 2, cv2.LINE_AA)
다랄(Datal), 트릭스(Triggs)
Histograms of Oriented Gradients 그래디언트 방향 히스토그램
그래디언트 크기, 방향
SVM(Support Vector Machine) 알고리즘 이용
64 x 128 크기의 영상에서 계산
과정
입력 영상으로부터 그래디언트 계산 (크기, 방향 성분)
8x8크기 단위로 셀(cell) 분할
→ 64x128크기 영상 : 가로 8개, 세로 16개 셀 생성
각각의 셀로부터 그래디언트 방향 성분에 대한 히스토그램 구함
→ 방향성분 20도 단위로 구분 : 9개의 빈
인접한 네 개의 셀 합침 : block
→ 9x4=36개의 실수값으로 이루어진 방향 히스토그램
→ 가로 7개, 세로 15개 블록 생성
전체 블록에서 방향 히스토그램 추출
→ 실수값 개수 : (7x15) x 36 = 3780
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
detected,_ = hog.detectMultiScale(src)
for (x, y, w, h) in detected:
cv2.rectangle(src, (x, y), (x+w, y+h), (0,0,255), 3)
detector = cv2.QRCodeDetector()
info, points, _ = detector.detectAndDecode(src)
if points is not None:
points = np.array(points, dtype=np.int32).reshape(4, 2)
cv2.polylines(src, [points], True, (0, 255, 255), 2)
if len(info) > 0:
cv2.putText(src, info, (10, 30), cv2.FONT_HERSHEY_DUPLEX, 1, (0,0,255), lineType=cv2.LINE_AA)