使用OpenCV-Python尝试制作护照照片

4

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

使用头部和肩膀Haar级联分类器,我能够创建一张肖像照片,但我现在想将背景变成白色(保留前景中的头部和肩膀肖像)。

只是不确定如何/最好的方法来实现这一点。任何帮助将不胜感激。

非常感谢。

以下是代码:

import numpy as np
import cv2

# face file
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# eye file
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
# head shoulders file
hs_cascade = cv2.CascadeClassifier('HS.xml')

cap = cv2.VideoCapture(1)

while 1:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    headshoulders = hs_cascade.detectMultiScale(gray, 1.3, 3)

    # find the head and shoulders
    for (x,y,w,h) in headshoulders:
        # variable change to make portrait orientation
        x = int(x*1.5)
        w = int(w/1.5)
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

        # crop the image
        crop_img = img[y: y + h, x: x + w]

        # show original and crop
        cv2.imshow('crop', crop_img)
        cv2.imshow('img', img)

    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
    elif k == ord('s'):
        # save out the portrait image
        cv2.imwrite('cropimage.png',crop_img)

# release the camera
cap.release()
cv2.destroyAllWindows()

你能贴出你写过的一些代码吗?并且看一下这个链接:http://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html 还有这个链接:http://scikit-image.org/ - Dadep
我的原始问题已经添加了代码。 - J Elliot
请查看以下链接:https://dev59.com/uF0Z5IYBdhLWcg3wvCSd 和 http://docs.opencv.org/3.1.0/d8/d83/tutorial_py_grabcut.html - Dadep
非常感谢您的指导。我会尝试并查看是否能够得到我想要的结果。 :-) - J Elliot
Dadep。再次感谢您。经过一些代码调整,我得到了一个非常接近我想要的结果。 :-) - J Elliot
欢迎您,如果您有一些脚本正在运行,您可以回答自己的帖子,这可能会在未来帮助到其他人! - Dadep
1个回答

0

我解决了它。这是我的解决方案。

请注意:这适用于HI-RES图像(尼康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()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接