【发布时间】:2019-06-02 14:02:18
【问题描述】:
我有白色背景的标志(任意形状的标志)图像,我想获得具有透明背景的标志图像。
我设法创建了一个蒙版并将其应用于图像,并认为使蒙版透明是可行的。我在这里和其他地方搜索了很多,但没有任何帮助。
import cv2
import numpy as np
file_name = "/path/to/input/img/Unbenannt.jpg" # can be also .png
img = cv2.imread(file_name)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th, threshed = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11,11))
morphed = cv2.morphologyEx(threshed, cv2.MORPH_CLOSE, kernel)
_, roi, _ = cv2.findContours(morphed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
mask = np.zeros(img.shape, img.dtype)
cv2.fillPoly(mask, roi, (255,)*img.shape[2], )
masked_image = cv2.bitwise_and(img, mask)
cv2.imwrite("/path/to/output/mask_test.png", masked_image)
输入:
电流输出:
如前所述,我想让背景透明。
非常感谢您的帮助。
【问题讨论】:
-
您好,谢谢您的回答。是的,以前见过。但它一开始还是没有用。无论如何,我同时想通了,并将在考试阶段之后(〜二月中旬)在这里发布解决方案;)
标签: transparency mask opencv-python