简体   繁体   English

OpenCV Python - 设置背景颜色

[英]OpenCV Python - Set background colour

I am trying to remove a greyish background from a photo and replace it with a white one我正在尝试从照片中删除灰色背景并将其替换为白色背景

so far I have this code:到目前为止,我有这个代码:

image = cv2.imread(args["image"])
r = 150.0 / image.shape[1]
dim = (150, int(image.shape[0] * r))
resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
lower_white = np.array([220, 220, 220], dtype=np.uint8)
upper_white = np.array([255, 255, 255], dtype=np.uint8)
mask = cv2.inRange(resized, lower_white, upper_white) # could also use threshold
res = cv2.bitwise_not(resized, resized, mask)
cv2.imshow('res', res) # gives black background

The problem is that the image now has a black background as I have masked out the grey.问题是图像现在有黑色背景,因为我已经掩盖了灰色。 How can I replace the empty pixels with white ones?如何用白色像素替换空像素?

前后

You can use the mask to index the array, and assign just the white parts of the mask to white:您可以使用掩码来索引数组,并将掩码的白色部分分配给白色:

coloured = resized.copy()
coloured[mask == 255] = (255, 255, 255)

截屏

I really recommend you to stick with OpenCV, it is well optimized.我真的建议你坚持使用 OpenCV,它优化得很好。 The trick is to invert the mask and apply it to some background, you will have your masked image and a masked background, then you combine both.诀窍是反转蒙版并将其应用于某些背景,您将拥有蒙版图像和蒙版背景,然后将两者结合起来。 image1 is your image masked with the original mask, image2 is the background image masked with the inverted mask, and image3 is the combined image. image1 是用原始蒙版蒙版的图像,image2 是用反转蒙版蒙版的背景图像,而 image3 是组合图像。 Important.重要的。 image1, image2 and image3 must be of the same size and type. image1、image2 和 image3 的大小和类型必须相同。 The mask must be grayscale.遮罩必须是灰度的。

前景和背景被屏蔽然后合并

import cv2
import numpy as np

# opencv loads the image in BGR, convert it to RGB
img = cv2.cvtColor(cv2.imread('E:\\FOTOS\\opencv\\zAJLd.jpg'),
                   cv2.COLOR_BGR2RGB)
lower_white = np.array([220, 220, 220], dtype=np.uint8)
upper_white = np.array([255, 255, 255], dtype=np.uint8)
mask = cv2.inRange(img, lower_white, upper_white)  # could also use threshold
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)))  # "erase" the small white points in the resulting mask
mask = cv2.bitwise_not(mask)  # invert mask

# load background (could be an image too)
bk = np.full(img.shape, 255, dtype=np.uint8)  # white bk

# get masked foreground
fg_masked = cv2.bitwise_and(img, img, mask=mask)

# get masked background, mask must be inverted 
mask = cv2.bitwise_not(mask)
bk_masked = cv2.bitwise_and(bk, bk, mask=mask)

# combine masked foreground and masked background 
final = cv2.bitwise_or(fg_masked, bk_masked)
mask = cv2.bitwise_not(mask)  # revert mask to original

At first, you need to get the background.首先,您需要了解背景。 To this must be subtracted from the original image with the mask image.为此必须从原始图像中减去蒙版图像。 And then change the black background to white (or any color).然后将黑色背景更改为白色(或任何颜色)。 And then back to add with the image of the mask.然后返回添加蒙版的图像。 Look here OpenCV grabcut() background color and Contour in Python看这里OpenCVgrabcut() 背景颜色和 Python 中的轮廓

First convert to GRAY and then threshold with cv2.threshold and then use numpy masking...首先转换为 GRAY,然后使用 cv2.threshold 进行阈值转换,然后使用 numpy 掩码...

ret, thresh = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 220, 255, cv2.THRESH_BINARY)
img[thresh == 255] = 255

If need black background set RHS to zero instead of 255如果需要黑色背景,请将 RHS 设置为零而不是 255

Instead of using bitwise_not, I would use而不是使用 bitwise_not,我会使用

resized.setTo([255, 255, 255], mask)

Before doing that I'd also erode and dilate and the mask, to get rid of the specs in the mask that are part of the image you want to keep.在这样做之前,我还会侵蚀和扩张蒙版,以摆脱蒙版中的规格,这些规格是您想要保留的图像的一部分。 http://docs.opencv.org/doc/tutorials/imgproc/erosion_dilatation/erosion_dilatation.html http://docs.opencv.org/doc/tutorials/imgproc/erosion_dilation/erosion_dilation.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM