gpt4 book ai didi

opencv - OpenCV:Lucas Kanade应用于特定区域(以检测面部特征)

转载 作者:行者123 更新时间:2023-12-02 17:48:59 25 4
gpt4 key购买 nike

我正在尝试使用具有Haar级联分类的Lucas Kanade算法执行面部跟踪。 Lucas Kanade是成功的并且可以跟踪用户,但是不幸的是,一些用于检测点的良好功能浪费在了背景的角落。我希望利用Haar Cascade的能力来检测事实,以获取所检测面部的坐标,并将Lucas Kanade仅应用于该受限区域内。

基本上,我想使用Haar Cascade来检测事实,获取x,y,w和h值,并使用这些坐标将Lucas Kanade应用到该受限区域内(这样就不会浪费时间为背景分配良好的功能,而仅检测到面部特征)

执行Lucas Kanade算法的代码行是以下代码:

p0 = cv2.goodFeaturesToTrack(old_gray, mask = None, **feature_params)

我怎么做?

码:
from matplotlib import pyplot as plt
import numpy as np

import cv2

rectangle_x = 0

face_classifier = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')

cap = cv2.VideoCapture(0)


# params for ShiTomasi corner detection
feature_params = dict( maxCorners = 200,
qualityLevel = 0.01,
minDistance = 10,
blockSize = 7 )

# Parameters for lucas kanade optical flow
lk_params = dict( winSize = (15,15),
maxLevel = 2,
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))

# Create some random colors
color = np.random.randint(0,255,(100,3))

# Take first frame and find corners in it
ret, old_frame = cap.read()

cv2.imshow('Old_Frame', old_frame)
cv2.waitKey(0)
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
restart = True
face = face_classifier.detectMultiScale(old_gray, 1.2, 4)

if len(face) == 0:
print "This is empty"

for (x,y,w,h) in face:
focused_face = old_frame[y: y+h, x: x+w]



cv2.imshow('Old_Frame', old_frame)

face_gray = cv2.cvtColor(old_frame,cv2.COLOR_BGR2GRAY)

gray = cv2.cvtColor(focused_face,cv2.COLOR_BGR2GRAY)

corners_t = cv2.goodFeaturesToTrack(gray, mask = None, **feature_params)
corners = np.int0(corners_t)




for i in corners:
ix,iy = i.ravel()
cv2.circle(focused_face,(ix,iy),3,255,-1)
cv2.circle(old_frame,(x+ix,y+iy),3,255,-1)

print ix, " ", iy

plt.imshow(old_frame),plt.show()


##########

#############################
p0 = cv2.goodFeaturesToTrack(old_gray, mask = None, **feature_params)
#############################
# Create a mask image for drawing purposes
mask = np.zeros_like(old_frame)

print "X: ", x
print "Y: ", y

while(1):
ret,frame = cap.read()
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# calculate optical flow
p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)

# Select good points
good_new = p1[st==1]
good_old = p0[st==1]

# draw the circles
for i,(new,old) in enumerate(zip(good_new,good_old)):
a,b = new.ravel()
c,d = old.ravel()
cv2.circle(frame,(a, b),5,color[i].tolist(),-1)
if i == 99:
break

cv2.imshow('frame',frame)
k = cv2.waitKey(30) & 0xff
if k == 27:
break

# Now update the previous frame and previous points
old_gray = frame_gray.copy()
p0 = good_new.reshape(-1,1,2)

cv2.destroyAllWindows()
cap.release()

最佳答案

这是代码片段:

p0 = np.array([[[x,y]], [[x0,y0]]], np.float32)

只需将p0替换为原始代码,然后将x,x0 ...分配给您想要的点
-确保其为二维数组
-类型为float 32,表示单精度

关于opencv - OpenCV:Lucas Kanade应用于特定区域(以检测面部特征),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27956181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com