Convert PByte to Mat OpenCV

350 views Asked by At

I am using PS3 camera with opencv it works with PByte data type.Gives PByte as frame result.In SDK samples they use IplImage.I want use Mat instead IplImage.But i cant convert PByte to Mat.How can i convert PByte to Mat ? Example In SDK

     IplImage *pCapImage;
    PBYTE pCapBuffer = NULL;
    cvGetImageRawData(pCapImage, &pCapBuffer);
    while(_running)
        {
            cvGetImageRawData(pCapImage, &pCapBuffer);
            CLEyeCameraGetFrame(_cam, pCapBuffer, (i==0)?2000:0);
}
//Function from SDK

IMPORT(bool) CLEyeCameraGetFrame(CLEyeCameraInstance cam, PBYTE pData, int waitTimeout = 2000);

//MyCode //I use 2 camera.

    cv::Mat pCapImage[2];
    PBYTE pCapBuffer = NULL;
    while(_running){

        for (int i = 0; i < 2; i++)
                {
                                  //trying convert
                    pCapImage[i] = cv::Mat (cvSize(w, h), 4, &pCapBuffer,IPL_DEPTH_8U).clone();

                    CLEyeCameraGetFrame(_cam[i], pCapBuffer);
                }

                Mat pCapImageMatL = pCapImage[0];
                Mat pCapImageMatR = pCapImage[1];
                imshow(_windowNameL, pCapImageMatL);
                imshow(_windowNameR, pCapImageMatR);
}
2

There are 2 answers

2
herohuyongtao On

Given that you have successfully converted PBYTE to IplImage *, you can further convert it to cv::Mat as follows:

IplImage *pCapImage;  // this is the IplImage * that you have converted from PBYTE 
cv::Mat pCapImageMat(pCapImage);
cvReleaseImage(&pCapImage); // free the previous memory
1
gibertoni On

If you are using OpenCV 3, IPlImage has been deprecated. So @herohuyongtao answer won't work.

In this case, the workaround is

cv::Mat frame;
frame = cv::cvarrToMat(pCapImage);