首页 > 解决方案 > 有什么方法可以从图像中的封闭空间中删除背景颜色?

问题描述

我正在尝试从 JPG 格式的图像中删除背景颜色并将其保存为 PNG 格式。即使从图像中存在的封闭空间中,也应完全删除背景颜色(参见下面的示例)。

我使用了此处提供的解决方案。我根据我的用例对其进行了修改,并且能够成功移除对象外部存在的背景颜色。

例如: JPG图片:

带有背景颜色的图像

PNG图像:

没有背景颜色的图像

我需要从靠近右脖子和头发的小封闭空间中去除背景颜色。我们可以使用 python opencv 来实现它吗?

file_name = "hello.jpg"

img = cv2.imread(file_name)

# == Parameters =======================================================================
BLUR = 5
CANNY_THRESH_1 = 127
CANNY_THRESH_2 = 255
MASK_DILATE_ITER = 2
MASK_ERODE_ITER = 2
MASK_COLOR = (0.0, 0.0, 1.0)  # In BGR format

# == Processing =======================================================================

# -- Change the image to gray scale image -----------------------------------------------
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# -- Edge detection -------------------------------------------------------------------
edges = cv2.Canny(gray, CANNY_THRESH_1, CANNY_THRESH_2)
edges = cv2.dilate(edges, None)
edges = cv2.erode(edges, None)

# -- Find contours in edges, sort by area ---------------------------------------------
contour_info = []
# _, contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
# Previously, for a previous version of cv2, this line was:
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

for c in contours:
    contour_info.append((
        c,
        cv2.isContourConvex(c),
        cv2.contourArea(c),
    ))
contour_info = sorted(contour_info, key=lambda c: c[2], reverse=True)
max_contour = contour_info[0]

# -- Create empty mask, draw filled polygon on it corresponding to largest contour ----
# Mask is black, polygon is white
mask = np.zeros(edges.shape)
cv2.fillConvexPoly(mask, max_contour[0], (255))

# -- Smooth mask, then blur it --------------------------------------------------------
mask = cv2.dilate(mask, None, iterations=MASK_DILATE_ITER)
mask = cv2.erode(mask, None, iterations=MASK_ERODE_ITER)
mask = cv2.GaussianBlur(mask, (BLUR, BLUR), 0)
mask_stack = np.dstack([mask]*3)    # Create 3-channel alpha mask

# -- Blend masked img into MASK_COLOR background --------------------------------------
mask_stack = mask_stack.astype('float32') / 255.0            # Use float matrices,
img = img.astype('float32') / 255.0                          # for easy blending

masked = (mask_stack * img) + ((1-mask_stack) * MASK_COLOR)  # Blend
masked = (masked * 255).astype('uint8')                      # Convert back to 8-bit

# Display
cv2.imshow('img', masked)
cv2.waitKey(5000)

# split image into channels
c_red, c_green, c_blue = cv2.split(img)

# merge with mask got on one of a previous steps
img_a = cv2.merge((c_red, c_green, c_blue, mask.astype('float32') / 255.0))

# save to disk
cv2.imwrite("/Documents/jpg-to-png/converted.png", img_a*255)

标签: pythonpython-3.xopencvpngjpeg

解决方案


推荐阅读