Python 通过转换为PNG使JPEG图像中的背景透明
在下图中,我试图使背景透明。见下图 使用Opencv和matplotlib,我能够实现这一点Python 通过转换为PNG使JPEG图像中的背景透明,python,numpy,opencv,matplotlib,image-processing,Python,Numpy,Opencv,Matplotlib,Image Processing,在下图中,我试图使背景透明。见下图 使用Opencv和matplotlib,我能够实现这一点 import cv2 import numpy as np from matplotlib import pyplot as plt #== Parameters ======================================================================= BLUR = 21 CANNY_THRESH_1 = 10 CANNY_THRESH_2 =
import cv2
import numpy as np
from matplotlib import pyplot as plt
#== Parameters =======================================================================
BLUR = 21
CANNY_THRESH_1 = 10
CANNY_THRESH_2 = 200
MASK_DILATE_ITER = 10
MASK_ERODE_ITER = 10
MASK_COLOR = (0.0,0.0,1.0) # In BGR format
#== Processing =======================================================================
#-- Read image -----------------------------------------------------------------------
img = cv2.imread('/home/hasher/Documents/30302649.jpg')
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)
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
# plt.imsave('/home/hasher/Documents/girl_blue.png', masked)
# 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_blue, c_green, c_red, mask.astype('float32') / 255.0))
# show on screen (optional in jupiter)
#%matplotlib inline
plt.imshow(img_a)
plt.show()
# save to disk
# cv2.imwrite('/home/hasher/Documents/girl_1.png', img_a*255)
# or the same using plt
plt.imsave('/home/hasher/Documents/transparent.png', img_a)
# cv2.imshow('img', masked) # Displays red, saves blue
cv2.waitKey()
我能够将图像转换为(参见图像)。
但代码中有一些小问题。转换图像的边框中有一些额外的细节。我搞不懂。感谢您的帮助
转换前的样品。
任务:将具有特定颜色背景的jpeg转换为透明png
(1) JPEG
(2) 对于这些JPEG,请将其转换为HSV和分割通道。然后我们可以在V通道中分离目标,因为背景与其他通道的差异最大
(3) 设置V通道的阈值并进行变形操作,然后我们可以得到一个alpha蒙版和png
守则:
为什么MASK\u COLOR
仅在红色通道上设置为1
。将所有通道设置为1.0并进行测试。还要注意的是,split And merge函数使用蓝色、绿色和红色的顺序格式作为默认格式。在您的代码中,c_red实际上有蓝色像素,而在我的例子中,我的所有原始图像都有我想要使背景透明的背景。我想做反转。有没有做反转的建议或想法?如果图像是无损格式且背景颜色不变,那么我们可以尝试inRange
查找颜色掩码。但众所周知,JPEG
是有损压缩
格式。因此,恢复图像中的边缘信息
并不容易。如果源代码的格式是PNG
格式,可能很容易处理。初始转换后,我将以PNG格式保存图像。我可以做进一步的处理,以达到所需的输出吗?你能提供一些PNG样本吗?
import cv2
import numpy as np
fname = "alpha.jpg"
img = cv2.imread(fname)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
v = hsv[:,:,2]
th, threshed = cv2.threshold(v, 100, 255, cv2.THRESH_OTSU|cv2.THRESH_BINARY_INV)
threshed[-1] = 255
cnts = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2]
mask = np.zeros_like(threshed)
cv2.drawContours(mask, cnts, -1, (255, 0, 0), -1, cv2.LINE_AA)
mask = cv2.erode(mask, np.ones((3,3), np.int32), iterations=1)
png = np.dstack((img, mask))
cv2.imwrite("alpha.png", png)