2014-02-27 75 views
2

我正在嘗試使用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 

回答

1

這是合乎邏輯的圖像變得越來越小,變得難以從鼻子區分的眼睛,例如。所以除非你從根本上理解你的圖像分析功能在尋找什麼(我沒有),否則很難知道縮小圖像尺寸的最佳方式,同時保留分析所需的信息類型。

話雖如此,我相信cv2.INTER_AREA用於除cv2.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) 
+0

只是等待我會試着讓你知道 – Anuradha