简体   繁体   English

ISampleGrabber :: BufferCB到IplImage; OpenCV中的显示显示乱码图像-C ++

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

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. 我正在使用DirectShow访问视频流,然后使用SampleGrabber过滤器和接口从每个帧中获取样本以进行进一步的图像处理。 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. 我基本上只是在PlayCap示例应用程序中工作过,并向图中添加了示例过滤器。

The problem I'm having is that I'm trying to display the grabbed samples on a different OpenCV window. 我遇到的问题是我试图在不同的OpenCV窗口上显示获取的样本。 However, when I try to cast the information in the buffer to an IplImage, I get a garbled mess of pixels. 但是,当我尝试将缓冲区中的信息转换为IplImage时,会出现乱码的像素。 The code for the BufferCB call is below, sans any proper error handling: BufferCB调用的代码如下,没有适当的错误处理:

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? 我是否缺少标题信息或其他内容?

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. 您在此处创建具有8位像素数据和未知通道/组件数的特定宽度/高度的图像对象。 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. 因此,您基本上需要先检查Sample Grabber的输入引脚上到底是哪种媒体类型。 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. SG的下游连接是什么以及是否特别连接到视频渲染器也可能很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM