📄 main.c
字号:
/*****************************************************************************
* File: Main.c
* Desc: Main functions of CTracker TestBed
* Author: Xuhui Zhou @ Carnegie Mellon University
* Date: 11/13/2003
*****************************************************************************/
#include <windows.h>
#include <winuser.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <commdlg.h>
#include <time.h>
#include "pxc.h"
#include "resource.h"
#include "math.h"
#include <direct.h>
#include "new.h"
#include "utility.h"
#include "ImageIO.h"
#include "MeanShiftWrap.h"
#include "KltWrap.h"
#include "Histogram.h"
#include "TemplateMatch.h"
#include "Msfeature.h"
#include "Emshift.h"
#include "GraphCutTracker.h"
#include "ParticleFilter.h"
/* this program can use either 24 bit color or 8 bit gray frames.
change the value of PIXEL_TYPE to PBITS_RGB24 or PBITS_Y8 */
#define PBITS_Y8 0x0008
#define PBITS_RGB24 0x0218
#define PIXEL_TYPE PBITS_RGB24
#ifdef _WIN32
#define PXC_NAME "pxc_95.dll"
#define FRAME_NAME "frame_32.dll"
#define PXC_NT "pxc_nt.dll"
#else
#define PXC_NAME "pxc_31.dll"
#define FRAME_NAME "frame_16.dll"
#define PXC_NT "pxc_31.dll"
#endif
#define TEST_TIMER 1
#define ON 1
#define OFF 2
/*-------------------------------------------------------------------------
This button structure is used to create the function buttons as child
windows. There are other ways to do this. We figured this would be the
best way to do it for our example.
-------------------------------------------------------------------------*/
#define NBUTTONS 12
typedef struct tagBUTTON
{
HWND hwnd;
long style;
LPSTR text;
int id;
} BUTTON;
/*-------------------------------------------------------------------------
These values are used to control the processing of the WM_COMMAND message.
-------------------------------------------------------------------------*/
#define ID_FBASE 101
#define ID_TRACKFWD ID_FBASE+0
#define ID_STOP ID_FBASE+1
#define ID_BROWSEBACK ID_FBASE+2
#define ID_STEPFWD ID_FBASE+3
#define ID_REWIND ID_FBASE+4
#define ID_PREDICT ID_FBASE+5
#define ID_READ ID_FBASE+6
#define ID_PLAYLOG ID_FBASE+7
#define ID_KLT ID_FBASE+8
#define ID_LOG ID_FBASE+9
#define ID_ALGORITHM ID_FBASE+10
#define ID_DEBUG ID_FBASE+11
BUTTON Functions[NBUTTONS] =
{
{NULL, BS_PUSHBUTTON, "Open", ID_READ},
{NULL, BS_PUSHBUTTON, "Algorithm", ID_ALGORITHM},
{NULL, BS_PUSHBUTTON, "<<Browse ", ID_BROWSEBACK},
{NULL, BS_PUSHBUTTON, "Track>>", ID_TRACKFWD},
{NULL, BS_PUSHBUTTON, "Step>", ID_STEPFWD},
{NULL, BS_PUSHBUTTON, "Stop", ID_STOP},
{NULL, BS_PUSHBUTTON, "Rewind", ID_REWIND},
{NULL, BS_PUSHBUTTON, "Klt", ID_KLT},
{NULL, BS_PUSHBUTTON, "Log", ID_LOG},
{NULL, BS_PUSHBUTTON, "Rpt", ID_PLAYLOG},
{NULL, BS_PUSHBUTTON, "Predict", ID_PREDICT},
{NULL, BS_PUSHBUTTON, "Debug", ID_DEBUG}
};
//second set on debug win
BUTTON Functions2[NBUTTONS] =
{
{NULL, BS_PUSHBUTTON, " Open ", ID_READ},
{NULL, BS_PUSHBUTTON, " Algorithm ", ID_ALGORITHM},
{NULL, BS_PUSHBUTTON, " <<Browse ", ID_BROWSEBACK},
{NULL, BS_PUSHBUTTON, " Track>> ", ID_TRACKFWD},
{NULL, BS_PUSHBUTTON, " Step> ", ID_STEPFWD},
{NULL, BS_PUSHBUTTON, " Stop ", ID_STOP},
{NULL, BS_PUSHBUTTON, " Rewind ", ID_REWIND},
{NULL, BS_PUSHBUTTON, " Klt ", ID_KLT},
{NULL, BS_PUSHBUTTON, " Log ", ID_LOG},
{NULL, BS_PUSHBUTTON, " Rpt ", ID_PLAYLOG},
{NULL, BS_PUSHBUTTON, " Predict ", ID_PREDICT},
{NULL, BS_PUSHBUTTON, "Debug", ID_DEBUG}
};
static unsigned ButtonTextLen;
static int ButtonHeight, ButtonWidth;
static int ViewOffset;
BOOL AppInit(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw);
LONG WINAPI CtlProc(HWND, UINT, WPARAM, LPARAM);
HWND CreateControlWindow(HINSTANCE hInst, LPSTR szCmdLine, int sw);
void RegisterWindowClasses(HINSTANCE hInst, LPSTR szCmdLine, int sw);
void AppExit(void);
BOOL AppIdle(void);
void AppPaint(HWND hwnd, HDC hdc);
void AppPaint2(HWND hwnd, HDC hdc);
void GetGlobals(HWND hwnd, LPARAM lParam);
int GetImage();
void DrawButtons(HWND hwnd, LPARAM lParam, int draw);
void DrawButtons2(HWND hwnd, LPARAM lParam, int draw);
void SetBitMapHead(void);
int AllocBuffer(void);
void ReleaseBuffer();
void HourGlass(int on);
/* text data for drawing text */
static int cxChar, /* average character width */
cyChar; /* character height */
static HINSTANCE hAppInst;
static HWND hwndCtl;
static HWND hwndCtl2;
static char szAppName[] = "PXCDRAW2" ;
static char szTitleBar[100];
static char szTitle[] = "DHID";
static int iBoardRev;
static char szVideoType[80];
static int videotype;
static int modelnumber;
static int grab_type;
//static BOOL fAcquire;
static int ImageMaxX,
ImageMaxY,
WindowX,
WindowY;
static int StartTest,
FrameCount,
ErrorCount;
/*-------------------------------< Time Data >-------------------------------*/
double bTime, eTime, dTime, iTime;
double bTime2,dTime2;
int FPS;
double dFPS;
/*----------------------------< GDI Functions >------------------------------*/
void CreateGrayPalette(void);
static HPALETTE hpalette;
static HANDLE hBuf;
static BYTE __PX_HW *gpBits;
static HANDLE hBuf2; //for previous image in history
static BYTE __PX_HW *gpBits2;
struct {
BITMAPINFOHEADER head;
RGBQUAD colors[256];
} maphead;
/*-------------------------------< file data >-------------------------------*/
static OPENFILENAME rfn, sfn;
static char szFilterSel[] = "(*.bmp,*.jpg)\0*.bmp;*.jpg\0";
static char tmp[201];
static char *WriteErr = "Stop acquiring an image before writing a file";
static char *ReadErr = "Stop acquiring an image before reading a file";
//-------------------------------<Global Definition >-------------------------------//
void dis_PrintMessage(); //display message as window title
//-------------------------------<Image IO and Sequence playing >-------------------------------//
//variables for sequence playing
IplImage *gcImgCurrent=NULL;
IplImage *gcImgCurrentHalf=NULL;
int giFrameInitial = 1;
int giPlayFlag = 0; //flag to play sequence
//Image IO Related Method
void AppSetDisplay();
int img_StepOneFrame(int nextFrameNo);
void dis_ShowImgCurrent();
void dis_ShowImgDebug();
//-------------------------------<Track Click Initialization>-------------------------------//
#define NUMPOINTS 4
POINT gaClickPoint[NUMPOINTS]; //bmp image corrdinate (bottom-left is 0)
POINT gaClickPointDisplay[NUMPOINTS]; //display coordinate (top-left is 0)
POINT gaClickPointHalf[NUMPOINTS]; //half coordinate (top-left is 0)
POINT gaClickPointHalfDisplay[NUMPOINTS]; //half coordinate (bottom-left is 0)
int giClickCount=0;
BOOL gbClickInitFlag=FALSE;
RECT gRectInit;
RECT gRectCurrent;
RECT gRectCurrentHalf;
//poly region handle
HRGN gRegionInit;
HRGN gRegionCurrent;
HRGN gRegionCurrentHalf;
HRGN gRegionDisplay;
HRGN gRegionDisplayHalf;
//Click related methods
void dis_DrawRect(HWND hwnd, RECT rect);
void img_ProcessLClick_Poly();
void dis_DrawRegion(HWND hwnd, HRGN hRegion);
void img_ProcessMouseMove(long x, long y);
//-------------------------------<MeanShift Tracking>-------------------------------//
int giTrackHalf = 1;
BOOL gbHalfSize = TRUE;
BOOL gbTrackMood = FALSE;
//-------------------------------<KLT Tracking>-------------------------------//
float gfXShift, gfYShift;
int giCountValid, giCountBG;
//-------------------------------<Histogram Projection>-------------------------------//
extern RBINS;
extern GBINS;
extern BBINS;
//-------------------------------<Tracker Report>-------------------------------//
typedef struct {
double time;
char *framePath;
double vx;
double vy;
double aspectAng;
double grazingAng;
double resolution;
int boxX;
int boxY;
int boxWid;
int boxHgt;
int imgWidth;
int imgHeight;
}TrackReport;
TrackReport tReport;
char gLogDir[_MAX_PATH]; //log dir path
char gLogDirName[_MAX_DIR]; //log dir name
char gLogFile[_MAX_PATH];
BOOL gLogDirFlag = FALSE; //build a sub-directory for one tracking click
char gClickFrameTitle[100];
char gLastFramePath[_MAX_PATH]; //record previous frame path
IplImage* gImgBitMap;
char gExePath[_MAX_PATH];
char gExeLogPath[_MAX_PATH];
int gLogVersion=5; //1: CMU-v1.0; 2: CID-v0.9; 3: CID-v1.0; 4:CMU V2.0;; 5: cmu v2.1
//methods
void io_PrintReport(); //version 1: CMU version 1.0
void io_PrintReport_CID09(); //version 2: CID version 0.9
void io_PrintReport_CID10(); //version 3: CID version 1.0
void io_PrintReport_V20(); //version 4: CMU version 2.0
void io_PrintReport_V21(); //version 5: CMU version 2.1
//-------------------------------<Tracker Report>-------------------------------//
BOOL gbReplayFlag=FALSE;
BOOL gbKltFlag = FALSE;
int giSeqType = 0; //0:image; 1: trk
BOOL gbLogFlag = FALSE;
//-------------------------------<Display>-------------------------------//
COLORREF crPenColor = RGB(255,0,0);
int giPenWidth = 3;
//-------------------------------<Prediction>-------------------------------//
#define POSNUM 40
//general type
typedef struct _FPOINT{
float x;
float y;
}
FPOINT;
FPOINT gPosPredict[POSNUM]; //prediction array
POINT gPosDraw[POSNUM]; //flipped prediction array for draw in sreen window
//POINT gPosTarget; //target position
int gPosCount = 0;
int gEffectNum = POSNUM;
float gAvgDx, gAvgDy;
BOOL gbPredictFlag = FALSE;
int gBlobWidthInit, gBlobHeightInit; //initial dimension of tracking blob
int gBlobWidth, gBlobHeight; //blob dimension of previous frame
int gIgnoreFrameNum = 8; //occlusion detection delay, recent number of frame to ignore.
BOOL gbAutoPredictFlag = FALSE;
float gAffineMatrix[6];
float gZoomAccum = 1; //accumulative scale factor
int gTrackCount = 0;
int img_PredictMotion();
void dis_DrawTraceLine(HWND hwnd, FPOINT* pts, int ptCount, int penStyle, int penWidth, COLORREF penColor);
//char sLogDirName[80]; // receives name of log directory.
BOOL CALLBACK dlg_LogDirProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK dlg_DirExistProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam);
BOOL dlg_LogDir();
void io_CalcLogDirName(char* outLogDirName);
//-------------------------------<Batch Process>-------------------------------//
BOOL gBatchFlag;
extern int gBtkFileCount;
extern char gBtkFileArray[100][MAX_PATH];
extern int gBtkStepNum[100];
void BatchProcess();
char gLogPathBackup[MAX_PATH];
char gBtkPath[MAX_PATH];
int gTrkIdx;
int gStepCount;
BOOL gbBatchStopFlag;
BOOL CALLBACK dlg_BatchConfirmProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK dlg_BatchFinishProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam);
void Batch_InitTrkFile(char *sTrkFile);
void Batch_Init();
void Batch_Exit(boolean flag);
//-------------------------------<TestBed>-------------------------------//
#define DEBUG 0
int gRbins=10;
int gGbins=10;
int gBbins=10;
BOOL gbBinChange = FALSE;
int gTrackerSelect;
int gTrackerIndex=3; //default tracker algorithm
BOOL gDebugWinFlag = FALSE; //whether to display second debug window
IplImage *gImgDisplayMain;
IplImage *gImgDisplayDebug;
IplImage *gImgInitMaskHalf=NULL;
IplImage *gImgInitMask=NULL;
TkResult gTkResult;
extern KLT_FeatureTable ftBg;
void dis_DrawKltBgLines();
void img_CreateRegionMask(HRGN inRegion, IplImage *outImgMask);
BOOL dlg_Option();
BOOL CALLBACK dlg_OptionProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam);
void img_RewindVideo();
/***************************************************************************************************************************
* Name: WinMain *
* Description: Main window program and message processing loop. *
***************************************************************************************************************************/
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
{
MSG msg;
hAppInst = hInst; /* save for later */
/* Call initialization procedure */
if(!AppInit(hInst, hPrev, szCmdLine, sw))
{
AppExit();
return FALSE;
}
/*-------------------------------------------------------------------------
Polling messages from event queue
-------------------------------------------------------------------------*/
for(;;)
{
if(PeekMessage(&msg, NULL, 0, 0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
if(AppIdle())
WaitMessage();
}
}
AppExit();
return msg.wParam;
}
/*****************************************************************************
* Name: AppInit *
*****************************************************************************/
BOOL AppInit(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
{
int rstOpenFile;
bTime = 0.0;
FrameCount = 0;
hBuf = NULL;
hBuf2 = NULL;
ImageMaxX = 640;
ImageMaxY = 480;
//get current path for log files path
_getcwd(gExePath, _MAX_PATH);
strcpy(gExeLogPath, gExePath);
strcat(gExeLogPath, "\\tkLog");
_mkdir(gExeLogPath);
/* create windows */
RegisterWindowClasses(hInst, szCmdLine, sw);
hwndCtl = CreateControlWindow(hInst, szCmdLine, sw);
if(!hwndCtl)
{
MessageBox(0, "Cannot create window", szAppName, MB_OK);
return FALSE;
}
ShowWindow(hwndCtl,sw);
//draw buttons
DrawButtons(hwndCtl, 0, TRUE);
//read histogram bins setting from file
his_ReadHistBinsFromFile();
//read in image files
iio_Init(hwndCtl);
//prompt user to open file
rstOpenFile = iio_OpenFileByPrompt();
//create debug window
hwndCtl2 = CreateControlWindow(hInst, szCmdLine, sw);
if(!hwndCtl2)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -