Python OpenCV:为什么 fillPoly() 只绘制灰色多边形,而不考虑其颜色参数?
Python OpenCV: Why does fillPoly() only draw grey polygons, regardless of its color argument?
我正在尝试在 OpenCV 中使用 Python:
在黑色二维 NumPy 数组(具有一个通道的图像)上写一个白色掩码
mask = np.zeros(shape=(100, 100), dtype=np.int8)
cv2.fillPoly(mask, np.array([[[0,0], [89, 0], [99,50], [33,96], [0,47]]], dtype=np.int32), color=255)
print(mask)
但是,当我打印蒙版时多边形是灰色的:
[[127 127 127 ... 0 0 0]
[127 127 127 ... 0 0 0]
[127 127 127 ... 0 0 0]
...
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]]
我尝试了 color=(255,255,255)
的 3D NumPy 数组,我尝试了不同的颜色,但都无济于事。为什么忽略 color
参数?
问题出在 mask
:
的初始化
mask = np.zeros(shape=(100, 100), dtype=np.int8)
int8
data type 的取值范围是 -128 ... 127
,因此任何高于 127
的值都将是 "truncated" 到 127
。
用 color=100
试试你的代码,你会得到预期的输出:
[[100 100 100 ... 0 0 0]
[100 100 100 ... 0 0 0]
[100 100 100 ... 0 0 0]
...
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]]
我猜,您想使用 uint8
而不是 int8
,所以这可能只是一个简单的错字!?
将您的代码相应地更改为
mask = np.zeros(shape=(100, 100), dtype=np.uint8)
然后给出预期的结果,也为 color=255
:
[[255 255 255 ... 0 0 0]
[255 255 255 ... 0 0 0]
[255 255 255 ... 0 0 0]
...
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]]
问题出在初始化numpy数组时的数据类型选择上。在您的示例代码中,您使用的是 np.int8
、which has a range from -128 ... 127.。您应该考虑使用 np.uint8
而不是 np.int8
,它的范围是 0 ... 255,您正在寻找。
mask = np.zeros(shape=(100, 100), dtype=np.int8)
应该是
mask = np.zeros(shape=(100, 100), dtype=np.uint8)
[[255 255 255 ... 0 0 0]
[255 255 255 ... 0 0 0]
[255 255 255 ... 0 0 0]
...
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]]
对我来说,问题不是用深度初始化掩码。
mask = np.zeros(shape = (MASK_WIDTH, MASK_HEIGHT), dtype=np.uint8)
用这段代码解决了
mask = np.zeros(shape = (MASK_WIDTH, MASK_HEIGHT, 3), dtype=np.uint8)
rcolor = list(np.random.random(size=3) * 256)
cv2.fillPoly(mask, [arr], color=rcolor)
cv2.imwrite(os.path.join(mask_folder, itr + ".jpg") , cv2.cvtColor(mask, cv2.COLOR_RGB2BGR))
我正在尝试在 OpenCV 中使用 Python:
在黑色二维 NumPy 数组(具有一个通道的图像)上写一个白色掩码mask = np.zeros(shape=(100, 100), dtype=np.int8)
cv2.fillPoly(mask, np.array([[[0,0], [89, 0], [99,50], [33,96], [0,47]]], dtype=np.int32), color=255)
print(mask)
但是,当我打印蒙版时多边形是灰色的:
[[127 127 127 ... 0 0 0]
[127 127 127 ... 0 0 0]
[127 127 127 ... 0 0 0]
...
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]]
我尝试了 color=(255,255,255)
的 3D NumPy 数组,我尝试了不同的颜色,但都无济于事。为什么忽略 color
参数?
问题出在 mask
:
mask = np.zeros(shape=(100, 100), dtype=np.int8)
int8
data type 的取值范围是 -128 ... 127
,因此任何高于 127
的值都将是 "truncated" 到 127
。
用 color=100
试试你的代码,你会得到预期的输出:
[[100 100 100 ... 0 0 0]
[100 100 100 ... 0 0 0]
[100 100 100 ... 0 0 0]
...
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]]
我猜,您想使用 uint8
而不是 int8
,所以这可能只是一个简单的错字!?
将您的代码相应地更改为
mask = np.zeros(shape=(100, 100), dtype=np.uint8)
然后给出预期的结果,也为 color=255
:
[[255 255 255 ... 0 0 0]
[255 255 255 ... 0 0 0]
[255 255 255 ... 0 0 0]
...
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]]
问题出在初始化numpy数组时的数据类型选择上。在您的示例代码中,您使用的是 np.int8
、which has a range from -128 ... 127.。您应该考虑使用 np.uint8
而不是 np.int8
,它的范围是 0 ... 255,您正在寻找。
mask = np.zeros(shape=(100, 100), dtype=np.int8)
应该是
mask = np.zeros(shape=(100, 100), dtype=np.uint8)
[[255 255 255 ... 0 0 0]
[255 255 255 ... 0 0 0]
[255 255 255 ... 0 0 0]
...
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]
[ 0 0 0 ... 0 0 0]]
对我来说,问题不是用深度初始化掩码。
mask = np.zeros(shape = (MASK_WIDTH, MASK_HEIGHT), dtype=np.uint8)
用这段代码解决了
mask = np.zeros(shape = (MASK_WIDTH, MASK_HEIGHT, 3), dtype=np.uint8)
rcolor = list(np.random.random(size=3) * 256)
cv2.fillPoly(mask, [arr], color=rcolor)
cv2.imwrite(os.path.join(mask_folder, itr + ".jpg") , cv2.cvtColor(mask, cv2.COLOR_RGB2BGR))