Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在没有背景的情况下保存图像opencv_Python_Python 3.x_Opencv - Fatal编程技术网

Python 在没有背景的情况下保存图像opencv

Python 在没有背景的情况下保存图像opencv,python,python-3.x,opencv,Python,Python 3.x,Opencv,我想创建一个小scrypt,创建没有背景的png图像。我已经阅读了一些信息,我不确定这是否可以通过opencv实现。(如果这是一个愚蠢的问题,很抱歉,但我是这个库的新手) 创建图像很容易 import cv2 import numpy as np # Create a black image img = np.zeros((512,512,3), np.uint8) # Drawing a circle circle = cv2.circle(img,(256,256), 63, (0,0,

我想创建一个小scrypt,创建没有背景的png图像。我已经阅读了一些信息,我不确定这是否可以通过
opencv
实现。(如果这是一个愚蠢的问题,很抱歉,但我是这个库的新手)

创建图像很容易

import cv2
import numpy as np

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Drawing a circle
circle = cv2.circle(img,(256,256), 63, (0,0,255), -1)

# 
cv2.imwrite('circle.png',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
但是,是否可以在没有背景的情况下保存它?在本例中,是否可以只保存圆


非常感谢

我在下面的代码中为图像添加了透明通道(或层)。它会给人一种背景中没有任何东西的感觉

import cv2
import numpy as np

# Create a black image
img = np.zeros((512,512, 4), np.uint8)

# Drawing a circle
circle = cv2.circle(img, (256,256), 63, (0,0,255, 255), -1)

# 
cv2.imwrite('circle.png',img)


你说没有背景是什么意思?你是说透明吗?甚至透明度也是一个背景。如果你想要你的红色圆圈在一个透明的背景上,为圆圈制作一个二元遮罩,并将其放入红色圆圈图像的alpha通道中。
import cv2
import numpy as np

# Create a black image
img = np.zeros((512,512,3), np.uint8)

# Drawing a circle
circle = cv2.circle(img, (256,256), 63, (0,0,255), -1)

# Convert circle to grayscale
gray = cv2.cvtColor(circle, cv2.COLOR_BGR2GRAY)

# Threshold to make a mask
mask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)[1]

# Put mask into alpha channel of Circle
result = np.dstack((circle, mask))

# 
cv2.imwrite('circle.png',result)