项目用到DirectShow来捕获一帧一帧的画像用来做后续的处理,譬如画像认识等。
其中想获取不同格式和不同大小的图像作处理,下面就说说在DirectShow中怎样修改捕获的视频格式。
在DirectSDK的下面目录里 DXSDK/Samples/C++/DirectShow/Capture/AMCap找到这样一段代码:
//可以设置输出视频的格式
IAMStreamConfig *pSC;
hr = gcap.pBuilder->FindInterface(&PIN_CATEGORY_CAPTURE,
&MEDIATYPE_Interleaved, gcap.pVCap,
IID_IAMStreamConfig, (void **)&pSC);
if(hr != NOERROR)
hr = gcap.pBuilder->FindInterface(&PIN_CATEGORY_CAPTURE,
&MEDIATYPE_Video, gcap.pVCap,
IID_IAMStreamConfig, (void **)&pSC);
ISpecifyPropertyPages *pSpec;
CAUUID cauuid;
hr = pSC->QueryInterface(IID_ISpecifyPropertyPages,(void **)&pSpec);
if(hr == S_OK)
{
hr = pSpec->GetPages(&cauuid);
hr = OleCreatePropertyFrame(ghwndApp, 30, 30, NULL, 1,
(IUnknown **)&pSC, cauuid.cElems,
(GUID *)cauuid.pElems, 0, 0, NULL);
// !!! What if changing output formats couldn't reconnect
// and the graph is broken? Shouldn't be possible...
if(gcap.pVSC)
{
AM_MEDIA_TYPE *pmt;
// get format being used NOW
hr = gcap.pVSC->GetFormat(&pmt);
// DV capture does not use a VIDEOINFOHEADER
if(hr == NOERROR)
{
if(pmt->formattype == FORMAT_VideoInfo)
{
// resize our window to the new capture size
DeleteMediaType(pmt);
}
}
CoTaskMemFree(cauuid.pElems);
pSpec->Release();
}
pSC->Release();
上面的代码先把IBaseFilter的输出IPin的信息和IAMStreamConfig关联起来,然后通过OleCreatePropertyFrame来生成输出IPin的属性页,通过修改这些属性,从而达到修改捕获的视频格式。
需要注意的是在执行上述代码之前,要确保所有的Filter没有连接,并且Filter没有加入到IGraphBuilder中,否则在属性页中会告诉你不支持之类的信息。
在DirectX的文档中还提到的另外一种方法来修改:
首先把IBaseFilter的属性读到IAMStreamConfig中。
IAMStreamConfig *pConfig = NULL;
hr = pBuild->FindInterface(
&PIN_CATEGORY_PREVIEW, // Preview pin.
0, // Any media type.
pCap, // Pointer to the capture filter.
IID_IAMStreamConfig, (void**)&pConfig);
遍历输出IPin中的所有AM_MEDIA_TYPE类型。
int iCount = 0, iSize = 0;
hr = pConfig->GetNumberOfCapabilities(&iCount, &iSize);
// Check the size to make sure we pass in the correct structure.
if (iSize == sizeof(VIDEO_STREAM_CONFIG_CAPS)
{
// Use the video capabilities structure.
for (int iFormat = 0; iFormat < iCount; iFormat++)
{
VIDEO_STREAM_CONFIG_CAPS scc;
AM_MEDIA_TYPE *pmtConfig;
hr = pConfig->GetStreamCaps(iFormat, &pmtConfig, (BYTE*)&scc);
if (SUCCEEDED(hr))
{
/* Examine the format, and possibly use it. */
// Delete the media type when you are done.
DeleteMediaType(pmtConfig);
}
}
在众多支持的AM_MEDIA_TYPE中挑选一种合适的,设置。
if ((pmtConfig.majortype == MEDIATYPE_Video) &
(pmtConfig.subtype == MEDIASUBTYPE_RGB24) &
(pmtConfig.formattype == FORMAT_VideoInfo) &
(pmtConfig.cbFormat >= sizeof (VIDEOINFOHEADER)) &
(pmtConfig.pbFormat != NULL))
{
VIDEOINFOHEADER *pVih = (VIDEOINFOHEADER*)pmtConfig.pbFormat;
// pVih contains the detailed format information.
LONG lWidth = pVih->bmiHeader.biWidth;
LONG lHeight = pVih->bmiHeader.biHeight;
}
在第二种方法中同样需要注意前面方法中所提到的需要注意的地方。
希望对大家有用。