Python Opencv 将 numpy.zeros 绘制为灰度图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18945785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:21:34  来源:igfitidea点击:

Opencv draws numpy.zeros as a gray image

pythonopencvnumpy

提问by Vanuan

I'm struggling with understanding how opencv interprets numpy arrays.

我正在努力理解 opencv 如何解释 numpy 数组。

import cv2
import numpy as np

if __name__ == '__main__':
    size = (w, h, channels) = (100, 100, 1)
    img = np.zeros(size, np.int8)
    cv2.imshow('result', img), cv2.waitKey(0)
    cv2.destroyAllWindows()

Grayscale black 100x100 image, right? No, it's showing me gray! Why's that?

灰度黑色 100x100 图像,对吗?不,它显示我是灰色的!为什么?

回答by Vanuan

Ok, the crucial part is dtype. I've chosen np.int8. When I use np.uint8, it is black.

好的,关键部分是 dtype。我选择了np.int8。当我使用时np.uint8,它是黑色的。

Suprisingly, when dtype=np.int8, zeros are interpreted as 127(or 128)!

令人惊讶的是,当dtype=np.int8,零被解释为 127(或 128)!

I expected that zero is still zero, no matter if it is signed or unsigned.

我预计零仍然为零,无论它是有符号还是无符号。

回答by Mateen Ulhaq

For a BGR image,

对于 BGR 图像,

img = np.zeros([height, width, 3], dtype=np.uint8)