OpenCV ROI背景填充

时间:2017-01-12 23:37:52

标签: python opencv numpy

我试图通过使用Numpy裁剪来专注于脸部的感兴趣区域。对于大图像,我没有遇到麻烦。但是对于较小的那些,我可以找到面部并找到匹配的边界框,但是当我尝试裁剪它并显示图像时,我得到图像右侧的灰色背景。我不介意它,但当我尝试将轮廓应用于图像时,底部和右边缘被拾取为轮廓边缘。如何确保灰色填充不会影响我的轮廓?

这是我的功能:

def head_ratio(self):
    face_cascade  = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    faces = face_cascade.detectMultiScale(self.gray, 1.3, 5)
    m_height, m_width, channels = self.image.shape
    x, y, w, h = faces[0]
    ROI = self.image[y:y+h,x:x+w]
    kernel = np.ones((5,5), np.float32)/10
    blurred = cv2.filter2D(self.gray[y:y+h, x:x+w], -1, kernel)
    ret, thresh = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY)
    _,contours,_ =  cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(ROI, contours, -1, (255, 255, 255), 3) 
    cv2.imshow("output", ROI)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    return 1

感谢您的帮助。我一直在努力寻找答案,但在寻找什么方面遇到了麻烦。

0 个答案:

没有答案
相关问题