convert char** to cv::Mat

asked 2016-02-07 06:16:38 -0500

Bilityuk gravatar image

updated 2016-02-07 06:26:48 -0500

Hi to everyone! I am trying to convert raw image data from my IP camera to cv::Mat. But can't do it properly. My IP camera use it's own library to retrieve images. This function looks like

GetVideoData(hwnd, (char**)&Frame, &dataLen, videofmt,&mediaSpeed);

Frame has a data buffer of type char data[0] in it's struct; Can anybody help me to find out how to convert Frame->data to cv::Mat?

I tried to do it like:

cv::Mat cameraFrame = cv::Mat(h, w, CV_8UC1, Frame->data);

But when i am trying to show it with imshow("name",cameraFrame); it looks like gray and very pure image.. C:\fakepath\Screen.JPG

edit retag flag offensive close merge delete

Comments

what is Frame , exactly ? please show us the declaration. same with GetVideoData()

berak gravatar imageberak ( 2016-02-07 06:24:04 -0500 )edit

Here is the declaration of GetVideoData function:

/*
@Name: GetVideoData.
@Description: Get the data which decoded.
@Param handle: the handle of current connection information.
@Param data: Buffer,use to save data which get.
@Param len: Size of buf.
@Param outLen: The size of the data which get.
@Param videoFmt: Video format.
@Param mediaSpeed: The speed of the media data which get(bytes/s).  
*/
GetVideoData(CONNECTION handle, char **data, int *outLen, FOSDECFMT videoFmt, int* mediaSpeed);
Bilityuk gravatar imageBilityuk ( 2016-02-07 06:37:00 -0500 )edit

Here is struct of the Frame datatype:

typedef struct   
{ 
    MEDIATYPE  type; //Is video or audio
    DECFMT  fmt; //The format of video or audio.
    int  isKey;     
    int  frameTag;  
    union{ 
        VIDEO_INFO      video; //Media type is video.
        AUDIO_INFO      audio; //Media type is audio.
    } media;
    unsigned long long  pts; //Pts.
    unsigned int len;    //Size of data.
    char    data[0];    //Just data.
}VIDEO_DATA;
Bilityuk gravatar imageBilityuk ( 2016-02-07 06:40:23 -0500 )edit

I need only Frame->data. This is the data of the image from IP camera stream. Finally i need to convert it to cv::Mat, make facerecognition processing, and convert the image back to char* data, that i can draw it with IP camera function

DrawVideoIamge(char *Videodata, int Total_ch, int ch);
Bilityuk gravatar imageBilityuk ( 2016-02-07 06:46:01 -0500 )edit

Also i tried to use another way of convertion, and it behaves different. Here i convert the Frame->data to cv:Mat:

Mat cameraFrame;
Mat InImg(h, w,CV_8UC1);  
memcpy(InImg.data,Frame->data, sizeof(char)*w*h);       
InImg.copyTo(cameraFrame);

make some face recognition processing with cameraFrame and finally trying to draw the image this way:

DrawVideoIamge((char*)cameraFrame.data, 1, 0);

And now i got the color image, but it seems like it mirrored or i dont know how to explaine. Here is a link i upload the screen shot.

Image and video hosting by TinyPic</a">link text

Bilityuk gravatar imageBilityuk ( 2016-02-07 06:54:54 -0500 )edit

If i use imshow() instead of DrawVideoIamge(). I've got the gray image like on the previos screen i posted.

Bilityuk gravatar imageBilityuk ( 2016-02-07 07:09:18 -0500 )edit

try cv::Mat cameraFrame = cv::Mat(h, w, CV_8UC3, Frame->data);

sturkmen gravatar imagesturkmen ( 2016-02-07 07:30:56 -0500 )edit

CV_8UC3 doesn't work for this code(throws c000005):

cv::Mat cameraFrame = cv::Mat(h, w, CV_8UC3, Frame->data);

If use CV_8UC3 with this code:

Mat InImg(h, w,CV_8UC3);  
memcpy(InImg.data,pFrame->data, sizeof(char)*w*h);  
InImg.copyTo(cameraFrame);

Results the almost the same as for CV_8UC1. But now it lookes like triple image.

Here is CV_8UC3 with imshow():

Image and video hosting by TinyPic

Here is CV_8UC3 with DrawVideoIamge():

Image and video hosting by TinyPic

Bilityuk gravatar imageBilityuk ( 2016-02-07 08:03:36 -0500 )edit

try

Mat cameraFrame;
Mat InImg(h, w,CV_8UC3);  
memcpy(InImg.data,Frame->data, 3*w*h);       
InImg.copyTo(cameraFrame);
sturkmen gravatar imagesturkmen ( 2016-02-07 08:11:41 -0500 )edit

Just tried:

Mat cameraFrame;
Mat InImg(h, w,CV_8UC3);  
memcpy(InImg.data,Frame->data, 3*w*h);       
InImg.copyTo(cameraFrame);

It throws an exception on the memcpy line of code:

Image and video hosting by TinyPic

Bilityuk gravatar imageBilityuk ( 2016-02-07 08:42:02 -0500 )edit