大家好,我是这方面十足的菜鸟,看了很多文章还是不太懂,我手中有一个翔飞的监控型摄像头,需要用它进行视频采集,此摄像头自带了驱动程序和应用程序,其采集卡有4个端口,进入其自带的应用程序后需要登录然后选择4个连接端口之一的画面,当选中实际所连那个端口的时候才会出现画面;我试着按大家的方法写了个捕捉的vfw的程序,无法读取,认为是自己的水平和理解问题,又下了几个现成的视频捕捉程序,经试验这些程序对于USB借口的webCamaro都能很好的支持,但是连接这个监视器性质的摄像头却显示找不到视频捕捉设备之类的提示,求高手帮忙,最好能把最基础的步骤都说明,我实在是太菜了,想做个自动调焦的系统作为毕业设计,现在连采集和帧存储都谈不到,真是愁死我了
解决方案 »
- 想一个线程产生一些数据放到一个stl的数据结构中,另一个线程读这个stl的数据结构进行一些处理,然后删除已经被处理的数据,这个stl的数据结构用哪个结构好?vector或者list或者其它?
- 关于exit(0)内存存泄露
- 多线程之间控制的问题!
- 我想在重画视图窗口内容时,无效矩形扩大10个单位,在哪设置较好?
- 在线等,马上结贴,大家看看
- 怎样完美的使mdi程序没一份文档有自己的标题
- 协方差矩阵怎么实现???
- 如何编程实现光驱禁用
- 谁有Visual Assist V6.10.3248的注册码
- 初学VC,介绍本入门书籍?
- 继承CListCtrl的ActiveX控件怎么做
- 纠结啊:如何提升进程UAC权限
#include <stdio.h>#include <cv.h>
#include <cxcore.h>
#include <highgui.h>int main( int argc, char** argv )
{
//声明IplImage指针
IplImage* pFrame = NULL;
IplImage* pFrImg = NULL;
IplImage* pBkImg = NULL; CvMat* pFrameMat = NULL;
CvMat* pFrMat = NULL;
CvMat* pBkMat = NULL;
CvCapture* pCapture = NULL;
int nFrmNum = 0; //创建窗口
cvNamedWindow("video", 1);
cvNamedWindow("background",1);
cvNamedWindow("foreground",1);
//使窗口有序排列
cvMoveWindow("video", 30, 0);
cvMoveWindow("background", 360, 0);
cvMoveWindow("foreground", 690, 0); if( argc > 2 )
{
fprintf(stderr, "Usage: bkgrd [video_file_name]\n");
return -1;
} //打开摄像头
if (argc ==1)
if( !(pCapture = cvCaptureFromCAM(-1)))
{
fprintf(stderr, "Can not open camera.\n");
return -2;
} //打开视频文件
if(argc == 2)
if( !(pCapture = cvCaptureFromFile(argv[1])))
{
fprintf(stderr, "Can not open video file %s\n", argv[1]);
return -2;
}
//逐帧读取视频
while(pFrame = cvQueryFrame( pCapture ))
{
nFrmNum++;
//如果是第一帧,需要申请内存,并初始化
if(nFrmNum == 1)
{
pBkImg = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U,1);
pFrImg = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U,1); pBkMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pFrMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pFrameMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1); //转化成单通道图像再处理
cvCvtColor(pFrame, pBkImg, CV_BGR2GRAY);
cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY); cvConvert(pFrImg, pFrameMat);
cvConvert(pFrImg, pFrMat);
cvConvert(pFrImg, pBkMat);
}
else
{
cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);
cvConvert(pFrImg, pFrameMat);
//高斯滤波先,以平滑图像
//cvSmooth(pFrameMat, pFrameMat, CV_GAUSSIAN, 3, 0, 0);
//当前帧跟背景图相减
cvAbsDiff(pFrameMat, pBkMat, pFrMat); //二值化前景图
cvThreshold(pFrMat, pFrImg, 60, 255.0, CV_THRESH_BINARY); //进行形态学滤波,去掉噪音
//cvErode(pFrImg, pFrImg, 0, 1);
//cvDilate(pFrImg, pFrImg, 0, 1); //更新背景
cvRunningAvg(pFrameMat, pBkMat, 0.003, 0);
//将背景转化为图像格式,用以显示
cvConvert(pBkMat, pBkImg); //显示图像
cvShowImage("video", pFrame);
cvShowImage("background", pBkImg);
cvShowImage("foreground", pFrImg); //如果有按键事件,则跳出循环
//此等待也为cvShowImage函数提供时间完成显示
//等待时间可以根据CPU速度调整
if( cvWaitKey(2) >= 0 )
break;
} }
//销毁窗口
cvDestroyWindow("video");
cvDestroyWindow("background");
cvDestroyWindow("foreground"); //释放图像和矩阵
cvReleaseImage(&pFrImg);
cvReleaseImage(&pBkImg); cvReleaseMat(&pFrameMat);
cvReleaseMat(&pFrMat);
cvReleaseMat(&pBkMat); cvReleaseCapture(&pCapture); return 0;
}
#include <vfw.h> // video for windows library
#include <commdlg.h> // common dialogs
#define SELCAPDRVDLG 100
#define SELCAPDRVDLG_LSTBOX 101
#define SELCAPDRVDLG_BUTTON 103
#define EXIT 104
#define HELP 105
#define MINIMIZE 106
#define MOVE 107
#define WndRgn 108
#define BIT_EXIT 109
#define BIT_MAXIMIZE 110
#define BIT_MINIMIZE 111
#define RECORD 112
#define PLAY 113
#define STOP 114
#define BUTTONSIZE 15
#define RECORDVIDEO 115
#define COPY 116
#define SOURCE 117
#define FORMAT 118
#define DISPLAY 119//#include "CapPicture.h" // resource header/********************* Prototypes ***********************/// main window procedure
LRESULT WINAPI MainWndProc( HWND, UINT, WPARAM, LPARAM );
// Select Capture Driver Procedure
LRESULT WINAPI SelCapDrvProc( HWND, UINT, WPARAM, LPARAM );
// Enumerate Capture Drivers
int EnumCapDrv();
// Create the buttons on the main window
//int CreateWndButtons(); this doesnt work
// handle the right click popup menu
VOID APIENTRY HandlePopupMenu(HWND, POINT);
// video thread procedure
DWORD WINAPI videoThreadProc(LPVOID lParam);
/******************* Global Variables ********************/HANDLE ghInstance; // application instance
HWND hwndMain; // main window handle
HWND hwndVideo; // video capture window
HWND hwndSelCapDrvDlg; // Select the capture driver dialog
HWND hwndSelCapDrvDlg_LBox; // list box for select capture driver dialog
HWND hwndExit; // exit button
HWND hwndMin; // minimize button
HWND hwndHelp; // help button
HWND hwndRecord; // record button
HANDLE hVideoThread; // thread to stop the hang when recording video
HRGN hRegion1; // region for window shaping
CAPDRIVERCAPS CapDrvCaps; // driver capabilities
bool isRecordFileOpen = false; // flag set if record file is open
char recordFile[260]; // file to hold recording
bool isPicFileOpen = false; // flag set if snapshot file is open
char pictureFile[260]; // file to hold snapshot
bool isRecording = false; // are we recording?
bool threadEnd = false; // should the video thread end?
/********************************************************************\
* *
* CLASSES, ENUMS, & STRUCTS *
* *
/********************************************************************/
/********************************************************************\
* Function: int PASCAL WinMain(HINSTANCE, HINSTANCE, LPSTR, int) *
* *
* Purpose: Initializes Application ** *
* Comments: Register window class, create and display the main *
* window, and enter message loop. *
* *
* *
\********************************************************************/int PASCAL WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance, LPSTR lpszCmdLine,
int nCmdShow )
{
WNDCLASS wc;
MSG msg; if( !hPrevInstance )
{
wc.lpszClassName = "GenericAppClass";
wc.lpfnWndProc = MainWndProc;
wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = CreateSolidBrush (RGB(0, 64, 128));
wc.lpszMenuName = "GenericAppMenu"; wc.cbClsExtra = 0;
wc.cbWndExtra = 0; RegisterClass( &wc );
} ghInstance = hInstance; hwndMain = CreateWindow( "GenericAppClass",
"Super Video",
WS_POPUP,
0,
0,
500,
500,
NULL,
NULL,
hInstance,
NULL
); ShowWindow( hwndMain, nCmdShow );
//Set the main window to the region
SetWindowRgn(hwndMain,hRegion1,1); while( GetMessage( &msg, NULL, 0, 0 ) ) {
TranslateMessage( &msg ); DispatchMessage( &msg );
} return msg.wParam;
}
* Function: LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM) *
* *
* Purpose: Processes Application Messages *
* *
* Comments: The following messages are processed ** *
* WM_PAINT *
* WM_CREATE *
* WM_DESTROY *
* *
* *
\********************************************************************/LRESULT CALLBACK MainWndProc( HWND hwndMain, UINT msg, WPARAM wParam,
LPARAM lParam )
{
HDC hDC = GetDC(hwndMain);
RECT rc; // client area
POINT pt; // location of mouse click switch( msg ) {/**************************************************************\
* WM_LBUTTONDBLCLK: *
\**************************************************************/ case WM_LBUTTONDBLCLK:
SetFocus(hwndMain);
break;
/**************************************************************\
* WM_LBUTTONDOWN: *
\**************************************************************/ case WM_RBUTTONDOWN:
// Get the bounding rectangle of the client area.
GetClientRect(hwndMain, (LPRECT) &rc);
// Get the client coordinates for the mouse click.
pt.x = LOWORD(lParam); pt.y = HIWORD(lParam);
// If the mouse click took place inside the client
// area, execute the application-defined function
// that displays the shortcut menu.
if (PtInRect((LPRECT) &rc, pt))
HandlePopupMenu(hwndMain, pt);
break;
/**************************************************************\
* WM_PAINT: *
\**************************************************************/ case WM_PAINT:
//Give the region a red border
FrameRgn(hDC,hRegion1,CreateSolidBrush(RGB(0,0,0)),2,2);
// brung our dialog to the foreground
BringWindowToTop(hwndSelCapDrvDlg);
return( DefWindowProc( hwndMain, msg, wParam, lParam ));/**************************************************************\
* WM_COMMAND: *
\**************************************************************/
case WM_COMMAND:
CAPSTATUS CapStatus;
switch( wParam ) {
case SOURCE:
if(CapDrvCaps.fHasDlgVideoSource)
capDlgVideoSource(hwndVideo);
break;
case FORMAT://why doesnt this work
//if(CapDrvCaps.fHasDlgVideoFormat)
//{
capDlgVideoFormat(hwndMain);
// Are there new image dimensions
capGetStatus(hwndVideo, &CapStatus, sizeof(CAPSTATUS));
SetWindowPos(hwndVideo, NULL, 0, 0, CapStatus.uiImageWidth,
CapStatus.uiImageHeight, SWP_NOZORDER | SWP_NOMOVE);
//}
break;
case DISPLAY://why doesnt this work
//if (CapDrvCaps.fHasDlgVideoDisplay)
capDlgVideoDisplay(hwndVideo);
break;
case EXIT:
SendMessage(hwndMain, WM_SYSCOMMAND, SC_CLOSE, 0);
break;
case MINIMIZE:
SendMessage(hwndMain, WM_SYSCOMMAND, SC_MINIMIZE, 0);
break;
case HELP:
SendMessage(hwndMain, WM_SYSCOMMAND, SC_CONTEXTHELP, 0);
break;
case RECORDVIDEO:
if(HIWORD(wParam) == BN_CLICKED && (HWND) lParam == hwndRecord)
{
if (!isRecordFileOpen)
{
OPENFILENAME ofn; // open file name structure
// initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwndMain;
ofn.lpstrFile = recordFile;
ofn.nMaxFile = sizeof(recordFile);
ofn.lpstrFilter = "Video\0*.avi";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
// Display the Save dialog box.
if(GetSaveFileName(&ofn) == TRUE)
{
strcpy(recordFile, ofn.lpstrFile);
strcat(recordFile, ".avi");
isRecordFileOpen = true;
// create the video capture thread
DWORD id;
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
hVideoThread = CreateThread(&sa, (ULONG)0,
videoThreadProc, (LPVOID)(ULONG)0, (ULONG)0, &id);
if(hVideoThread == NULL)
MessageBox(NULL, "Creation of Record Thread failed!", "Thread", MB_OK | MB_ICONEXCLAMATION);
break;
}
} if (isRecordFileOpen) // we already have a file selected
{
if(isRecording) // we're already recording
{
threadEnd = true;
// end the capture and save it
capFileSaveAs(hwndVideo, recordFile);
// make the record button say "Record Video"
SetWindowText(hwndRecord, "Record Video");
isRecording = false;
break;
}
if(!isRecording ) // we're not recording, but a file's selected
{
int a = 0;
MessageBox(hwndMain, "Do you want to write over the open file?",
"File warning", MB_YESNO | MB_ICONWARNING);
if (a != IDYES)
{
isRecordFileOpen = false;
SendMessage(hwndMain, WM_COMMAND, MAKEWPARAM(RECORDVIDEO, BN_CLICKED), (LPARAM) hwndRecord);
}
if (a == IDYES)
{
capCaptureSequence(hwndVideo);
isRecording = true;
}
break;
}
}
}
break;
}
break;
* WM_CREATE: *
\**************************************************************/ case WM_CREATE:
RECT helpRect, minRect, exitRect;
HRGN helpRgn, minRgn, exitRgn;
// make the main region
hRegion1 = CreateRoundRectRgn(0,0,500,400, 200, 200);
// create video capture window
hwndVideo = capCreateCaptureWindow(
(LPSTR) "My Capture Window",
WS_CHILD | WS_VISIBLE,
160, 120, 200, 148,
(HWND) hwndMain,
(int) 1);
// Create the main window's buttons
// create the close button
hwndExit = CreateWindow (
"button", // Builtin button class
"x", // button text
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, // styles
275, 10, BUTTONSIZE, BUTTONSIZE, // position and size
hwndMain, // Parent is main window
(HMENU) EXIT,// Control ID: EXIT
(HINSTANCE)ghInstance,
(LPVOID)NULL);
// create the minimize button
hwndMin = CreateWindow (
"button", // Builtin button class
"-", // button text
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, // styles
250, 10, BUTTONSIZE, BUTTONSIZE, // position and size
hwndMain, // Parent is main window
(HMENU) MINIMIZE,// Control ID: MINIMIZE
(HINSTANCE)ghInstance,
(LPVOID)NULL); // create the help button
hwndHelp = CreateWindow (
"button", // Builtin button class
"?", // button text
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, // styles
225, 10, BUTTONSIZE, BUTTONSIZE, // position and size
hwndMain, // Parent is main window
(HMENU) HELP,// Control ID: HELP
(HINSTANCE)ghInstance,
(LPVOID)NULL);
// create the record video button
hwndRecord = CreateWindow (
"button", // Builtin button class
"Record Video", // button text
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER | BS_VCENTER, // styles
200, 280, 90, 28, // position and size
hwndMain, // Parent is main window
(HMENU) RECORDVIDEO,// Control ID: RECORDVIDEO
(HINSTANCE)ghInstance,
(LPVOID)NULL);
// get the button rectangles
GetClientRect(hwndHelp, &helpRect);
GetClientRect(hwndMin, &minRect);
GetClientRect(hwndExit, &exitRect);
// create the button regions
helpRgn = CreateEllipticRgnIndirect(&helpRect);
minRgn = CreateEllipticRgnIndirect(&minRect);
exitRgn = CreateEllipticRgnIndirect(&exitRect);
//Set the window to the region
SetWindowRgn(hwndExit,exitRgn,1);
SetWindowRgn(hwndMin,minRgn,1);
SetWindowRgn(hwndHelp,helpRgn,1);
// create the SelCapDrv dialog
hwndSelCapDrvDlg = CreateDialog((HINSTANCE)ghInstance,
MAKEINTRESOURCE( SELCAPDRVDLG ),
0, (DLGPROC)SelCapDrvProc);
// get the handle to the list box
hwndSelCapDrvDlg_LBox = GetDlgItem(hwndSelCapDrvDlg,
SELCAPDRVDLG_LSTBOX);
EnumCapDrv();
break;/****************************************************************\
*WM_DESTROY: PostQuitMessage() is called and get rid of vfw stuff*
\****************************************************************/ case WM_DESTROY:
capPreview(hwndVideo, FALSE); // end preview
capDriverDisconnect(hwndVideo); // disconnect from driver
PostQuitMessage( 0 );
break;/**************************************************************\
* Let the default window proc handle all other messages *
\**************************************************************/ default:
return( DefWindowProc( hwndMain, msg, wParam, lParam )); } return 0;
}
WPARAM wParam, LPARAM lParam )
{ switch(msg)
{
// dialog created
case WM_INITDIALOG:
return TRUE;
// command
case WM_COMMAND:
switch ( wParam )
{
// user clicked the select driver button
case SELCAPDRVDLG_BUTTON:
int sel = 0;
// get the selected driver
SendMessage( hwndSelCapDrvDlg_LBox, LB_GETSELITEMS, 1, sel);
// connect to the driver
SendMessage( hwndVideo, WM_CAP_DRIVER_CONNECT, sel, 0L);
// then close this dialog
SendMessage( hwndSelCapDrvDlg, WM_CLOSE, 0, 0);
// update the driver capabilities
SendMessage( hwndVideo, WM_CAP_DRIVER_GET_CAPS,
sizeof(CAPDRIVERCAPS), (LONG) (LPVOID) &CapDrvCaps);
// set preview rate to 66 miliseconds
capPreviewRate( hwndVideo, 66 );
// start preview video
capPreview( hwndVideo, TRUE );
}
return TRUE;
// user wants to close dialog
case WM_CLOSE:
DestroyWindow(hwndSelCapDrvDlg);
return TRUE;
} return( 0L );
}int EnumCapDrv() // enumerate the installed capture drivers
{
char szDeviceName[80]; // driver name
char szDeviceVersion[80]; // driver version
char item[161]; // concatinated string
int i; // counter for (i=0; i<10; i++)
{
if ( capGetDriverDescription(i, szDeviceName, sizeof(szDeviceName),
szDeviceVersion, sizeof(szDeviceVersion)) )
{
strcpy(item, szDeviceName);
strcat(item, " ");
strcat(item, szDeviceVersion);
// add item to list box
SendMessage(hwndSelCapDrvDlg_LBox, LB_ADDSTRING, 0,
(LPARAM) item);
SendMessage(hwndSelCapDrvDlg_LBox, LB_SETITEMDATA, i, (LPARAM) i);
}
}
return 0;
}/*int CreateWndButtons()
{//why doesnt this work
RECT rc; // window rectangle
GetClientRect(hwndMain, &rc);
// create the button
hwndExit = CreateWindow (
"button", // Builtin button class
"X", // button text
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // styles
100, 100, BUTTONSIZE, BUTTONSIZE, // position and size
hwndMain, // Parent is main window
(HMENU) EXIT,// Control ID: EXIT
(HINSTANCE)ghInstance,
(LPVOID)NULL);
return 0;
}
*/
VOID APIENTRY HandlePopupMenu(HWND hwnd, POINT pt)
{
HMENU hmenu; // menu template
HMENU hmenuTrackPopup; // shortcut menu
// Load the menu template containing the shortcut menu from the
// application's resources.
hmenu = LoadMenu((HINSTANCE)ghInstance, "PopupMenu");
if (hmenu == NULL)
return;
// Get the first shortcut menu in the menu template. This is the // menu that TrackPopupMenu displays.
hmenuTrackPopup = GetSubMenu(hmenu, 0);
// TrackPopup uses screen coordinates, so convert the
// coordinates of the mouse click to screen coordinates.
ClientToScreen(hwnd, (LPPOINT) &pt);
// Draw and track the shortcut menu.
TrackPopupMenu(hmenuTrackPopup, TPM_LEFTALIGN | TPM_LEFTBUTTON,
pt.x, pt.y, 0, hwnd, NULL);
// Destroy the menu.
DestroyMenu(hmenu); }DWORD WINAPI videoThreadProc(LPVOID lParam)
{
// make the record button say "Stop Recording"
SetWindowText(hwndRecord, "Stop Recording");
// capture the video
capCaptureSequence(hwndVideo);
isRecording = true;
// don't exit the thread until the record button is pressed again
while (!threadEnd)
;
MessageBox(NULL, "Leaving Thread", "thread", NULL);
return 0;
}