python - 有什么方法可以从图像中的封闭空间中删除背景颜色?
问题描述
我正在尝试从 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)
解决方案
推荐阅读
- graphhopper - Graphhopper - 如何设置路由配置文件'foot'
- javascript - 使用 jquery 在两个选项之间附加一个选项
- php - 为什么这个数组没有在 foreach 循环之外正确设置?
- java - 指定用于创建索引的方法
- python-3.x - Python:Traceback(最近一次调用最后一次):引发 ConnectionError(e,request=request)requests.exceptions.ConnectionError:
- php - 当用户在 swift 中单击推送通知时打开自定义 url
- python - Seaborn 中的联合绘图
- safari - Safari 网络检查器不显示元素选项卡
- jquery - 在数据表 DOM 选项中定位元素
- c++ - 如何使用 Visual Studio 为 Raspberry Pi 调试 Linux TUI 应用程序