我将发送一个c++
数组到python函数,numpy array
然后再返回另一个numpy array
.在查阅了numpy
文档和其他一些线程并调整代码后,最后代码正在运行,但我想知道这段代码是否以最佳方式编写,考虑到:
c++
和之间不必要地复制数组numpy (python)
.C++代码:
// python_embed.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "Python.h"
#include "numpy/arrayobject.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Py_SetProgramName(argv[0]);
Py_Initialize();
import_array()
// Build the 2D array
PyObject *pArgs, *pReturn, *pModule, *pFunc;
PyArrayObject *np_ret, *np_arg;
const int SIZE{ 10 };
npy_intp dims[2]{SIZE, SIZE};
const int ND{ 2 }; …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用opencv v 2.1创建一个图像,但是我得到了这个错误:image = cv.CreateImage((w,h),no_of_bits,channels)AttributeError:'module'对象没有属性'CreateImage'
代码是
#!/usr/bin/python
import cv
from opencv import *
from opencv.cv import *
from opencv.highgui import *
import sys
import PIL
w=500
h=500
no_of_bits=8
channels=3
image=cv.CreateImage((w,h),no_of_bits,channels)
cv.ShowImage('WindowName',image)
cvWaitKey()
Run Code Online (Sandbox Code Playgroud) 我习惯了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,但链接已损坏
我正在围绕 ArUco 增强现实库(基于 OpenCV)编写一个薄包装。我正在尝试构建的界面非常简单:
但是,我无法弄清楚如何在 Python 中表示图像以将其传递给 C++。对于 GUI 和相机管理,我将使用 PyQt,所以最初它将是 QImage,但我不能简单地将它传递给 OpenCV(或者我可以?)。一开始,我尝试使用嵌套元组来表示每个像素的行、列和颜色,所以我最终得到了这个示例代码:
using namespace cv;
namespace py = boost::python;
void display(py::tuple pix)
{
/*
Receive image from Python and display it.
*/
Mat img(py::len(pix), py::len(pix[0]), CV_8UC3, Scalar(0, 0, 255));
for (int y = 0; y < py::len(pix); y++)
for (int x = 0; x < py::len(pix[y]); x++)
{
Vec3b rgb;
for (int i = 0; i < 3; i++)
rgb[i] = py::extract<int>(pix[y][x][i]); …
Run Code Online (Sandbox Code Playgroud)