[英]Opencv Region of Interest
I have a program that detects a face when the the web cam i recording.我有一个程序可以在录制 web 摄像头时检测到人脸。 I've created a region of interest and i want to only detect faces only within the roi.
我创建了一个感兴趣区域,我只想检测 roi 内的人脸。 Im trying to instruct my program to operate only in that region.
我试图指示我的程序仅在该区域运行。 Have no clue how to
不知道该怎么做
cap = cv2.VideoCapture(0)
Cascade_face = cv2.CascadeClassifier('C:\\Users\moham\PycharmProjects\Face\cascade\cascade.xml')
roi = cap[40:520,340:550]
while True:
success, img = cap.read()
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = Cascade_face.detectMultiScale(imgGray, 1.3, 5)
for (x, y, w, h) in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)
cv2.imshow('face_detect', img)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyWindow('face_detect')
Try this for ROI.试试这个以获得投资回报率。 I do not have cascade.xml.
我没有 cascade.xml。 Actually, I cannot test it.
实际上,我无法测试它。
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
cv2.imshow('face_detect',img)
k = cv2.waitKey(30) & 0xff
if k == 27: # press 'ESC' to quit
break
cap.release()
cv2.destroyAllWindows()
You need to create a mask using the coordinates of the ROI.您需要使用 ROI 的坐标创建掩码。
Sample image taken from this link :取自此链接的示例图片:
Code:代码:
img = cv2.imread('crowd.jpg')
# create background to draw the mask
black = np.zeros((img.shape[0], img.shape[1]), np.uint8)
#ROI for this image: img[40:180, 130:300]
# create the mask using ROI coordinates
black = cv2.rectangle(black, (180, 40), (300, 130), 255, -1)
# masking the image
roi = cv2.bitwise_and(img, img, mask = black)
Now you can perform face detection on roi
.现在你可以对
roi
进行人脸检测了。 You need to incorporate the above snippet in your code accordingly.您需要相应地将上述代码段合并到您的代码中。
Note: To draw rectangle,
cv2.rectangle()
follows (x1, y1), (x2, y2) order.注意:要绘制矩形,
cv2.rectangle()
遵循 (x1, y1), (x2, y2) 顺序。 But to crop an image, the order is reversed:crop_img = img[y1:x1, y2:x2]
但是要裁剪图像,顺序是相反的:
crop_img = img[y1:x1, y2:x2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.