3

I am new to C++, OpenCV and to the concept of pointers, so I am sorry for any trivial mistakes on my side.

I am trying to get images from an IDS camera via the manufacturers SDK (called uEye) and further process them as fast as possible via OpenCV. I am able to allocate memory, load the image from the camera and save it as a file (all of this via the SDK functions; I save the image just for debugging purposes). But instead of saving it, I want to give it to my OpenCV-pipeline as a Mat type. Now I have no idea how to do this conversion. I did some googling, but couldn't find a hint.

Here is my code:

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));

I hope this snipped from my C++ class is not too confusing.

It would be great if anyone had a hint on how to convert the image from my buffer into a OpenCV Mat type. Is there a way to do this without actually copying the bytes in the ram to be as fast as possible? What I am also wondering is if there is a problem with giving the pointer from my class above as a return value to my main program (I just heard somewhere that returning pointers may cause problems)?

Thanks a lot in advance!

edit: Thanks to Yunus Temurlenk's approach I could create the Mat. This is the method in my class to create the 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;
};

This is how I call the method to display the Mat file in a window:

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

edit: wit works now, I added the propper definition for the Mat.

Thanks!

GreenCoder
  • 53
  • 1
  • 6
  • Did you check [this one](https://github.com/StevenPuttemans/tryout_code_snippets/blob/master/camera_interfacing/IDS_uEye_camera.cpp). It has a function as "getFrame" which converts IDS frame to OpenCV Mat format. Also you can check [here](https://github.com/jmlowenthal/idslib/blob/master/IDSVideoCapture.cpp) – Yunus Temurlenk Jan 17 '20 at 09:20
  • This seems to be exactly what I needed, thanks a lot Yunus! – GreenCoder Jan 17 '20 at 09:25
  • @YunusTemurlenk Creating the Mat works so far, but I can not display the image in the main program that gets the Mat as a return variable from my class (see my edit). Do you have an idea what I might do wrong here? – GreenCoder Jan 17 '20 at 10:10
  • You are sending an undefined Mat to Capture funtion. Please check the links more carefully. Or ur camera software may already have an example about it. – Yunus Temurlenk Jan 17 '20 at 11:16
  • Thank you, you are right. I edited the code above, – GreenCoder Jan 17 '20 at 11:34

0 Answers0