python 人脸清晰度_调整图像大小时人脸和眼睛检测中的模糊结果

该博客讨论了使用Python OpenCV进行人脸识别和眼睛检测时遇到的问题。当图像大小调整到特定比例(如0.2或0.4)时,检测结果变得模糊。博主寻求一种通用方法,能在不同尺寸的图像上准确检测人脸和眼睛,而不只是在原始或特定缩小比例的图像上。代码示例展示了如何加载haar级联文件,读取图像,调整大小,转换为灰度并检测面部和眼睛。
摘要由CSDN通过智能技术生成

我正在使用Python在opencv中尝试一个人脸和眼睛检测代码。这段代码适用于2848x4272的图像,甚至当我将其调整为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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值