【问题标题】:ambiguous results in face and eye detection when image is resized调整图像大小时面部和眼睛检测的模糊结果
【发布时间】:2014-03-30 10:29:30
【问题描述】:

我正在尝试使用 Python 在 Open CV 中进行面部和眼睛检测的代码。该代码适用于图像大小 2848 X 4272,即使我将其大小调整了 0.5 倍。但是,每当我用其他因素(例如 0.2、0.4 等)调整它的大小时,它会给我的眼睛(例如前额、鼻子的几个区域)带来模棱两可的结果。在这种情况下,我无法获得所有图像大小的通用代码.是否有任何代码可以让我对任何图像尺寸进行正确检测,因为处理如此大的图像非常困难。代码是这样的

import numpy as np
import cv2
import cv2.cv as cv

#attaching the haar cascade files
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

# reading the image
img11 = cv2.imread('IMG_0347.JPG')

if img11 !=None:

# resizing the image
    w,h,c= img11.shape
    print "dimension"
    print w,h
    img = cv2.resize(img11,None,fx=0.4, fy=0.3, interpolation =   cv2.INTER_LINEAR)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # converting into grayscale
    gray = cv2.equalizeHist(gray)
    #cv2.imshow('histo',gray)
    w,h,c= img.shape # finding out the dimensions of the image i.e width, height and number of channels

# creating a white background of same dimensions as input image for pasting the eyes detected by 'haarcascade_eye.xml'
    im = np.zeros((w,h,c),np.uint8)
    im[:]=[255,255,255]

# creating a white background of same dimensions as input image for pasting the masked eyes
    im_mask = np.zeros((w,h,c),np.uint8)
    im_mask[:]=[255,255,255]

# faces gives the top left coordinates of the detected face and width and height of the rectangle
    faces = face_cascade.detectMultiScale(gray, 1.5, 5)

# taking face as the ROI
    for (x,y,w,h) in faces:
                cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),1) # Draws the rectangle around the detected face
                roi_gray = gray[y:y+h, x:x+w]
                roi_color = img[y:y+h, x:x+w]
                #cv2.imshow('image1',img)  # shows the original image with face detected
                #cv2.imshow('image1',roi_color)  # shows only the face detected (colored)

# searching for eyes in the detected face i.e in the roi gray
                eyes = eye_cascade.detectMultiScale(roi_gray)
#print eyes # prints the top left coordinates of the detected eyes and width and height of the rectangle
                if eyes.any():
                    for (ex,ey,ew,eh)in eyes:
                        cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),1) # draws rectangle around the masked eyes
                        eye_mask= roi_color[ey+1:u, ex+1:ex+ew]                             # eye_mask is the masked portion of the detected eye extracted from roi_color
                        im_mask[ey+1+y:y+u, ex+x+1:ex+ew+x]=eye_mask         #pasting the eye_mask on the white background called im_mask
                else:
                        print ("eyes could not be detected")

            cv2.imshow('image',im_mask)   #shows the im-mask white background with masked eyes pasted on it

【问题讨论】:

    标签: python opencv face-detection


    【解决方案1】:

    例如,随着图像变得越来越小,区分眼睛和鼻子变得越来越困难,这是合乎逻辑的。因此,除非您从根本上了解您的图像分析功能正在寻找什么(我不知道),否则很难知道在保留分析所需信息类型的同时缩小图像尺寸的最佳方法。

    话虽如此,我相信cv2.INTER_AREAcv2.INTER_LINEAR 等更常用于缩小图像。

    试试这个而不是你有的调整大小:

    img = cv2.resize(img11, None, fx=0.4, fy=0.3, interpolation=cv2.INTER_AREA)
    

    另外,您不是通过更改图像的纵横比(fx != fy)来增加识别眼睛的难度吗?如果您没有特殊原因,您可以使用第二个位置参数 size 显式选择目标大小。例如:

    effective_but_smaller_size = (640, 480)  # or whatever you find works
    img = cv2.resize(img11, effective_but_smaller_size, interpolation=cv2.INTER_AREA)
    

    【讨论】:

    • 等一下,我会试着让你知道
    猜你喜欢
    • 2013-03-22
    • 2017-01-11
    • 1970-01-01
    • 2011-05-29
    • 2013-04-28
    • 2018-11-27
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    相关资源
    最近更新 更多