📄 图像特征跟踪系统.cpp
字号:
// 图像特征跟踪系统.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "图像特征跟踪系统.h"
#include "TrackerParameters.h"
#include "MainFrm.h"
#include "ChildFrm.h"
#include "图像特征跟踪系统Doc.h"
#include "图像特征跟踪系统View.h"
#include "Vfw.h"
#include "ZXDib.h"
#include "DIPSLib.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyApp
BEGIN_MESSAGE_MAP(CMyApp, CWinApp)
//{{AFX_MSG_MAP(CMyApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyApp construction
CMyApp::CMyApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CMyApp object
CMyApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CMyApp initialization
BOOL CMyApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_TASKTYPE,
RUNTIME_CLASS(CMyDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CMyView));
AddDocTemplate(pDocTemplate);
// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
m_pMainWnd = pMainFrame;
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The main window has been initialized, so show and update it.
pMainFrame->ShowWindow(SW_SHOWMAXIMIZED);
pMainFrame->UpdateWindow();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
// No message handlers
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// App command to run the dialog
void CMyApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CMyApp message handlers
/////////////////////////////////////////////////////////////////////////////
// 定义全局变量:
//=============图像处理通用类:====================
CZXDib dib;
//==============当前视类指针:======================
CMyView *pCurrentView=NULL;
//=========和摄像机有关的:===========================================================================
HWND ghWndCap; //捕获窗口句柄
CAPDRIVERCAPS gCapDriverCaps;//摄像机驱动状态结构
CAPSTATUS gCapStatus; //摄像机状态
bool bDispatchMouseMessage=true;//将鼠标消息发送出去
bool bTrackBegin = false;
CPoint arrpointTrackWindow[4]={(0,0),(0,0),(0,0),(0,0)};//在捕获窗口内画四边形框
bool bBufferNotReady=true;
bool bAdjust = false;
//定义了一些回调函数:
LRESULT CALLBACK ErrorCallbackProc(HWND hWnd,int nErrID,LPSTR lpErrorText)
{
if(!ghWndCap) return false;
if(nErrID==0) return true;
MessageBox(hWnd,lpErrorText,CAPTION,MB_OK|MB_ICONEXCLAMATION);
return (LRESULT) true;
}
LRESULT CALLBACK StatusCallbackProc(HWND hWnd,int nID,LPSTR lpStatusText)
{
if(!ghWndCap) return false;
capGetStatus(ghWndCap,&gCapStatus,sizeof(CAPSTATUS));
//设置相对位置
SetWindowPos(ghWndCap,NULL,0,0,gCapStatus.uiImageWidth,gCapStatus.uiImageHeight,SWP_NOZORDER|SWP_NOMOVE|SWP_SHOWWINDOW);//
if(nID==0)
{
SetWindowText(ghWndCap,(LPSTR)CAPTION);
return (LRESULT) true;
}
SetWindowText(ghWndCap,(LPSTR) CAPTION);
return (LRESULT) true;
}
static CString strFileName = "e:\\000000.bmp";
//在每次显示新的帧之前,都要执行下面的函数:
LRESULT CALLBACK FrameCallbackProc(HWND hWnd,LPVIDEOHDR lpVHdr)
{
if(!ghWndCap)
return false;
if(!pCurrentView) return (LRESULT) true;
RGBQUAD rgb; //设置红色:
rgb.rgbBlue = 0; rgb.rgbRed = 255; rgb.rgbGreen = 0;
if(!bAdjust) return (LRESULT) true;
if(bTrackBegin || bBufferNotReady && pCurrentView->pBitmapInput)
{
DWORD dwHeight = gCapStatus.uiImageHeight;
DWORD dwWidth = gCapStatus.uiImageWidth;
double* pBitmap = pCurrentView->pBitmapInput;
DWORD i,j;
double temp;
for(i=0;i<dwHeight;i++)
for(j=0;j<dwWidth;j++)
{
temp = lpVHdr->lpData[(i*dwWidth+j)*3];
temp +=lpVHdr->lpData[(i*dwWidth+j)*3+1];
temp +=lpVHdr->lpData[(i*dwWidth+j)*3+2];
pBitmap[(dwHeight-1-i)*dwWidth+j] = temp/3;
}
pCurrentView->Track();
}
bBufferNotReady = false;
//画框:
dib.DrawLineInTheRGBMatric(arrpointTrackWindow[0].x,arrpointTrackWindow[0].y,arrpointTrackWindow[1].x,arrpointTrackWindow[1].y,lpVHdr->lpData,gCapStatus.uiImageHeight,gCapStatus.uiImageWidth,rgb);
dib.DrawLineInTheRGBMatric(arrpointTrackWindow[1].x,arrpointTrackWindow[1].y,arrpointTrackWindow[2].x,arrpointTrackWindow[2].y,lpVHdr->lpData,gCapStatus.uiImageHeight,gCapStatus.uiImageWidth,rgb);
dib.DrawLineInTheRGBMatric(arrpointTrackWindow[2].x,arrpointTrackWindow[2].y,arrpointTrackWindow[3].x,arrpointTrackWindow[3].y,lpVHdr->lpData,gCapStatus.uiImageHeight,gCapStatus.uiImageWidth,rgb);
dib.DrawLineInTheRGBMatric(arrpointTrackWindow[0].x,arrpointTrackWindow[0].y,arrpointTrackWindow[3].x,arrpointTrackWindow[3].y,lpVHdr->lpData,gCapStatus.uiImageHeight,gCapStatus.uiImageWidth,rgb);
/*
BYTE* pTemp = new BYTE[gCapStatus.uiImageHeight*gCapStatus.uiImageWidth*3];
for(DWORD i=0;i<gCapStatus.uiImageHeight;i++)
for(DWORD j=0;j<gCapStatus.uiImageWidth*3;j++)
pTemp[(gCapStatus.uiImageHeight-1-i)*(gCapStatus.uiImageWidth*3)+j] = lpVHdr->lpData[i*(gCapStatus.uiImageWidth*3)+j];
dib.WriteBMPFileFromVector(strFileName,pTemp,gCapStatus.uiImageHeight,gCapStatus.uiImageWidth,24);
dib.FindNextFileName(strFileName,1);
delete pTemp;
*/
return (LRESULT) true;
}
void AdjustCamera()
{
bAdjust = false;
if(gCapDriverCaps.fHasDlgVideoSource)
capDlgVideoSource(ghWndCap);
if(gCapDriverCaps.fHasDlgVideoFormat)
capDlgVideoFormat(ghWndCap);
if(gCapDriverCaps.fHasDlgVideoDisplay)
capDlgVideoDisplay(ghWndCap);
bAdjust = true;
}
/*
LRESULT CALLBACK VideoStreamCallbackProc(HWND hWnd,LPVIDEOHDR lpVHdr)
{
if(!ghWndCap)
return false;
ReleaseCapture();
return (LRESULT) true;
}
*/
//打开摄像头
bool OpenCamera(RECT rectCaptureWindow)
{
bAdjust = true;//false;
ghWndCap = capCreateCaptureWindow((LPCTSTR)CAPTION,WS_CHILD|WS_VISIBLE,rectCaptureWindow.left,rectCaptureWindow.top,rectCaptureWindow.right,rectCaptureWindow.bottom,(HWND)pCurrentView->GetSafeHwnd(),(int)0);//
if(!ghWndCap) return false;
if(!capSetCallbackOnError(ghWndCap,(FARPROC)ErrorCallbackProc)) return false;
if(!capSetCallbackOnStatus(ghWndCap,(FARPROC)StatusCallbackProc)) return false;
if(!capSetCallbackOnFrame(ghWndCap,(FARPROC)FrameCallbackProc)) return false;
// capSetCallbackOnVideoStream(ghWndCap,(FARPROC)VideoStreamCallbackProc);
if(!capDriverConnect(ghWndCap,1) && !capDriverConnect(ghWndCap,0)) return false;
if(!capDriverGetCaps(ghWndCap,&gCapDriverCaps,sizeof(CAPDRIVERCAPS))) return false;
if(!capPreviewRate(ghWndCap,1)) return false;
if(!capPreview(ghWndCap,TRUE)) return false;
// AdjustCamera();
//设置钩子
SetDIPSHook(ghWndCap,(HWND)pCurrentView->GetSafeHwnd());
return true;
}
//关闭摄像头
void CloseCamera()
{
//释放钩子
SetDIPSHook(0,0);
capSetCallbackOnStatus(ghWndCap,NULL); //取消登记的三个回调函数
capSetCallbackOnError(ghWndCap,NULL);
capSetCallbackOnFrame(ghWndCap,NULL);
capCaptureAbort(ghWndCap); //停止捕获
capDriverDisconnect(ghWndCap);//将捕获的窗同驱动器断开
DestroyWindow(ghWndCap);
ghWndCap = 0;
}
//======================================================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -