📄 main.c.bak
字号:
wc.cbWndExtra = 0;
wc.hInstance = hAppInst;
wc.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1));
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
RegisterClass (&wc);
}
}
/*****************************************************************************
* Name: CreateControlWindow *
*****************************************************************************/
HWND CreateControlWindow(HINSTANCE hInst, LPSTR szCmdLine, int sw)
{
HWND hwnd;
hwnd = CreateWindow (szAppName, // window class name
szTitleBar,
//(WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX),
(WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX),
//WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hAppInst, // program instance handle
NULL); // creation parameters
return hwnd;
}
/*****************************************************************************
* Name: GetGlobals *
*****************************************************************************/
void GetGlobals(HWND hwnd, LPARAM lParam)
{
int i;
HDC hdc;
TEXTMETRIC tm; /* text metric structure */
/* get the maximum button length */
ButtonTextLen = 0;
for(i=0; i<NBUTTONS; i++)
if(ButtonTextLen < strlen(Functions[i].text))
ButtonTextLen = strlen(Functions[i].text);
hdc = GetDC (hwndCtl);
SelectObject(hdc, GetStockObject(SYSTEM_FONT));
GetTextMetrics (hdc, &tm);
ReleaseDC (hwndCtl, hdc);
cxChar = tm.tmAveCharWidth;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ButtonHeight = 4*cyChar/3;
ViewOffset = ButtonHeight;
/* calculate the initial window size */
WindowX = max(ImageMaxX,500) + (GetSystemMetrics(SM_CXFRAME)*2);
WindowY = max(ImageMaxY,240) + ButtonHeight + GetSystemMetrics (SM_CYCAPTION) + GetSystemMetrics (SM_CYFRAME)*2;
}
/*****************************************************************************
* Name: AllocBuffer *
*****************************************************************************/
int AllocBuffer(void)
{
hBuf = GlobalAlloc(GMEM_FIXED, (long)ImageMaxX * ImageMaxY*(((PIXEL_TYPE&0xFF)+7L)>>3));
if(hBuf == NULL)
{
MessageBox(0, "Cannot allocate buffer", szAppName, MB_OK);
return FALSE;
}
gpBits = (BYTE __PX_HUGE *)GlobalLock(hBuf);
hBuf2 = GlobalAlloc(GMEM_FIXED, (long)ImageMaxX * ImageMaxY*(((PIXEL_TYPE&0xFF)+7L)>>3));
//hBuf2 = GlobalAlloc(GMEM_FIXED, (long)ImageMaxX * ImageMaxY);
if(hBuf2 == NULL)
{
MessageBox(0, "Cannot allocate buffer", szAppName, MB_OK);
return FALSE;
}
gpBits2 = (BYTE __PX_HUGE *)GlobalLock(hBuf2);
memset(gpBits2, 0, ImageMaxX*ImageMaxY*3); //set zero
gImgDisplayMain = cvCreateImageHeader(cvSize(ImageMaxX, ImageMaxY), 8, 3);
gImgDisplayMain->imageData = gpBits;
gImgDisplayMain->imageDataOrigin = gpBits;
gImgDisplayDebug = cvCreateImageHeader(cvSize(ImageMaxX, ImageMaxY), 8, 3);
gImgDisplayDebug->imageData = gpBits2;
gImgDisplayDebug->imageDataOrigin = gpBits2;
return TRUE;
}
/*****************************************************************************/
void ReleaseBuffer()
/*****************************************************************************/
{
cvReleaseImageHeader(&gImgDisplayMain);
cvReleaseImageHeader(&gImgDisplayDebug);
if(hBuf)
{
GlobalUnlock(hBuf);
GlobalFree(hBuf);
}
if(hBuf2)
{
GlobalUnlock(hBuf2);
GlobalFree(hBuf2);
}
}
/*****************************************************************************
* Name: HourGlass *
*****************************************************************************/
void HourGlass(int on)
{
static HCURSOR hCursor;
if(on == ON)
{
hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
ShowCursor(TRUE);
}
else
{
ShowCursor(FALSE);
SetCursor(hCursor);
}
}
/******************************************************************************/
void dis_PrintMessage(){
char *frameName = iio_GetFileTitle();
char szTitleBar2[100];
char trackerDesc[5];
char *kltDesc;
char *logDesc;
char *predictDesc;
char sTkscore[10];
char sAffineMtx[6][20];
char sZoomAccum[10];
int i;
//if (FPS>30) FPS = 30;
if (gbReplayFlag==TRUE){
strcpy(trackerDesc,"On");
}
else{
strcpy(trackerDesc,"Off");
}
if (gbKltFlag==TRUE) kltDesc = "on";
else kltDesc = "off";
if (gbLogFlag==TRUE) logDesc = "on";
else logDesc = "off";
if (gbAutoPredictFlag==TRUE) predictDesc = "on";
else predictDesc = "off";
fn_DoubleToStr2(gTkResult.score,2, sTkscore);
//set title 1
wsprintf(szTitleBar,"%s; Klt:%s; Log:%s; PlayRpt:%s; Predict:%s; ",frameName, kltDesc, logDesc, trackerDesc, predictDesc);
SetWindowText(hwndCtl, szTitleBar);
//set title 2
for(i=0;i<6;i++){
fn_DoubleToStr2(gAffineMatrix[i], 2, sAffineMtx[i]);
}
fn_DoubleToStr2(gZoomAccum, 2, sZoomAccum);
wsprintf(szTitleBar2, "FPS:%d; Zoom:%s; score:%s;",FPS, sZoomAccum, sTkscore);
SetWindowText(hwndCtl2, szTitleBar2);
return;
}
///////////////////////////////////////////////////////////////////////////////
int img_StepOneFrame(int nextFrameNo)
//Step to next frame in sequence
{
IplImage *imgForeground;
int i;
float zoom;
int predictReady;
//if in the Replay Mood, read trk/rpt file and display it
if (gbReplayFlag == TRUE){
if (nextFrameNo == giFrameInitial){
iio_ReplayTrkFile();
//get image
gcImgCurrent = iio_GetInputImage();
//reset track Rect
gRectCurrent = gRectInit;
//stop backward browsing
giPlayFlag = 0;
//display changes
dis_ShowImgCurrent();
return 2;
}
//otherwise open rpt file
iio_ReplayRptFile(nextFrameNo);
//if no new file, stop looping
if (iio_GetFrameNo() !=nextFrameNo){
giPlayFlag = 0;
}
gcImgCurrent = iio_GetInputImage();
gRectCurrent.left = gaClickPoint[0].x;
gRectCurrent.top = gaClickPoint[0].y;
gRectCurrent.right = gaClickPoint[2].x;
gRectCurrent.bottom = gaClickPoint[2].y;
dis_ShowImgCurrent();
return 2;
}
//Read next frame from file
iio_ReadFileByIndex(nextFrameNo);
//if no new frame, return
if (gcImgCurrent==iio_GetInputImage() || iio_GetInputImage()==NULL){
giPlayFlag=0;
return 0;
}
//update image sequence info.
gcImgCurrent = iio_GetInputImage();
cvResize(gcImgCurrent, gcImgCurrentHalf, CV_INTER_LINEAR);
//if not in initialized, return
if (gbClickInitFlag==FALSE || gbTrackMood==FALSE){
//display current image and rect
dis_ShowImgCurrent();
return 0;
}
//klt tracker
if (gbKltFlag==TRUE){
klt_TrackNextFrame(gcImgCurrentHalf, gAffineMatrix);
//get zoom scale factor from affine matrix
zoom = (float)(fn_Abs(gAffineMatrix[0]) + fn_Abs(gAffineMatrix[4]))/2;
//if zoom scale is not 1 (different above threshold), accumulate zoom change.
if (fabs(zoom-1)>0.002){
gZoomAccum = gZoomAccum*zoom;
}
//change size if not in predict mood
if (gbPredictFlag==FALSE)
{
//change rect size based on scale factor accumulation
gBlobWidth = (int)fn_Round(gBlobWidthInit * gZoomAccum);
gBlobHeight = (int)fn_Round(gBlobHeightInit * gZoomAccum);
//adjust the current Blob size
utl_RectSizeAdjust(&gRectCurrentHalf, gBlobWidth, gBlobHeight, ImageMaxX/2, ImageMaxY/2);
}
}
else{
for (i=0;i<6;i++){
gAffineMatrix[i] = 0;
}
}
//runing tracker on this frame
switch(gTrackerIndex) {
case 2:
//Meanshift tracker
msw_TrackNextFrame(gcImgCurrentHalf, gRectCurrentHalf, &gTkResult);
break;
case 3:
//template matching tracker with Correlation
temp_TrackNextFrame_Coeff(gcImgCurrentHalf, gRectCurrentHalf, &gTkResult);
break;
default: //1
//HistogramShift - Enhanced meanshift by Foreground/Bkground
his_TrackNextFrame(gcImgCurrentHalf, gRectCurrentHalf, &gTkResult);
}
gRectCurrentHalf = gTkResult.targetBox;
utl_RectCheckBound(&gRectCurrentHalf, ImageMaxX/2, ImageMaxY/2);
//get foreground image/bitmap for report purpose
imgForeground = gTkResult.FGImage;
cvSetZero(gImgBitMap);
cvResize(imgForeground, gImgBitMap, CV_INTER_LINEAR);
//if occluded, predict motion
gTrackCount++;
if (gbAutoPredictFlag==TRUE){
if (gbPredictFlag==FALSE && gTkResult.occlusion==TRUE && gTrackCount>=3){
gTrackCount = 0;
gbPredictFlag=TRUE;
}
else if(gbPredictFlag==TRUE && gTkResult.occlusion==FALSE && gTrackCount>=3){
gTrackCount = 0;
gbPredictFlag=FALSE;
}
}
//predict motion on current frame
if(gbKltFlag==TRUE){
//update the Blob Position array to current camera coordinate, and shift one position in array
for (i=POSNUM-1;i>0;i--){
//affine transform
gPosPredict[i].x = 2*klt_CalcAffineX(gPosPredict[i-1].x/2, gPosPredict[i-1].y/2);
gPosPredict[i].y = 2*klt_CalcAffineY(gPosPredict[i-1].x/2, gPosPredict[i-1].y/2);
utl_PixelCheckBound_Float(&gPosPredict[i].x, &gPosPredict[i].y, ImageMaxX, ImageMaxY);
}
//get effective position count
gPosCount = min(gPosCount++, POSNUM);
//predict the blob position and get gAvgDx, gAvgDy;
predictReady = img_PredictMotion();
}
//shift the blob based on prediction if flag is on
if (gbPredictFlag==TRUE && predictReady==1)
{
//Since recent frames are occluded, rePredict position in gPosPredict[0]-[4] based on 5 frames earlier
for(i=gIgnoreFrameNum-1;i>=0;i--){
gPosPredict[i].x = gPosPredict[gIgnoreFrameNum].x+ gAvgDx*(gIgnoreFrameNum-i);
gPosPredict[i].y = gPosPredict[gIgnoreFrameNum].y+ gAvgDy*(gIgnoreFrameNum-i);
utl_PixelCheckBound_Float(&gPosPredict[i].x, &gPosPredict[i].y, ImageMaxX, ImageMaxY);
}
//reset target rect based on prediction
gRectCurrentHalf.left = (long)fn_Round(gPosPredict[0].x/2 - gBlobWidth/2);
gRectCurrentHalf.right = (long)fn_Round(gPosPredict[0].x/2 + gBlobWidth/2);
gRectCurrentHalf.top = (long)fn_Round(gPosPredict[0].y/2 - gBlobHeight/2);
gRectCurrentHalf.bottom = (long)fn_Round(gPosPredict[0].y/2 + gBlobHeight/2);
utl_RectCheckBound(&gRectCurrentHalf, ImageMaxX/2, ImageMaxY/2);
}
else{
//merge predict position with actual tracked position
gPosPredict[0].x = (float)gRectCurrentHalf.left+gRectCurrentHalf.right;
gPosPredict[0].y = (float)gRectCurrentHalf.top+gRectCurrentHalf.bottom;
}
//update full size RECT for display and Log report purpose
gRectCurrent.left = gRectCurrentHalf.left*2;
gRectCurrent.top = gRectCurrentHalf.top*2;
gRectCurrent.right = gRectCurrentHalf.right*2;
gRectCurrent.bottom = gRectCurrentHalf.bottom*2;
//print track report
if (gbLogFlag == TRUE){
tReport.time = iio_GetFrameNo();
tReport.framePath = iio_GetFilePath();
tReport.boxX = gRectCurrent.left;
tReport.boxY = ImageMaxY - gRectCurrent.bottom;
tReport.boxWid = abs(gRectCurrent.right - gRectCurrent.left);
tReport.boxHgt = abs(gRectCurrent.bottom - gRectCurrent.top);
tReport.imgWidth = ImageMaxX;
tReport.imgHeight = ImageMaxY;
io_PrintReport();
}
//flip the points for draw purpose on screen window
for (i=0;i<gPosCount;i++){
gPosDraw[i].x = (long)gPosPredict[i].x;
gPosDraw[i].y = ImageMaxY-1 + ButtonHeight - (long)gPosPredict[i].y;
}
//display current image and rect
dis_ShowImgCurrent();
return 1;
}
///////////////////////////////////////////////////////////////////////////////
void AppSetDisplay()
//set display window and related parameters based on ImageMaxX/ImageMaxY
{
ReleaseBuffer();
SetBitMapHead();
AllocBuffer();
GetGlobals(hwndCtl,0);
MoveWindow(hwndCtl,0,0,WindowX,WindowY,1);
InvalidateRect(hwndCtl, NULL, TRUE);
DrawButtons(hwndCtl, 0, FALSE); //resize buttons
//if debug windows exists
if (debugwin==1){
int screenx = GetSystemMetrics(SM_CXSCREEN);
int screeny = GetSystemMetrics(SM_CYSCREEN);
int left = min(WindowX,screenx-WindowX);
int top = (left>=WindowX-20?0:min(WindowY, screeny-30-WindowY));
//int top = 0;
left = max(left,0);
top = max(top,0);
MoveWindow(hwndCtl2,left,top,WindowX,WindowY,TRUE);
InvalidateRect(hwndCtl2, NULL, TRUE);
DrawButtons2(hwndCtl2, 0, FALSE); //resize buttons
}
SetActiveWindow(hwndCtl);
}
///////////////////////////////////////////////////////////////////////////////
void dis_ShowImgCurrent()
//copy current image into main window buffer
{
//if batch mood, return
if (gBatchFlag==TRUE) return;
//if no image, return
if (gcImgCurrent==NULL) return;
cvCopy(gcImgCurrent, gImgDisplayMain, NULL);
//paint image
AppPaint(hwndCtl, GetDC(hwndCtl));
//AppPaint2(hwndCtl2, GetDC(hwndCtl2));
//display debug image
dis_ShowImgDebug();
//print title
dis_PrintMessage();
}
///////////////////////////////////////////////////////////////////////////////
void dis_ShowImgDebug()
//copy current image into main window buffer
{
CvRect roi;
if (debugwin==0) return;
//if replay, no debug display
if (gbReplayFlag==TRUE) return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -