尝试使用OpenCV-Python制作Passport Photo

时间:2017-07-24 13:31:45

标签: python-2.7 opencv

提前道歉,因为我是OpenCV-Python的新手。我为自己设置了一个从视频捕获中创建Passport类型图像的任务。

使用头部和肩部Haar Cascade我能够创建一个肖像照片但我现在想要将背景变成白色背景(将头部和肩部的肖像留在前景中)。

只是不确定如何/最佳方式来做到这一点。欢迎任何帮助。

非常感谢提前。

以下是代码:

.container {
  width: 400px;
  height: 200px; 
  background-color: grey;
  display: flex;
  flex-direction: row;
}

.content {
  width: 100%;
  margin: 20px;
  border: 1px solid red;
  margin: 10px;
}

.sidebar {
  background-color: lightgrey;
  display: flex;
  flex-flow: column wrap-reverse;
  overflow: hidden;
}

.sidebar:hover { overflow: visible; width: 75px; }

.icon {
  width: 20px;
  height: 20px;
  padding: 5px;
}

.icon path { fill: white }

1 个答案:

答案 0 :(得分:0)

我得到了它的工作。这是我的解决方案。

请注意:这适用于HI-RES图像(Nikon D7100 - JPEG)。当我尝试使用网络摄像头(Logitech C615)时,LOW-RES无效。

我使用了建议链接中的一些代码。

# import numpy
import numpy as np
# import cv2
import cv2
# import Matplitlib
from matplotlib import pyplot as plt


# Fill any holes function
def get_holes(image, thresh):
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    im_bw = cv2.threshold(gray, thresh, 255, cv2.THRESH_BINARY)[1]
    im_bw_inv = cv2.bitwise_not(im_bw)

    im_bw_inv, contour, _ = cv2.findContours(im_bw_inv, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
    for cnt in contour:
        cv2.drawContours(im_bw_inv, [cnt], 0, 255, -1)

    nt = cv2.bitwise_not(im_bw)
    im_bw_inv = cv2.bitwise_or(im_bw_inv, nt)
    return im_bw_inv

# Remove background Function
def remove_background(image, thresh, scale_factor=.25, kernel_range=range(1, 15), border=None):
    border = border or kernel_range[-1]

    holes = get_holes(image, thresh)
    small = cv2.resize(holes, None, fx=scale_factor, fy=scale_factor)
    bordered = cv2.copyMakeBorder(small, border, border, border, border, cv2.BORDER_CONSTANT)

    for i in kernel_range:
        #kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2*i+1, 2*i+1))
        kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (2*i+1, 2*i+1))
        bordered = cv2.morphologyEx(bordered, cv2.MORPH_CLOSE, kernel)

    unbordered = bordered[border: -border, border: -border]
    mask = cv2.resize(unbordered, (image.shape[1], image.shape[0]))
    fg = cv2.bitwise_and(image, image, mask=mask)
    return fg


# Load a color image in grayscale
img = cv2.imread('original/11.png')
# Start background removal -- Parameters are <image> and <threshold level>
nb_img = remove_background(img, 180)
# Change Black Pixels to WHITE
nb_img[np.where((nb_img==[0,0,0]).all(axis=2))] = [255,255,255]

# resize the viewing size (as the images are too big for the screen
small = cv2.resize(nb_img, (300, 400)) 

# Show the finished image
cv2.imshow('image',small)

k = cv2.waitKey(0) & 0xFF
if k == 27:  #wait for ESC key to exit
    # if ESC pressed close the camera windows
    cv2.destroyAllWindows()
elif k == ord('s'): #wait for 's' key to save and exit
    # Save the img(greyscale version)
    cv2.imwrite('bg_removal/11.png',small)
    cv2.destroyAllWindows()