有什么方法可以从图像的封闭空间中除去背景色?

时间:2019-04-15 09:35:48

标签: python python-3.x opencv png jpeg

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

我使用了here提供的解决方案。我根据用例对其进行了修改,并能够成功删除对象外部的背景色。

例如: JPG图片:

Image with background colour

PNG图片:

Image without background colour

我需要从右脖子和头发附近的小封闭空间中去除背景色。我们可以使用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)

0 个答案:

没有答案