convert char** to cv::Mat

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

Bilityuk gravatar image

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

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 -0600 )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 -0600 )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 -0600 )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 -0600 )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 -0600 )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 -0600 )edit

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

sturkmen gravatar imagesturkmen ( 2016-02-07 07:30:56 -0600 )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 -0600 )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 -0600 )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 -0600 )edit

what is dataLen value and w and h value?

GetVideoData(hwnd, (char**)&Frame, &dataLen, videofmt,&mediaSpeed);
sturkmen gravatar imagesturkmen ( 2016-02-07 09:27:38 -0600 )edit
1

I don't think copying to a Mat is your problem, but getting the right data. When I look at the Frame struct, I see that there's more than just the data container. So I think

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

could be the problem. At first I would check (char**)&Frame and try something like &Frame->data

matman gravatar imagematman ( 2016-02-07 10:47:05 -0600 )edit

I want to post the full function code. But there is not enough space for comments. I will post it in slices

Bilityuk gravatar imageBilityuk ( 2016-02-07 10:56:44 -0600 )edit

Here is global variables:

HANDLE hFos = m_h;
DEC_DATA* Frame = NULL;
int dataLen = 0;
DECFMT videofmt = DECTYPE_YUYV422;
DECFMT audiofmt = DECTYPE_PCM;
int w=0, h=0;
int mediaSpeed = 0;
Bilityuk gravatar imageBilityuk ( 2016-02-07 11:01:00 -0600 )edit

Function:

if (GetVideoData(hFos, (char**)&Frame, &dataLen, videofmt,&mediaSpeed) )
{
SetMediaSpeed(mediaSpeed);
if (dataLen)
{
w = pFrame->media.video.picWidth;
h = pFrame->media.video.picHeight;
m_ddraw.CreatEmptySurface(Frame->media.video.picWidth, Frame->media.video.picHeight);
m_ddraw.CreatOverlaySurface(Frame->media.video.picWidth, Frame->media.video.picHeight);
HWND hWnd = GetDlgItem(IDC_STATIC_SCREEN)->GetSafeHwnd();
m_d3d.InitDDraw(hWnd, w, h);
}
Mat InImg(h, w,CV_8UC3);  
memcpy(InImg.data,pFrame->data, sizeof(char)*w*h);          
InImg.copyTo(displayedFrame);

//Face Recogition processing//<<-- process the displayedFrame Mat

//display image//
m_d3d.DrawVideoIamge((char*)displayedFrame.data, 1, 0);
}
Bilityuk gravatar imageBilityuk ( 2016-02-07 11:04:50 -0600 )edit