eps*_*nes 6 python java opencv mat
我习惯了Java的OpenCV实现。我想创建一个Mat
结构,将数据填充到其中,提取 asubmat
然后应用一些图像转换。在Java中,我使用:
my_mat = new Mat(my_rows, my_cols, CvType.CV_8U);
my_mat.put(0, 0, my_data);
my_mat.submat(0, my_other_rows, 0, my_other_cols);
Run Code Online (Sandbox Code Playgroud)
但我没有发现任何可以在 python 的 OpenCV 中工作的东西。我在 OpenCV 论坛上找到了这个链接:cv2.CreateMat in python,但链接已损坏
尽管这个问题是很久以前提出的,但这应该对今天寻找答案的人有所帮助。
这应该适用于 opencv 版本 >= 2.X。在 python 中,opencv 图像现在表示为 numpy 数组。因此,Mat
可以简单地创建一个对象,如下所示:
cvImg = np.zeros((rows, columns, channels), dtype = "uint8")
Run Code Online (Sandbox Code Playgroud)
对于 OpenCV 1.x:
\n\n您可以使用CreateMat来做到这一点:
\n\n\n\n\n创建矩阵头并分配矩阵数据。
\n
Python: cv.CreateMat(rows, cols, type) \xe2\x86\x92 mat\n Parameters: \n rows \xe2\x80\x93 Number of rows in the matrix\n cols \xe2\x80\x93 Number of columns in the matrix\n type \xe2\x80\x93 The type of the matrix elements in the form CV_<bit depth><S|U|F>C<number of channels> , where S=signed, U=unsigned, F=float. For example, CV _ 8UC1 means the elements are 8-bit unsigned and the there is 1 channel, and CV _ 32SC2 means the elements are 32-bit signed and there are 2 channels.\n
Run Code Online (Sandbox Code Playgroud)\n\n该函数调用相当于以下代码:
\n\nCvMat* mat = cvCreateMatHeader(rows, cols, type);\ncvCreateData(mat);\n
Run Code Online (Sandbox Code Playgroud)\n\n对于 cv2 接口:
\n\nPython 的新 cv2 接口集成了numpy数组集成到 OpenCV 框架中,这使得操作更加简单,因为它们用简单的多维数组表示。\n这里是一个起始示例:
\n\nimport numpy as np, cv\nvis = np.zeros((384, 836), np.float32)\nh,w = vis.shape\nvis2 = cv.CreateMat(h, w, cv.CV_32FC3)\nvis0 = cv.fromarray(vis)\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
64114 次 |
最近记录: |