⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 学习跟踪的好程序
💻 C
📖 第 1 页 / 共 5 页
字号:
/*****************************************************************************
 * Name: DrawButtons                                                         *
 *****************************************************************************/
void DrawButtons(HWND hwnd, LPARAM lParam, int draw)
{
	int i, rem;
	RECT Rect;

	GetClientRect(hwnd, (LPRECT)&Rect);
	ButtonWidth = Rect.right/NBUTTONS;
	rem = Rect.right % NBUTTONS;

	if(draw)
	{
		/*---------------------------------------------------------------------
		create the function buttons as child windows
		---------------------------------------------------------------------*/
		for(i=0; i<NBUTTONS; i++)
		{
			if(i == NBUTTONS-1)
				Functions[i].hwnd = CreateWindow("button", Functions[i].text,
				WS_CHILD | WS_VISIBLE | Functions[i].style,
				ButtonWidth*i, 0, ButtonWidth+rem, ButtonHeight,
				hwnd, (HMENU)Functions[i].id, hAppInst, NULL);
			else
				Functions[i].hwnd = CreateWindow("button", Functions[i].text,
				WS_CHILD | WS_VISIBLE | Functions[i].style,
				ButtonWidth*i, 0, ButtonWidth, ButtonHeight,
				hwnd, (HMENU)Functions[i].id, hAppInst, NULL);
		}
	}
	else
	{
		/*---------------------------------------------------------------------
		resize the function buttons
		---------------------------------------------------------------------*/
		for(i=0; i<NBUTTONS; i++)
		{
			if(i == NBUTTONS-1)
				SetWindowPos(Functions[i].hwnd,NULL,ButtonWidth*i,0,
				ButtonWidth+rem,ButtonHeight,SWP_SHOWWINDOW);
			else
				SetWindowPos(Functions[i].hwnd,NULL,ButtonWidth*i,0,
				ButtonWidth,ButtonHeight,SWP_SHOWWINDOW);
		}
	}
}
/*****************************************************************************
 * Name: DrawButtons2                                                         *
 *****************************************************************************/
void DrawButtons2(HWND hwnd, LPARAM lParam, int draw)
{
	int i, rem;
	RECT Rect;

	GetClientRect(hwnd, (LPRECT)&Rect);
	ButtonWidth = Rect.right/NBUTTONS;
	rem = Rect.right % NBUTTONS;

	if(draw)
	{
		/*---------------------------------------------------------------------
		create the function buttons as child windows
		---------------------------------------------------------------------*/
		for(i=0; i<NBUTTONS; i++)
		{
			if(i == NBUTTONS-1)
				Functions2[i].hwnd = CreateWindow("button", Functions[i].text,
				WS_CHILD | WS_VISIBLE | WS_DISABLED | Functions[i].style,
				ButtonWidth*i, 0, ButtonWidth+rem, ButtonHeight,
				hwnd, (HMENU)Functions[i].id, hAppInst, NULL);
			else
				Functions2[i].hwnd = CreateWindow("button", Functions[i].text,
				WS_CHILD | WS_VISIBLE | WS_DISABLED | Functions[i].style,
				ButtonWidth*i, 0, ButtonWidth, ButtonHeight,
				hwnd, (HMENU)Functions[i].id, hAppInst, NULL);
		}
	}
	else
	{
		/*---------------------------------------------------------------------
		resize the function buttons
		---------------------------------------------------------------------*/
		for(i=0; i<NBUTTONS; i++)
		{
			if(i == NBUTTONS-1)
				SetWindowPos(Functions2[i].hwnd,NULL,ButtonWidth*i,0,
				ButtonWidth+rem,ButtonHeight,SWP_SHOWWINDOW);
			else
				SetWindowPos(Functions2[i].hwnd,NULL,ButtonWidth*i,0,
				ButtonWidth,ButtonHeight,SWP_SHOWWINDOW);
		}
	}
}

/*****************************************************************************
 * Name: CreateGrayPalette                                                   *
 *****************************************************************************/
void CreateGrayPalette(void)
{
	int i;
	struct
	{
		WORD Version;
		WORD NumberOfEntries;
		PALETTEENTRY aEntries[256];
	} Palette;

	if (hpalette)
		DeleteObject(hpalette);

	Palette.Version = 0x300;
	Palette.NumberOfEntries = 256;
	for (i=0; i<256; ++i)
	{
		Palette.aEntries[i].peRed = i;
		Palette.aEntries[i].peGreen = i;
		Palette.aEntries[i].peBlue = i;
		Palette.aEntries[i].peFlags = PC_NOCOLLAPSE;
		maphead.colors[i].rgbBlue = i;
		maphead.colors[i].rgbGreen= i;
		maphead.colors[i].rgbRed = i;
		maphead.colors[i].rgbReserved = 0;
	}
	hpalette = CreatePalette((LOGPALETTE *)&Palette);
}
/*****************************************************************************/
int GetImage()
/*****************************************************************************/
{		
	int stopFlag;
	int stepRst;

	//if in batch mood, go to next until no more trk file or rpt file
	if (gBatchFlag==TRUE){
		if (gbBatchStopFlag==FALSE){		
			stepRst = img_StepOneFrame(iio_GetFrameNo()+1);
			gStepCount++;

			//default flag is not-stop
			stopFlag = 0;
			
			//if tracker stop (no more rpt file or failed)		
			if (stepRst==0 || gStepCount>=gBtkStepNum[gTrkIdx]){
				// if no more trk sequence, stop, else fetch next trk
				if (gTrkIdx>=gBtkFileCount-1){
					Batch_Exit(TRUE);
					stopFlag = 1;
				}
				else{
					gTrkIdx++;
					Batch_InitTrkFile(gBtkFileArray[gTrkIdx]);
					gStepCount=0;
					stopFlag = 0;
				}
			}
		}
		else{
			gBatchFlag = FALSE;
			Batch_Exit(TRUE);
			stopFlag = 1;
		}
		return stopFlag;
	}

	//if play mood, step one frame
	switch(giPlayFlag) {
	case 1: //track forward
		img_StepOneFrame(iio_GetFrameNo()+1);
		FrameCount++;		
		stopFlag = 0;
		break;
	case 2: //track backward
		img_StepOneFrame(iio_GetFrameNo()-1);
		FrameCount++;		
		stopFlag = 0;
		break;
	default:		
		//FrameCount = 0;		
		stopFlag = 1;
		break;
	}

	//FrameCount++;
	return stopFlag; 
}
/*****************************************************************************
 * Name: SetBitMapHead                                                       *
 *****************************************************************************/
void SetBitMapHead(void)
{
	// Set up bitmap header
	maphead.head.biSize=sizeof(BITMAPINFOHEADER);
	maphead.head.biWidth=ImageMaxX;
	maphead.head.biHeight=ImageMaxY;
	maphead.head.biPlanes=1;
	maphead.head.biBitCount=PIXEL_TYPE&0xFF;
	maphead.head.biCompression=BI_RGB;
	maphead.head.biSizeImage=0;
	maphead.head.biXPelsPerMeter=0;
	maphead.head.biYPelsPerMeter=0;
	maphead.head.biClrUsed=0;
	maphead.head.biClrImportant=0;
}
/*****************************************************************************
 * Name: RegisterWindowClasses                                               *
 *****************************************************************************/
void RegisterWindowClasses(HINSTANCE hInst, LPSTR szCmdLine, int sw)
{
	WNDCLASS  wc;
	static ATOM aClass = 0;

	memset(&wc,0,sizeof(wc));         /* clear stack variable */

	/* register the control window class */

	if(aClass == 0)
	{
		wc.style = CS_BYTEALIGNWINDOW | CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
		wc.lpfnWndProc   = CtlProc;
		wc.cbClsExtra    = 0;
		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 ;	
	char	szTitleBar2[100];
	char	trackerDesc[5];
	char	*kltDesc;
	char	*logDesc;
	char	*predictDesc;
	char	sTkscore[15];
	char	sAffineMtx[6][20];
	char	sZoomAccum[10];
	int		i;

	//if (FPS>30) FPS = 30;
	
	if (gbReplayFlag==TRUE){
		strcpy(trackerDesc,"On");
		frameName = iio_GetFileTitle();
	}
	else{
		strcpy(trackerDesc,"Off");
		frameName = iio_GetImageTitle();
	}

	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
	if (gBatchFlag==TRUE){
		wsprintf(szTitleBar,"%s; Batch mode, press STOP to quit ",frameName);
	}
	else{	
		wsprintf(szTitleBar,"%s; Klt:%s; Log:%s; Rpt:%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; %s",FPS, sZoomAccum, sTkscore, feat_GetFeatLabel());
	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;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -