gpt4 book ai didi

Python OpenCV : Why does fillPoly() only draw grey polygons, 无论其颜色参数如何?

转载 作者:行者123 更新时间:2023-12-02 16:14:00 27 4
gpt4 key购买 nike

我正在尝试使用 Python 在 OpenCV 中的黑色二维 NumPy 数组(具有一个 channel 的图像)上写一个白色掩码:

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 = np.zeros(shape=(100, 100), dtype=np.int8)

int8 data type的取值范围是 -128 ... 127,因此任何高于 127 的值都将被“截断”为 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]]

关于Python OpenCV : Why does fillPoly() only draw grey polygons, 无论其颜色参数如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56255778/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com