📄 camshiftf.cpp
字号:
CvPoint p[4];
p[0].x = p[1].x = i*size.width/(2*dims);
p[2].x = p[3].x = (i+1)*size.width/(2*dims);
p[1].y = p[2].y = 0;
p[0].y = p[3].y = (val*size.height)/(3*255);
cvFillConvexPoly( image, p, 4, CV_RGB(255,0,0));
}
}
}
void CCamShiftF::DrawCross( CvImage* image )
{
float cs = (float)cos( m_cCamShift.get_orientation() );
float sn = (float)sin( m_cCamShift.get_orientation() );
int x = m_object.x + m_object.width / 2;
int y = m_object.y + m_object.height / 2;
CvPoint p1 = {(int)(x + m_cCamShift.get_length() * cs / 2),
(int)(y + m_cCamShift.get_length() * sn / 2)};
CvPoint p2 = {(int)(x - m_cCamShift.get_length() * cs / 2),
(int)(y - m_cCamShift.get_length() * sn / 2)};
CvPoint p3 = {(int)(x + m_cCamShift.get_width() * sn / 2),
(int)(y - m_cCamShift.get_width() * cs / 2)};
CvPoint p4 = {(int)(x - m_cCamShift.get_width() * sn / 2),
(int)(y + m_cCamShift.get_width() * cs / 2)};
cvLine( image, p1, p2, CV_RGB(255,255,255) );
cvLine( image, p4, p3, CV_RGB(255,255,255) );
}
//
// Transform
//
// 'In place' adjust the CamShift of this sample
//
HRESULT CCamShiftF::Transform(IMediaSample *pMediaSample)
{
BYTE* pData;
CvImage image;
pMediaSample->GetPointer(&pData);
AM_MEDIA_TYPE* pType = &m_pInput->CurrentMediaType();
VIDEOINFOHEADER *pvi = (VIDEOINFOHEADER *) pType->pbFormat;
// Get the image properties from the BITMAPINFOHEADER
CvSize size = cvSize( pvi->bmiHeader.biWidth, pvi->bmiHeader.biHeight );
int stride = (size.width * 3 + 3) & -4;
cvInitImageHeader( &image, size, IPL_DEPTH_8U, 3, IPL_ORIGIN_TL, 4, 1 );
cvSetImageData( &image, pData,stride );
if(IsTracking == false)
{
if(IsInit == false)
{
CvPoint p1, p2;
// Draw box
p1.x = cvRound( size.width * m_params.x );
p1.y = cvRound( size.height * m_params.y );
p2.x = cvRound( size.width * (m_params.x + m_params.width));
p2.y = cvRound( size.height * (m_params.y + m_params.height));
CheckBackProject( &image );
cvRectangle( &image, p1, p2, -1, 1 );
}
else
{
m_object.x = cvRound( size.width * m_params.x );
m_object.y = cvRound( size.height * m_params.y );
m_object.width = cvRound( size.width * m_params.width );
m_object.height = cvRound( size.height * m_params.height );
ApplyCamShift( &image, true );
CheckBackProject( &image );
IsTracking = true;
}
}
else
{
ApplyCamShift( &image, false );
CheckBackProject( &image );
DrawCross( &image );
}
cvSetImageData( &image, 0, 0 );
return NOERROR;
} // Transform
//
// CheckInputType
//
// Check the input type is OK, return an error otherwise
//
HRESULT CCamShiftF::CheckInputType(const CMediaType *mtIn)
{
// Check this is a VIDEOINFO type
if (*mtIn->FormatType() != FORMAT_VideoInfo) {
return E_INVALIDARG;
}
// Is this a palettised format
if (CanChangeCamShiftLevel(mtIn)) {
return NOERROR;
}
return E_FAIL;
} // CheckInputType
//
// CheckTransform
//
// To be able to transform the formats must be identical
//
HRESULT CCamShiftF::CheckTransform(const CMediaType *mtIn,const CMediaType *mtOut)
{
HRESULT hr;
if (FAILED(hr = CheckInputType(mtIn))) {
return hr;
}
// format must be a VIDEOINFOHEADER
if (*mtOut->FormatType() != FORMAT_VideoInfo) {
return E_INVALIDARG;
}
// formats must be big enough
if (mtIn->FormatLength() < sizeof(VIDEOINFOHEADER) ||
mtOut->FormatLength() < sizeof(VIDEOINFOHEADER))
return E_INVALIDARG;
VIDEOINFO *pInput = (VIDEOINFO *) mtIn->Format();
VIDEOINFO *pOutput = (VIDEOINFO *) mtOut->Format();
if (memcmp(&pInput->bmiHeader,&pOutput->bmiHeader,sizeof(BITMAPINFOHEADER)) == 0) {
return NOERROR;
}
return E_INVALIDARG;
} // CheckTransform
//
// DecideBufferSize
//
// Tell the output pin's allocator what size buffers we
// require. Can only do this when the input is connected
//
HRESULT CCamShiftF::DecideBufferSize(IMemAllocator *pAlloc,ALLOCATOR_PROPERTIES *pProperties)
{
// Is the input pin connected
if (m_pInput->IsConnected() == FALSE) {
return E_UNEXPECTED;
}
ASSERT(pAlloc);
ASSERT(pProperties);
HRESULT hr = NOERROR;
pProperties->cBuffers = 1;
pProperties->cbBuffer = m_pInput->CurrentMediaType().GetSampleSize();
ASSERT(pProperties->cbBuffer);
// If we don't have fixed sized samples we must guess some size
if (!m_pInput->CurrentMediaType().bFixedSizeSamples) {
if (pProperties->cbBuffer < 100000) {
// nothing more than a guess!!
pProperties->cbBuffer = 100000;
}
}
// Ask the allocator to reserve us some sample memory, NOTE the function
// can succeed (that is return NOERROR) but still not have allocated the
// memory that we requested, so we must check we got whatever we wanted
ALLOCATOR_PROPERTIES Actual;
hr = pAlloc->SetProperties(pProperties,&Actual);
if (FAILED(hr)) {
return hr;
}
ASSERT( Actual.cBuffers == 1 );
if (pProperties->cBuffers > Actual.cBuffers ||
pProperties->cbBuffer > Actual.cbBuffer) {
return E_FAIL;
}
return NOERROR;
} // DecideBufferSize
//
// GetMediaType
//
// I support one type, namely the type of the input pin
// We must be connected to support the single output type
//
HRESULT CCamShiftF::GetMediaType(int iPosition, CMediaType *pMediaType)
{
// Is the input pin connected
if (m_pInput->IsConnected() == FALSE) {
return E_UNEXPECTED;
}
// This should never happen
if (iPosition < 0) {
return E_INVALIDARG;
}
// Do we have more items to offer
if (iPosition > 0) {
return VFW_S_NO_MORE_ITEMS;
}
*pMediaType = m_pInput->CurrentMediaType();
return NOERROR;
} // GetMediaType
//
// GetPages
//
// This is the sole member of ISpecifyPropertyPages
// Returns the clsid's of the property pages we support
//
STDMETHODIMP CCamShiftF::GetPages(CAUUID *pPages)
{
pPages->cElems = 1;
pPages->pElems = (GUID *) CoTaskMemAlloc(sizeof(GUID));
if (pPages->pElems == NULL) {
return E_OUTOFMEMORY;
}
*(pPages->pElems) = CLSID_CamShiftPropertyPage;
return NOERROR;
} // GetPages
STDMETHODIMP CCamShiftF::GetParams( CvCamShiftParams* params )
{
if( params )
{
*params = m_params;
}
return NOERROR;
}
STDMETHODIMP CCamShiftF::SetParams( CvCamShiftParams* params )
{
CAutoLock cAutoLock(&m_CamShiftLock);
if( params )
{
m_params = *params;
}
return NOERROR;
}
STDMETHODIMP CCamShiftF::StartTracking()
{
CAutoLock cAutoLock(&m_CamShiftLock);
IsInit = true;
IsTracking = false;
return NOERROR;
}
STDMETHODIMP CCamShiftF::StopTracking()
{
CAutoLock cAutoLock(&m_CamShiftLock);
IsInit = IsTracking = false;
return NOERROR;
}
//
// CanChangeCamShiftLevel
//
// Check if this is a paletised format
//
BOOL CCamShiftF::CanChangeCamShiftLevel(const CMediaType *pMediaType) const
{
if ((IsEqualGUID(*pMediaType->Type(), MEDIATYPE_Video))
&& (IsEqualGUID(*pMediaType->Subtype(), MEDIASUBTYPE_RGB24))) {
// I think I can process this format (8 bit palettised)
// So do a quick sanity check on the palette information
VIDEOINFO *pvi = (VIDEOINFO *) pMediaType->Format();
return (pvi->bmiHeader.biBitCount == 24);
} else {
return FALSE;
}
} // CanChangeCamShiftLevel
//
// DllRegisterServer
//
// Handle registration of this filter
//
STDAPI DllRegisterServer()
{
return AMovieDllRegisterServer2( TRUE );
} // DllRegisterServer
//
// DllUnregisterServer
//
STDAPI DllUnregisterServer()
{
return AMovieDllRegisterServer2( FALSE );
} // DllUnregisterServer
/* End of file. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -