使用 cv2 在 python 中创建一个多通道零垫
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16235955/
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
create a multichannel zeros mat in python with cv2
提问by nkint
i want to create a multichannel mat object in python with cv2 opencv wrapper.
我想用 cv2 opencv 包装器在 python 中创建一个多通道 mat 对象。
i've found examples on the net where the c++ Mat::zeros is replaced with numpy.zeros, that seems good. but no multichannel type seems to fit..
我在网上找到了用 numpy.zeros 替换 c++ Mat::zeros 的例子,这看起来不错。但似乎没有多通道类型适合..
look at the code:
看代码:
import cv2
import numpy as np
size = 200, 200
m = np.zeros(size, dtype=np.uint8) # ?
m = cv2.cvtColor(m, cv2.COLOR_GRAY2BGR)
p1 = (0,0)
p2 = (200, 200)
cv2.line(m, p1, p2, (0, 0, 255), 10)
cv2.namedWindow("draw", cv2.CV_WINDOW_AUTOSIZE)
while True:
cv2.imshow("draw", m)
ch = 0xFF & cv2.waitKey(1)
if ch == 27:
break
cv2.destroyAllWindows()
i want to avoid m = cv2.cvtColor(m, cv2.COLOR_GRAY2BGR)but neither cv2.CV_8UC3np.uin32works.
我想避免,m = cv2.cvtColor(m, cv2.COLOR_GRAY2BGR)但都不行cv2.CV_8UC3np.uin32。
some hint?
一些提示?
采纳答案by gatto
Try this as size:
试试这个size:
size = 200, 200, 3
m = np.zeros(size, dtype=np.uint8)
Basically what I did to find what arguments I need for the matrix is:
基本上,我为找到矩阵所需的参数所做的工作是:
img = cv2.imread('/tmp/1.jpg')
print img.shape, img.dtype
# (398, 454, 3), uint8
But one could probably find it in OpenCV documentation as well.
但是人们也可能在 OpenCV 文档中找到它。

