ISampleGrabber::BufferCB to IplImage; display in OpenCV shows garbled image - C++

北城余情 提交于 2019-12-24 11:36:30

问题


I'm using DirectShow to access a video stream, and then using the SampleGrabber filter and interface to get samples from each frame for further image processing. I'm using a callback, so it gets called after each new frame. I've basically just worked from the PlayCap sample application and added a sample filter to the graph.

The problem I'm having is that I'm trying to display the grabbed samples on a different OpenCV window. However, when I try to cast the information in the buffer to an IplImage, I get a garbled mess of pixels. The code for the BufferCB call is below, sans any proper error handling:

STDMETHODIMP BufferCB(double Time, BYTE *pBuffer, long BufferLen)
{
    AM_MEDIA_TYPE type;
    g_pGrabber->GetConnectedMediaType(&type);
    VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER *)type.pbFormat;

    BITMAPINFO* bmi = (BITMAPINFO *)&pVih->bmiHeader;
    BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);
    int channels = bmih->biBitCount / 8;

    mih->biPlanes = 1;
    bmih->biBitCount = 24;
    bmih->biCompression = BI_RGB;

    IplImage *Image = cvCreateImage(cvSize(bmih->biWidth, bmih->biHeight), IPL_DEPTH_8U, channels);     

    Image->imageSize = BufferLen;
    CopyMemory(Image->imageData, pBuffer, BufferLen);
    cvFlip(Image);  

    //openCV Mat creation
    Mat cvMat = Mat(Image, true);
    imshow("Display window", cvMat);                   // Show our image inside it.
    waitKey(2);


    return S_OK;
}

My question is, am I doing something wrong here that will make the image displayed look like this:

Am I missing header information or something?


回答1:


The quoted code is a part of the solution. You create here an image object of certain width/height with 8-bit pixel data and unknown channel/component count. Then you copy data from another buffer of unknown format.

The only chance for it to work well is that all unknowns amazingly match without your effort. So you basically need to start with checking what media type is exactly on Sample Grabber's input pin. Then, if it is not what you wanted, you have to update your code respectively. It might also be important what is the downstream connection of the SG, and whether it is connected to video renderer in particular.



来源:https://stackoverflow.com/questions/21902048/isamplegrabberbuffercb-to-iplimage-display-in-opencv-shows-garbled-image-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!