3

我是 C++、OpenCV 和指针概念的新手,所以对于我身边的任何微不足道的错误,我深表歉意。

我正在尝试通过制造商的 SDK(称为 uEye)从 IDS 相机获取图像,并通过 OpenCV 尽可能快地进一步处理它们。我能够分配内存,从相机加载图像并将其保存为文件(所有这些都通过 SDK 功能;我保存图像仅用于调试目的)。但我不想保存它,而是想将它作为 Mat 类型提供给我的 OpenCV 管道。现在我不知道如何进行这种转换。我做了一些谷歌搜索,但找不到提示。

这是我的代码:

int     numcams = 2;                // number of cameras used
char*   pcImageMemory[numcams];     // pointer to buffer
int     iMemID[numcams];            // buffer ID

// capture image with camera 1
int ID = 1;

// use this uEye function to allocate the memory; hCam is a camera handle I generate for every 
// camera; uWidth and uHeight give me the size of the image, uBitspixel gives the number of bits per 
// pixel
is_AllocImageMem(hCam, uWidth, uHeight, uBitspixel, &pcImageMemory[ID], &iMemID[ID]);

// use this uEye function to activate the allocated memory for the next image acquisition
is_SetImageMem(hCam, pcImageMemory[ID], iMemID[ID]);

// use this uEye function to catpure an image
is_FreezeVideo(hCam, IS_WAIT);

// save the image
ImageFileParams.pwchFileName = L"LOCAL PATH";
ImageFileParams.nFileType = IS_IMG_PNG;
ImageFileParams.nQuality = 100;
is_ImageFile(hCam, IS_IMAGE_FILE_CMD_SAVE, (void*)&ImageFileParams, sizeof(ImageFileParams));

我希望从我的 C++ 类中截取的内容不会太混乱。

如果有人提示如何将图像从我的缓冲区转换为 OpenCV Mat 类型,那就太好了。有没有办法在不实际复制内存中的字节以尽可能快的情况下做到这一点?我还想知道的是,将上面的类中的指针作为返回值提供给我的主程序是否存在问题(我刚刚在某处听说返回指针可能会导致问题)?

提前非常感谢!

编辑:感谢 Yunus Temurlenk 的方法,我可以创建垫子。这是我班级中创建 Mat 的方法:

cv::Mat idsWrapper::CaptureMat(INT ID, cv::Mat mat)
{
    //// capture an image
    nRet = is_FreezeVideo(hCam, IS_WAIT); // IS_WAIT IS_DONT_WAIT);
    if (nRet != IS_SUCCESS)
    {
        cout << "ERROR: Image could not be captured for IDS camera with ID " 
        << hCam << ". Error code: " << nRet << endl;
    }

    VOID* pMem_b;
    int retInt = is_GetImageMem(hCam, &pMem_b);
    if (retInt != IS_SUCCESS) {
        cout << "Image data could not be read from memory!" << endl;
    }
    memcpy(mat.ptr(), pMem_b, mat.cols * mat.rows);

    return mat;
};

这就是我调用该方法在窗口中显示 Mat 文件的方式:

...
Mat matIDS1 (height,Swidth, CV_8UC1);
Mat matIDS1 = IDS1.CaptureMat(1, matIDS1);
namedWindow("Display window", WINDOW_AUTOSIZE);
imshow("Display window", matIDS1);
waitKey(10);
...

编辑:现在可以工作了,我为 Mat 添加了 propper 定义。

谢谢!

4

0 回答 0