gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:13:14 26 4
gpt4 key购买 nike

提前致歉,因为我是 OpenCV-Python 的新手。我为自己设定了一项任务,即根据视频捕获创建一张护照类型的图像。

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

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

非常感谢。

代码如下:

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()

最佳答案

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

请注意:这适用于高分辨率图像 (Nikon D7100 - JPEG)。当我尝试使用网络摄像头 (Logitech C615) 时,低分辨率不起作用。

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

# 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()

关于python-2.7 - 尝试使用 OpenCV-Python 制作护照照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45282019/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com