cvcap.cpp.svn-base
来自「非结构化路识别」· SVN-BASE 代码 · 共 1,486 行 · 第 1/4 页
SVN-BASE
1,486 行
CvCaptureAVI_VFW* capture = (CvCaptureAVI_VFW*)cvAlloc( sizeof(*capture));
memset( capture, 0, sizeof(*capture));
capture->vtable = &captureAVI_VFW_vtable;
if( !icvOpenAVI_VFW( capture, filename ))
cvReleaseCapture( (CvCapture**)&capture );
return (CvCapture*)capture;
}
/********************* Capturing video from camera via VFW *********************/
typedef struct CvCaptureCAM_VFW
{
CvCaptureVTable* vtable;
CAPDRIVERCAPS caps;
HWND capWnd;
VIDEOHDR* hdr;
DWORD fourcc;
HIC hic;
IplImage* rgb_frame;
IplImage frame;
}
CvCaptureCAM_VFW;
static LRESULT PASCAL
FrameCallbackProc( HWND hWnd, VIDEOHDR* hdr )
{
CvCaptureCAM_VFW* capture = 0;
if (!hWnd) return FALSE;
capture = (CvCaptureCAM_VFW*)capGetUserData(hWnd);
capture->hdr = hdr;
return (LRESULT)TRUE;
}
// Initialize camera input
static int icvOpenCAM_VFW( CvCaptureCAM_VFW* capture, int wIndex )
{
char szDeviceName[80];
char szDeviceVersion[80];
HWND hWndC = 0;
if( (unsigned)wIndex >= 10 )
wIndex = 0;
for( ; wIndex < 10; wIndex++ )
{
if( capGetDriverDescription( wIndex, szDeviceName,
sizeof (szDeviceName), szDeviceVersion,
sizeof (szDeviceVersion)))
{
hWndC = capCreateCaptureWindow ( "My Own Capture Window",
WS_POPUP | WS_CHILD, 0, 0, 320, 240, 0, 0);
if( capDriverConnect (hWndC, wIndex))
break;
DestroyWindow( hWndC );
hWndC = 0;
}
}
if( hWndC )
{
capture->capWnd = hWndC;
capture->hdr = 0;
capture->hic = 0;
capture->fourcc = (DWORD)-1;
capture->rgb_frame = 0;
memset( &capture->caps, 0, sizeof(capture->caps));
capDriverGetCaps( hWndC, &capture->caps, sizeof(&capture->caps));
::MoveWindow( hWndC, 0, 0, 320, 240, TRUE );
capSetUserData( hWndC, (long)capture );
capSetCallbackOnFrame( hWndC, FrameCallbackProc );
CAPTUREPARMS p;
capCaptureGetSetup(hWndC,&p,sizeof(CAPTUREPARMS));
p.dwRequestMicroSecPerFrame = 66667/2;
capCaptureSetSetup(hWndC,&p,sizeof(CAPTUREPARMS));
//capPreview( hWndC, 1 );
capPreviewScale(hWndC,FALSE);
capPreviewRate(hWndC,1);
}
return capture->capWnd != 0;
}
static void icvCloseCAM_VFW( CvCaptureCAM_VFW* capture )
{
if( capture && capture->capWnd )
{
capSetCallbackOnFrame( capture->capWnd, NULL );
capDriverDisconnect( capture->capWnd );
DestroyWindow( capture->capWnd );
cvReleaseImage( &capture->rgb_frame );
if( capture->hic )
{
ICDecompressEnd( capture->hic );
ICClose( capture->hic );
}
capture->capWnd = 0;
capture->hic = 0;
capture->hdr = 0;
capture->fourcc = 0;
capture->rgb_frame = 0;
memset( &capture->frame, 0, sizeof(capture->frame));
}
}
static int icvGrabFrameCAM_VFW( CvCaptureCAM_VFW* capture )
{
if( capture->capWnd )
{
SendMessage( capture->capWnd, WM_CAP_GRAB_FRAME_NOSTOP, 0, 0 );
return 1;
}
return 0;
}
static IplImage* icvRetrieveFrameCAM_VFW( CvCaptureCAM_VFW* capture )
{
if( capture->capWnd )
{
BITMAPINFO vfmt;
memset( &vfmt, 0, sizeof(vfmt));
int sz = capGetVideoFormat( capture->capWnd, &vfmt, sizeof(vfmt));
if( capture->hdr && capture->hdr->lpData && sz != 0 )
{
long code = ICERR_OK;
char* frame_data = (char*)capture->hdr->lpData;
if( vfmt.bmiHeader.biCompression != BI_RGB ||
vfmt.bmiHeader.biBitCount != 24 )
{
BITMAPINFOHEADER& vfmt0 = vfmt.bmiHeader;
BITMAPINFOHEADER vfmt1 = icvBitmapHeader( vfmt0.biWidth, vfmt0.biHeight, 24 );
code = ICERR_ERROR;
if( capture->hic == 0 ||
capture->fourcc != vfmt0.biCompression ||
capture->rgb_frame == 0 ||
vfmt0.biWidth != capture->rgb_frame->width ||
vfmt0.biHeight != capture->rgb_frame->height )
{
if( capture->hic )
{
ICDecompressEnd( capture->hic );
ICClose( capture->hic );
}
capture->hic = ICOpen( MAKEFOURCC('V','I','D','C'),
vfmt0.biCompression, ICMODE_DECOMPRESS );
if( capture->hic &&
ICDecompressBegin( capture->hic, &vfmt0, &vfmt1 ) == ICERR_OK )
{
cvReleaseImage( &capture->rgb_frame );
capture->rgb_frame = cvCreateImage(
cvSize( vfmt0.biWidth, vfmt0.biHeight ), IPL_DEPTH_8U, 3 );
capture->rgb_frame->origin = IPL_ORIGIN_BL;
code = ICDecompress( capture->hic, 0,
&vfmt0, capture->hdr->lpData,
&vfmt1, capture->rgb_frame->imageData );
frame_data = capture->rgb_frame->imageData;
}
}
}
if( code == ICERR_OK )
{
cvInitImageHeader( &capture->frame,
cvSize(vfmt.bmiHeader.biWidth,
vfmt.bmiHeader.biHeight),
IPL_DEPTH_8U, 3, IPL_ORIGIN_BL, 4 );
capture->frame.imageData = capture->frame.imageDataOrigin = frame_data;
return &capture->frame;
}
}
}
return 0;
}
static double icvGetPropertyCAM_VFW( CvCaptureCAM_VFW* capture, int property_id )
{
switch( property_id )
{
case CV_CAP_PROP_FRAME_WIDTH:
return capture->frame.width;
case CV_CAP_PROP_FRAME_HEIGHT:
return capture->frame.height;
case CV_CAP_PROP_FOURCC:
return capture->fourcc;
}
return 0;
}
static CvCaptureVTable captureCAM_VFW_vtable =
{
6,
(CvCaptureCloseFunc)icvCloseCAM_VFW,
(CvCaptureGrabFrameFunc)icvGrabFrameCAM_VFW,
(CvCaptureRetrieveFrameFunc)icvRetrieveFrameCAM_VFW,
(CvCaptureGetPropertyFunc)icvGetPropertyCAM_VFW,
(CvCaptureSetPropertyFunc)0,
(CvCaptureGetDescriptionFunc)0
};
/********************* Capturing video from camera via MIL *********************/
//#ifdef WIN32
/* Small patch to cope with automatically generated Makefiles */
#if !defined _MSC_VER || defined __ICL || defined CV_BATCH_MSVC
#undef USE_MIL
#endif
#ifdef USE_MIL
#include "mil.h" /* to build MIL-enabled version of HighGUI you
should have MIL installed */
struct
{
MIL_ID MilApplication;
int MilUser;
} g_Mil = {0,0}; //global structure for handling MIL application
typedef struct CvCaptureCAM_MIL
{
CvCaptureVTable* vtable;
MIL_ID
MilSystem, /* System identifier. */
MilDisplay, /* Display identifier. */
MilDigitizer, /* Digitizer identifier. */
MilImage; /* Image buffer identifier. */
IplImage* rgb_frame;
}
CvCaptureCAM_MIL;
// Initialize camera input
static int icvOpenCAM_MIL( CvCaptureCAM_MIL* capture, int wIndex )
{
if( g_Mil.MilApplication == M_NULL )
{
assert(g_Mil.MilUser == 0);
MappAlloc(M_DEFAULT, &(g_Mil.MilApplication) );
g_Mil.MilUser = 1;
}
else
{
assert(g_Mil.MilUser>0);
g_Mil.MilUser++;
}
int dev_table[16] = { M_DEV0, M_DEV1, M_DEV2, M_DEV3,
M_DEV4, M_DEV5, M_DEV6, M_DEV7,
M_DEV8, M_DEV9, M_DEV10, M_DEV11,
M_DEV12, M_DEV13, M_DEV14, M_DEV15 };
//set default window size
int w = 320/*160*/;
int h = 240/*120*/;
if( (unsigned)wIndex < 116)
{
wIndex -= 100;
}
if( wIndex < 0 ) wIndex = 0;
for( ; wIndex < 16; wIndex++ )
{
MsysAlloc( M_SYSTEM_SETUP, //we use default system,
//if this does not work
//try to define exact board
//e.g.M_SYSTEM_METEOR,M_SYSTEM_METEOR_II...
dev_table[wIndex],
M_DEFAULT,
&(capture->MilSystem) );
if( capture->MilSystem != M_NULL )
break;
}
if( capture->MilSystem != M_NULL )
{
MdigAlloc(capture->MilSystem,M_DEFAULT,
M_CAMERA_SETUP, //default. May be M_NTSC or other
M_DEFAULT,&(capture->MilDigitizer));
capture->rgb_frame = cvCreateImage(cvSize(w,h), IPL_DEPTH_8U, 3 );
MdigControl(capture->MilDigitizer, M_GRAB_SCALE, 1.0 / 2);
/*below line enables getting image vertical orientation
consistent with VFW but it introduces some image corruption
on MeteorII, so we left the image as is*/
//MdigControl(capture->MilDigitizer, M_GRAB_DIRECTION_Y, M_REVERSE );
capture->MilImage = MbufAllocColor(capture->MilSystem, 3, w, h,
8+M_UNSIGNED,
M_IMAGE + M_GRAB,
M_NULL);
}
return capture->MilSystem != M_NULL;
}
static void icvCloseCAM_MIL( CvCaptureCAM_MIL* capture )
{
if( capture->MilSystem != M_NULL )
{
MdigFree( capture->MilDigitizer );
MbufFree( capture->MilImage );
MsysFree( capture->MilSystem );
cvReleaseImage(&capture->rgb_frame );
capture->rgb_frame = 0;
g_Mil.MilUser--;
if(!g_Mil.MilUser)
MappFree(g_Mil.MilApplication);
capture->MilSystem = M_NULL;
capture->MilDigitizer = M_NULL;
}
}
static int icvGrabFrameCAM_MIL( CvCaptureCAM_MIL* capture )
{
if( capture->MilSystem )
{
MdigGrab(capture->MilDigitizer, capture->MilImage);
return 1;
}
return 0;
}
static IplImage* icvRetrieveFrameCAM_MIL( CvCaptureCAM_MIL* capture )
{
MbufGetColor(capture->MilImage, M_BGR24+M_PACKED, M_ALL_BAND, (void*)(capture->rgb_frame->imageData));
//make image vertical orientation consistent with VFW
//You can find some better way to do this
capture->rgb_frame->origin = IPL_ORIGIN_BL;
cvFlip(capture->rgb_frame,capture->rgb_frame,0);
return capture->rgb_frame;
}
static double icvGetPropertyCAM_MIL( CvCaptureCAM_MIL* capture, int property_id )
{
switch( property_id )
{
case CV_CAP_PROP_FRAME_WIDTH:
if( capture->rgb_frame) return capture->rgb_frame->width;
case CV_CAP_PROP_FRAME_HEIGHT:
if( capture->rgb_frame) return capture->rgb_frame->height;
}
return 0;
}
static CvCaptureVTable captureCAM_MIL_vtable =
{
6,
(CvCaptureCloseFunc)icvCloseCAM_MIL,
(CvCaptureGrabFrameFunc)icvGrabFrameCAM_MIL,
(CvCaptureRetrieveFrameFunc)icvRetrieveFrameCAM_MIL,
(CvCaptureGetPropertyFunc)icvGetPropertyCAM_MIL,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?