opencv and python: how to use cv2.surf() with mask

筅森魡賤 提交于 2021-02-07 20:41:52

问题


I am a newbie at opencv and python and am trying to collect keypoints and descriptors of faces within an image.

I am using HAAR cascade classifier with frontal face template to look for faces in an image. The HAAR cascade gives me a list of coordinates marking the faces in the image. I want to generate a "mask" at those coordinates so that I can use cv2.surf() to extract keypoints and descriptors within the masked region.

I don't know how to create that mask.

Try this photo as an example to work on.

Here is the code thus far:

import cv2
import numpy as np

# Load image and convert to grayscale
img = cv2.imread('testPhoto.jpg')
imgg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Look for faces in the image
cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
faceRegions = cascade.detectMultiScale(imgg)

After this I'd like to do the SURF extraction with a mask using faceRegions. Suppose faceRegions looks like this:

array([[488, 163,  91,  91],
       [357, 184,  93,  93],
       [154,  78, 107, 107]], dtype=int32)

There were three faces found in imgg so I want to create three separate masks at their specific locations with their particular width and height. And then have cv2.surf() look only in the masked regions. How can I do that?


回答1:


The faceRegions you got denote the x,y,width,height of the faces. So you can simply set a ROI (Region of Interest) with these coordinates and send that rectangle as an image to your SURF function.

For eg:

face1 = imgg[y:y+height, x:x+width]

Now you can pass this face1 to cv2.SURF() instead of passing the full image.



来源:https://stackoverflow.com/questions/16811371/opencv-and-python-how-to-use-cv2-surf-with-mask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!