简体   繁体   English

如何在 Python 中使用 OpenCV 创建 Mat 矩阵?

[英]How do I create a Mat matrix using OpenCV in Python?

Obviously this line is wrong.显然这条线是错误的。

matOut = cv2::Mat::Mat(height, width, cv2.CV_8UC4)

this one too.这个也是。

Mat matOut(height, width, cv2.CV_8UC4)

How do I create an modern openCV empty matrix with a given size, shape, format?如何创建具有给定大小、形状和格式的现代 openCV 空矩阵? The latest openCV docs on topic don't seem to be helping... I gleaned the formats used above directly from that document. 有关主题的最新 openCV 文档似乎没有帮助...我直接从该文档中收集了上面使用的格式。 Note: I'm assuming OpenCV ( import cv2 ) Python3, import numpy , etc...注意:我假设 OpenCV ( import cv2 ) Python3, import numpy等...

I was looking to create an empty matrix with the intent of copying content from a different buffer into it...我正在寻找创建一个空矩阵,目的是将内容从不同的缓冲区复制到其中...

edit, more failed attempts...编辑,更多失败的尝试......

matOut = cv2.numpy.Mat(height, width, cv2.CV_8UC4)

matOut = numpy.array(height, width, cv2.CV_8UC4)

So user696969 called out a largely successful solution to the question asked.因此,user696969 针对所提出的问题提出了一个非常成功的解决方案。 You create a new shaped area via:您可以通过以下方式创建一个新的形状区域:

matOut = numpy.zeros([height, width, 4], dtype=numpy.uint8)

note I have replaced the desired content, cv2.CV_8UC4 , with its expected response, the number 4 .请注意,我已将所需内容cv2.CV_8UC4替换为其预期响应,即数字4 There are 4 eight bit bytes in a simple RGBA pixel descriptor.一个简单的 RGBA 像素描述符中有 4 个八位字节。 I would have preferred if OpenCV tooling had performed that response as a function call, but that didn't seem to work...如果 OpenCV 工具已将响应作为 function 调用执行,我会更喜欢,但这似乎不起作用......

I do want to share my use case.我确实想分享我的用例。 I originally was going to create an empty shaped matrix so I could transfer data from a single dimensional array there.我原本打算创建一个空的形状矩阵,这样我就可以从那里的一维数组中传输数据。 As I worked this problem I realized there was a better way.当我解决这个问题时,我意识到有更好的方法。 I start the routine where I have received a file containing 8 bit RGBA data without any prefix metadata.我开始例程,其中我收到了一个包含 8 位 RGBA 数据的文件,没有任何前缀元数据。 Think raw BMP without any header info.想想没有任何 header 信息的原始 BMP。

matContent = numpy.frombuffer(fileContent, numpy.uint8) 
matContentReshaped = matContent.reshape(height, width, 4)
cv2.imshow("Display Window", matContentReshaped)
k = cv2.waitKey(0)

Thats it.而已。 Easy, Peasy.... Thanks to user696969 and eldesgraciado for help here.简单,Peasy....感谢 user696969 和 eldesgraciado 在这里提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM