CV2的detectMultiScale方法返回什么?

问题描述 投票:2回答:2

在CV2中,我可以从上传的图像生成面部。

faces = faceCascade.detectMultiScale(
    read_img,
    scaleFactor = 1.1,
    minNeighbors = 0,
    minSize=(100,100)
)
how_many_faces = len(faces)

how_many_faces返回正确的面数。

如果我将这些面附加到数组中......

our_faces = []
for i in faces:
    our_faces.append(i)

return str(our_faces)

...并返回our_faces,我得到以下数据:

[array([187,138,236,236],dtype = int32),array([197,138,236,236],dtype = int32),array([163,130,260,260],dtype = int32 ),array([163,141,260,260],dtype = int32),array([173,141,260,260],dtype = int32),array([184,141,260,260],dtype = int32),array([143,119,286,286],dtype = int32),array([167,119,286,286],dtype = int32),array([143,131,286,286],dtype = int32),array([155,131,286,286],dtype = int32),array([167,131,286,286],dtype = int32),array([144,105,315,315], dtype = int32),array([157,105,315,315],dtype = int32),array([131,118,315,315],dtype = int32),array([144,118,315,315] ,dtype = int32),array([157,118,315,315],dtype = int32),array([170,118,315,315],dtype = int32),array([130,87,346,346] ],dtype = int32),array([115,101,346,346],dtype = int32),array([130,101,346,346],dtype = int32),array([144,101,346, 346],dtype = int32),数组([159,101,346,346],dtype = int32),数组([130,115,346,346],dtype = int32),数组([87,70,419] ,419],dtype = i NT32)]

我是否正确地假设这个数组包含每个面的所有数据,并且它是一个Numpy数组?如果是这样,我如何将数组中的数据转换回图像格式?

python arrays opencv cv2
2个回答
3
投票

faceCascade.detectMultiScale()返回一个矩形列表,因此它不包含检测到的面部的图像,并且您无法完全从该列表重建面部。

如果你想获得面部图像,你需要:

  • 保留您最初寻找面孔的图像副本,以及
  • 使用Numpy切片或类似方法提取其边界位于faces返回的faceCascade.detectMultiScale()列表中的矩形

1
投票
def crop(image, faces, k=0):
"""
This function crops the initial image into faces' images seperately. 
Arguments:
    image  (np array image)
    faces (list of tuples)
"""
faces_arrays = []
for (top, right, bottom, left)in faces:
    x0, y0 = left, bottom
    x1, y1 = right, top
    w, h = right-left, top-bottom
    cv2.rectangle(img=image, pt1=(x0, y0), pt2=(x1, y1), color=(255,0,0), thickness=2)
    x2, x3 = x1-w,  x0+w
    # crop the region of interest over a copy 
    face = image[y1:y0, x2:x3].copy()
    faces_arrays.append(face)
    # comment the two following lines if you want to stop saving the crops
    cv2.imwrite('face'+str(k)+'.jpg', face)
    k += 1
return faces_arrays
© www.soinside.com 2019 - 2024. All rights reserved.