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

📄 liveviewdlg.cpp

📁 该程序实现FIRE足球机器人竞赛中的3:3比赛源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// LiveViewDlg.cpp : implementation file
//

#include "stdafx.h"
#include "LiveView.h"
#include "LiveViewDlg.h"
#include "mmsystem.h"
#include "math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define COLORSELECT 0
#define BOUNDSET    1

#define stackSize 200
int stackx[stackSize];
int stacky[stackSize];
int stackPointer;

#define MASKCOLOR  10687
#define MASKCOLOR1  10688
#define OPPMASKCOLOR 10689

#define TEAM          0
#define MEMB1         1
#define MEMB2         2
#define MEMB3         3
#define MEMB4         4
#define MEMB5         5
#define BALL          6
#define OPP           7

int     robot_xy[360][12][2];             //机器人仿真图像关键点查找表
CPen    OurRobotPen(PS_SOLID,1,RGB(255,255,0));     //一个黄色画笔
CPen    BallPen(PS_SOLID,1,RGB(255,128,0));         //一个桔黄色画笔
CBrush  BallBrush(RGB(255,128,0));
CPen    OppRobotPen(PS_SOLID,1,RGB(255,0,255));
CBrush  OppRobotBrush(RGB(0,128,0));
int		m_LUT[7][0x8000];
int     m_LUTBK[0x8000];
Ground  ground;

/////////////////////////////////////////////////////////////////////////////
// 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)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CLiveViewDlg dialog

CLiveViewDlg::CLiveViewDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLiveViewDlg::IDD, pParent)
{
	m_savePara=false;
	m_stop=false;
	m_pBCameraSet=new CBCameraSet;
	m_colortest=true;      //单帧采集时是否显示,当测试时为false不显示
	m_test=FALSE; //用在显示线程中,辨识测试时为true;
    m_start=FALSE;        //用在显示线程中,开始比赛时为true;
	m_LiveGrabbing = false;//! 标记是否正在采集,True是采集正在进行
	m_bIsDispImage = FALSE;//是否显示图像
	m_bConnectBcam = false;
	m_bEnabledWhiteBalancer=false;
	for (int i=0; i<NUM_BUFFERS; i++)//! 指针数组,每个数组成员是指向一个图像缓冲区的指针
		m_ptrBitmaps[i] = NULL;
    m_ptrBmpInfo = NULL;//! 指针,保存每帧图像的BitmapInfo,在回显图像时使用
	m_bOCROk = true;
	InitOK = FALSE;
	//丢失的帧数
	m_nLostFrame = 0;
    m_ImageSize.cx=640;
    m_ImageSize.cy=480;
	m_pMyQueue = new CMyQueue(MYQUEUE_LENGTH, m_ImageSize, 8);
	if ( ! m_evtNewBufferAvailable.Create() )
	{
        throw BcamException(::GetLastError(), "CCamera: Create Event");
	}
    if ( ! m_evtTerminate.Create() )
	{
        throw BcamException(::GetLastError(), "CCamera: Create Event");
	}
	m_pDispSingle = new BYTE[m_ImageSize.cx*m_ImageSize.cy*3];
    m_pSingleBitmap = new char[m_ImageSize.cx * m_ImageSize.cy];
	/////////////// 预计算仿真图像中机器人关键点相对中心的偏移量 //////
	double ttheta,side=7.5*1.5;
	for (i=0;i<=360;i++)
	{       
		ttheta=3.1415926*(2-(double)i/180);
		robot_xy[i][0][0]=(int)(side*cos(ttheta)-side*sin(ttheta));
		robot_xy[i][0][1]=(int)(side*sin(ttheta)+side*cos(ttheta));
		robot_xy[i][1][0]=(int)(side*cos(ttheta)+side*sin(ttheta));
		robot_xy[i][1][1]=(int)(side*sin(ttheta)-side*cos(ttheta));
		robot_xy[i][2][0]=(int)(-side*cos(ttheta)-side*sin(ttheta));
		robot_xy[i][2][1]=(int)(-side*sin(ttheta)+side*cos(ttheta));			
		robot_xy[i][3][0]=(int)(-side*cos(ttheta)+side*sin(ttheta));
		robot_xy[i][3][1]=(int)(-side*sin(ttheta)-side*cos(ttheta));

		robot_xy[i][4][0]=(robot_xy[i][0][0]+robot_xy[i][1][0])/2;
		robot_xy[i][4][1]=(robot_xy[i][0][1]+robot_xy[i][1][1])/2;
		robot_xy[i][5][0]=(robot_xy[i][2][0]+robot_xy[i][3][0])/2;
		robot_xy[i][5][1]=(robot_xy[i][2][1]+robot_xy[i][3][1])/2;
		robot_xy[i][6][0]=(robot_xy[i][0][0]+robot_xy[i][2][0])/2;
		robot_xy[i][6][1]=(robot_xy[i][0][1]+robot_xy[i][2][1])/2;
		robot_xy[i][7][0]=(robot_xy[i][1][0]+robot_xy[i][3][0])/2;
		robot_xy[i][7][1]=(robot_xy[i][1][1]+robot_xy[i][3][1])/2;
		robot_xy[i][8][0]=robot_xy[i][6][0]/2;
		robot_xy[i][8][1]=robot_xy[i][6][1]/2;
		robot_xy[i][9][0]=robot_xy[i][7][0]/2;
		robot_xy[i][9][1]=robot_xy[i][7][1]/2;

		robot_xy[i][10][0]=(robot_xy[i][4][0]+robot_xy[i][6][0])/2;
		robot_xy[i][10][1]=(robot_xy[i][4][1]+robot_xy[i][6][1])/2;
		robot_xy[i][11][0]=(robot_xy[i][4][0]+robot_xy[i][7][0])/2;
		robot_xy[i][11][1]=(robot_xy[i][4][1]+robot_xy[i][7][1])/2;
	}
	//{{AFX_DATA_INIT(CLiveViewDlg)
	m_brightness = 0;
	m_gain = 0;
	m_shutter = 0;
	m_slidegain = 0;
	m_v = 0;
	m_u = 0;
	m_slideshutter = 0;
	m_slideu = 0;
	m_slidev = 0;
	m_slidebrightness = 0;
	m_caprate = 0.0;
	m_disprate = 0.0;
	m_setselect = 0;
	m_attack = 0;
	m_area = 0;
	m_kick = 0;
	m_lostframe = 0;
	m_nesample = FALSE;
	m_nefilter = TRUE;
	m_robotnum = 0;
	m_rightvalue = 0;
	m_leftvalue = 0;
	m_object = 0;
	m_change1 = 0;
	m_change2 = 0;
	m_SelectRect = FALSE;
	m_CorrectPatch = TRUE;
	m_length =11;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CLiveViewDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLiveViewDlg)
	DDX_Control(pDX, IDC_STATIC_ZOOMAREA, m_local);
	DDX_Control(pDX, IDC_STATIC_MEM2, m_smem2);
	DDX_Control(pDX, IDC_SAVE, m_save);
	DDX_Control(pDX, IDC_STATIC_TEAM, m_steam);
	DDX_Control(pDX, IDC_STATIC_MEM5, m_smem5);
	DDX_Control(pDX, IDC_STATIC_MEM4, m_smem4);
	DDX_Control(pDX, IDC_STATIC_MEM3, m_smem3);
	DDX_Control(pDX, IDC_STATIC_MEM1, m_smem1);
	DDX_Control(pDX, IDC_STATIC_BALL, m_sball);
	DDX_Control(pDX, IDC_STRATEGYSELECT, m_strategyselect);
	DDX_Control(pDX, IDC_STATIC_DISPLAY, m_display);
	DDX_Text(pDX, IDC_EDIT_BRIGHTNESS, m_brightness);
	DDX_Text(pDX, IDC_EDIT_GAIN, m_gain);
	DDX_Text(pDX, IDC_EDIT_SHUTTER, m_shutter);
	DDX_Slider(pDX, IDC_SLIDER_GAIN, m_slidegain);
	DDX_Text(pDX, IDC_EDIT_V, m_v);
	DDX_Text(pDX, IDC_EDIT_U, m_u);
	DDX_Slider(pDX, IDC_SLIDER_SHUTTER, m_slideshutter);
	DDX_Slider(pDX, IDC_SLIDER_U, m_slideu);
	DDX_Slider(pDX, IDC_SLIDER_V, m_slidev);
	DDX_Slider(pDX, IDC_SLIDER_BRIGHTNESS, m_slidebrightness);
	DDX_Text(pDX, IDC_EDIT_CAPTRUERATE, m_caprate);
	DDX_Text(pDX, IDC_EDIT_DISPLAYRATE, m_disprate);
	DDX_Radio(pDX, IDC_RADIO_COLORSELECT, m_setselect);
	DDX_Radio(pDX, IDC_ATTACK, m_attack);
	DDX_Radio(pDX, IDC_LEFTA, m_area);
	DDX_Radio(pDX, IDC_NORMAL, m_kick);
	DDX_Text(pDX, IDC_EDIT_LOSTFRAMES, m_lostframe);
	DDX_Check(pDX, IDC_CHECK_NESAMPLE, m_nesample);
	DDX_Check(pDX, IDC_CHECK_NEFILTER, m_nefilter);
	DDX_Text(pDX, IDC_EDIT_ROBOTNUM, m_robotnum);
	DDX_Text(pDX, IDC_EDIT_RIGHTVALUE, m_rightvalue);
	DDX_Text(pDX, IDC_EDIT_LEFTVALUE, m_leftvalue);
	DDX_Radio(pDX, IDC_RADIO_TEAM, m_object);
	DDX_Text(pDX, IDC_EDIT_CHANGE1, m_change1);
	DDX_Text(pDX, IDC_EDIT_CHANGE2, m_change2);
	DDX_Check(pDX, IDC_CHECK_SELECTRECT, m_SelectRect);
	DDX_Check(pDX, IDC_CHECK_PATCHCORRECTION, m_CorrectPatch);
	DDX_Text(pDX, IDC_EDIT_LENGHT, m_length);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLiveViewDlg, CDialog)
	//{{AFX_MSG_MAP(CLiveViewDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CAPTRUE, OnCaptrue)
	ON_MESSAGE(WM_GRAB_STOPPED, OnGrabStopped)
	ON_MESSAGE(WM_ERROR, OnError)
	ON_BN_CLICKED(IDC_STOP, OnStop)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_BRIGHTNESS, OnReleasedcaptureSliderBrightness)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_GAIN, OnReleasedcaptureSliderGain)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_SHUTTER, OnReleasedcaptureSliderShutter)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_U, OnReleasedcaptureSliderU)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_V, OnReleasedcaptureSliderV)
	ON_EN_CHANGE(IDC_EDIT_BRIGHTNESS, OnChangeEditBrightness)
	ON_EN_CHANGE(IDC_EDIT_GAIN, OnChangeEditGain)
	ON_EN_CHANGE(IDC_EDIT_SHUTTER, OnChangeEditShutter)
	ON_EN_CHANGE(IDC_EDIT_U, OnChangeEditU)
	ON_EN_CHANGE(IDC_EDIT_V, OnChangeEditV)
	ON_BN_CLICKED(IDC_SAVEPARA, OnSavepara)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_GRABSINGLE, OnGrabsingle)
	ON_WM_LBUTTONDOWN()
	ON_BN_CLICKED(IDC_RADIO_COLORSELECT, OnRadioColorselect)
	ON_BN_CLICKED(IDC_RADIO_BOUNDSET, OnRadioBoundset)
	ON_WM_MOUSEMOVE()
	ON_BN_CLICKED(IDC_BOUNDSET, OnBoundset)
	ON_BN_CLICKED(IDC_LOAD, OnLoad)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_BN_CLICKED(IDC_SAVEAS, OnSaveas)
	ON_BN_CLICKED(IDC_COLORSAMPLING, OnColorsampling)
	ON_BN_CLICKED(IDC_COLORTEST, OnColortest)
	ON_BN_CLICKED(IDC_COLORFILTER, OnColorfilter)
	ON_BN_CLICKED(IDC_CLEAR, OnClear)
	ON_BN_CLICKED(IDC_IDENTIFYTEST, OnIdentifytest)
	ON_BN_CLICKED(IDC_INITIAL, OnInitial)
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_ENLARGE, OnEnlarge)
	ON_BN_CLICKED(IDC_NEWSAMPLE, OnNewsample)
	ON_BN_CLICKED(IDC_NEWFILTER, OnNewfilter)
	ON_BN_CLICKED(IDC_ZERO, OnZero)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_FORTH, OnForth)
	ON_BN_CLICKED(IDC_BACK, OnBack)
	ON_BN_CLICKED(IDC_LEFT, OnLeft)
	ON_BN_CLICKED(IDC_RIGHT, OnRight)
	ON_BN_CLICKED(IDC_STOPROBOT, OnStoprobot)
	ON_BN_CLICKED(IDC_WHITEBALANCE, OnWhitebalance)
	ON_BN_CLICKED(IDC_COLOREXCHANGE, OnColorexchange)
	ON_BN_CLICKED(IDC_REBACK, OnReback)
	ON_BN_CLICKED(IDC_BUTTON_RETURN, OnButtonReturn)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLiveViewDlg message handlers

BOOL CLiveViewDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	InitGlobVar();// 初始化全局变量
	GetParaFromIniFile();// 读取INI文件中的摄像机参数

	CSliderCtrl *pSC = (CSliderCtrl *)GetDlgItem(IDC_SLIDER_BRIGHTNESS);
	pSC->SetRange(0, 255, TRUE);
	pSC = (CSliderCtrl *)GetDlgItem(IDC_SLIDER_GAIN);
	pSC->SetRange(192, 640, TRUE);
	pSC = (CSliderCtrl *)GetDlgItem(IDC_SLIDER_SHUTTER);
	pSC->SetRange(1, 4095, TRUE);
	pSC = (CSliderCtrl *)GetDlgItem(IDC_SLIDER_U);
	pSC->SetRange(16, 255, TRUE);
	pSC = (CSliderCtrl *)GetDlgItem(IDC_SLIDER_V);
	pSC->SetRange(16, 255, TRUE);

	SetBrightnessVal(m_nBrightness);
	SetGainVal(m_nGain);
	SetShutterVal(m_nShutter);
	SetUVal(m_nU);
	SetVVal(m_nV);

	m_ptrBmpInfo = (PBITMAPINFO)new BYTE[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
	m_ptrBmpInfo->bmiHeader.biBitCount = 24;
	m_ptrBmpInfo->bmiHeader.biClrImportant = 0;
	m_ptrBmpInfo->bmiHeader.biClrUsed = 0;
	m_ptrBmpInfo->bmiHeader.biCompression = BI_RGB;
	m_ptrBmpInfo->bmiHeader.biHeight = m_ImageSize.cy;
	m_ptrBmpInfo->bmiHeader.biPlanes = 1;
	m_ptrBmpInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	m_ptrBmpInfo->bmiHeader.biSizeImage = 0;
	m_ptrBmpInfo->bmiHeader.biWidth = m_ImageSize.cx;
	m_ptrBmpInfo->bmiHeader.biXPelsPerMeter = 0;
	m_ptrBmpInfo->bmiHeader.biYPelsPerMeter = 0;
	for (int i=0; i<256; i++)
	{
		m_ptrBmpInfo->bmiColors[i].rgbBlue = (BYTE)i;
		m_ptrBmpInfo->bmiColors[i].rgbGreen = (BYTE)i;
		m_ptrBmpInfo->bmiColors[i].rgbRed = (BYTE)i;
		m_ptrBmpInfo->bmiColors[i].rgbReserved = 0;
	} 

	CBitmap Bitmap;   
	Bitmap.LoadBitmap(IDB_GROUND);
groundDC.CreateCompatibleDC(NULL);   
	groundDC.SelectObject(&Bitmap);   
	Bitmap.DeleteObject(); 
   	/////// 初始化自己的虚拟图形设备 ////////
CDC*pDC=GetDC();
    //创建兼容设备描述表///
	CBitmap rawBitmap,destBitmap;
    destBitmap.CreateCompatibleBitmap(pDC,640,480);
    ///装载位图///
	destDC.CreateCompatibleDC(pDC);              //for result             
    destDC.SelectObject(&destBitmap);	
	ReleaseDC(pDC);
	m_RobotComm.InitCom(1);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CLiveViewDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CLiveViewDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.

⌨️ 快捷键说明

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