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

📄 videodlg.cpp

📁 本程序是2005年参加中国机器人大赛的比赛程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
			 YUV[R][G][B][0]=(char)(0.500*R+(-0.419)*G+(-0.081)*B+127);
		 }
}

void CVideoDlg::OnGet() 
{
//手动调阈值
m_pConfigDlg->GetYuzhi();
m_y_low =m_pConfigDlg->now_low_y;
m_u_low =m_pConfigDlg->now_low_u;
m_v_low =m_pConfigDlg->now_low_v;
m_y_high=m_pConfigDlg->now_high_y;
m_u_high=m_pConfigDlg->now_high_u;
m_v_high=m_pConfigDlg->now_high_v;
UpdateData(false);	
//UINT nPos;
//nPos=now_high_y;
m_yuv_yh.SetScrollPos(m_y_high);
m_yuv_uh.SetScrollPos(m_u_high);
m_yuv_vh.SetScrollPos(m_v_high);
m_yuv_yl.SetScrollPos(m_y_low);
m_yuv_ul.SetScrollPos(m_u_low);
m_yuv_vl.SetScrollPos(m_v_low);
}

BOOL CVideoDlg::SensitiveScan(double percent,
							  int i,
							  int j,
							  byte Upper[],
							  byte Lower[],
							  int ReturnCentrePoint[])
{
/*#######################################################//
	函数名:
		SensitiveScan(double percent, 
							  int i,
							  int j,
							  double Upper[],
							  double Lower[],
							  int ReturnCentrePoint[])
	说明:
		一种区域扫描方法,从中心向四周扫描。返回目标中心。		
//#######################################################*/
	int count=0;
	int rowSum=0;
	int colSum=0;

	int DirectionCount=0;//++之后方向会改变一次
	int Direction;
		//0->right	i++	j
		//1->down	i	j++
		//2->left	i--	j
		//3->up		i	j--
	int Step;//每个方向上应该走几步
	int StepCount;//用于记录一个方向已经走了几步
	double n=0.9;//一个在潜移默化中自增的神秘变量
	bool SkipLine[4]={false};//四个方向上分别判断是否skip?
	bool MeetSignal=false;//判断一行中是否有点满足IsMeetStrict()//黄点出现
	bool BreakSignal=false;//判断是否跳出while的死循环
		
	while(!BreakSignal)
	{
		DirectionCount++;
		n+=0.5;
		Step=(int)n;
		Direction=DirectionCount%4;
		switch(Direction)
		{
			case 0://right
				MeetSignal=false;//一开始时没有出现黄点
				if(m_FieldRect.right-i<=Step)SkipLine[1]=true;
				if(SkipLine[Direction]){i+=Step;break;}
				for(StepCount=1;StepCount<=Step;StepCount++)
				{
					i++;
					if(i>m_FieldRect.right)continue;
					GetPixelColor(i, j, Get_RGB);	
					if(IsMeetRestrict(Get_RGB, Upper, Lower))
					{
						//记录此点;
						rowSum += i;
						colSum += j;
						count++;
						MeetSignal=true;
					}
					else
					{
						if(MeetSignal==false)i+=2;
						else 
						{
							i=i+(Step-StepCount);
							StepCount=Step+1;
						}
					}
				}
				if(!MeetSignal)SkipLine[Direction]=true;
				MeetSignal=false;
				break;
			case 1://down
				if(m_FieldRect.bottom-j<=Step)SkipLine[2]=true;
				if(SkipLine[Direction]){j+=Step;break;}
				for(StepCount=1;StepCount<=Step;StepCount++)
				{
					j++;
					if(j>m_FieldRect.bottom)continue;
					GetPixelColor(i, j, Get_RGB);	
					if(IsMeetRestrict(Get_RGB, Upper, Lower))
					{
						//记录此点;
						rowSum += i;
						colSum += j;
						count++;
						MeetSignal=true;
					}
					else
					{
						if(MeetSignal==false)j+=2;
						else 
						{
							j=j+(Step-StepCount);
							StepCount=Step+1;
						}
					}
				}
				if(!MeetSignal)SkipLine[Direction]=true;
				MeetSignal=false;
				break;
			case 2://left
				if(i-m_FieldRect.left<=Step)SkipLine[3]=true;
				if(SkipLine[Direction]){i-=Step;break;}
				for(StepCount=1;StepCount<=Step;StepCount++)
				{
					i--;
					if(i<m_FieldRect.left)continue;
					GetPixelColor(i, j, Get_RGB);	
					if(IsMeetRestrict(Get_RGB, Upper, Lower))
					{
						//记录此点;
						rowSum += i;
						colSum += j;
						count++;
						MeetSignal=true;
					}
					else
					{
						if(MeetSignal==false)i-=2;
						else 
						{
							i=i-(Step-StepCount);
							StepCount=Step+1;
						}
					}
				}
				if(!MeetSignal)SkipLine[Direction]=true;
				MeetSignal=false;
				break;
			case 3://up
				if(j-m_FieldRect.top<=Step)SkipLine[0]=true;
				if(SkipLine[Direction]){j-=Step;break;}
				for(StepCount=1;StepCount<=Step;StepCount++)
				{
					j--;
					if(j<m_FieldRect.top)continue;	
					GetPixelColor(i, j, Get_RGB);	
					if(IsMeetRestrict(Get_RGB, Upper, Lower))
					{
						//记录此点;
						rowSum += i;
						colSum += j;
						count++;
						MeetSignal=true;
					}
					else
					{
						if(MeetSignal==false)j-=2;
						else 
						{
							j=j-(Step-StepCount);
							StepCount=Step+1;
						}
					}
				}
				if(!MeetSignal)SkipLine[Direction]=true;			
				MeetSignal=false;
				break;
			default:
				break;
		}//switch ended
		BreakSignal=SkipLine[0];
		for(int t=1;t<4;t++)BreakSignal=BreakSignal&&SkipLine[t];
	}//while ended
	//判断是否是噪声点
	if(count<(int)(m_pConfigDlg->m_ObjectDia*m_pConfigDlg->m_ObjectDia*percent))return false;
	ReturnCentrePoint[1] = (int)(rowSum / count);//0
	ReturnCentrePoint[0] = (int)(colSum / count);//1
	return true;
}

void CVideoDlg::OnInitYUVClass() 
{
	// TODO: Add your control notification handler code here

	//BYTE	YUVClass[256][9][3];//0-255级颜色,9种颜色(这一位很重要),三个颜色坐标

	for(int i=0;i<256;i++)
	{
		for (int j=0;j<9;j++)//五种颜色
		{
			for(int k=0;k<3;k++)
			{
				YUVClass[i][j][k]=0;
				//九种颜色编码如下
				//	0	我队标志	1	pink	2	white
				//	3	green		4	ball	5	他队标志
				//	6	他队一		7	他队二	8	场地
				
			//8	场地
			if((i<(int)m_pConfigDlg->m_upper_Field[k])
			&&(i>(int)m_pConfigDlg->m_lower_Field[k]))
			YUVClass[i][8][k]=1;
			
			//4	ball
			if((i<(int)m_pConfigDlg->m_upper_Ball[k])
			&&(i>(int)m_pConfigDlg->m_lower_Ball[k]))
			YUVClass[i][4][k]=1;

			//5	他队标志
			if((i<(int)m_pConfigDlg->m_upper_TheirTeamLable[k])
			&&(i>(int)m_pConfigDlg->m_lower_TheirTeamLable[k]))
			YUVClass[i][5][k]=1;

			//0	我队标志
			if((i<(int)m_pConfigDlg->m_upper_MyTeamLable[k])
			&&(i>(int)m_pConfigDlg->m_lower_MyTeamLable[k]))
			YUVClass[i][0][k]=1;

			//m_upper_MyTeam=>pink0 white1 green2
			//1	pink
			if((i<(int)m_pConfigDlg->m_upper_MyTeam[0][k])
			&&(i>(int)m_pConfigDlg->m_lower_MyTeam[0][k]))
			YUVClass[i][1][k]=1;

			//2	white
			if((i<(int)m_pConfigDlg->m_upper_MyTeam[1][k])
			&&(i>(int)m_pConfigDlg->m_lower_MyTeam[1][k]))
			YUVClass[i][2][k]=1;

			//3	green
			if((i<(int)m_pConfigDlg->m_upper_MyTeam[2][k])
			&&(i>(int)m_pConfigDlg->m_lower_MyTeam[2][k]))
			YUVClass[i][3][k]=1;

			//6	他队一		
			if((i<(int)m_pConfigDlg->m_upper_TheirTeam[0][k])
			&&(i>(int)m_pConfigDlg->m_lower_TheirTeam[0][k]))
			YUVClass[i][6][k]=1;
			//7	他队二
			if((i<(int)m_pConfigDlg->m_upper_TheirTeam[1][k])
			&&(i>(int)m_pConfigDlg->m_lower_TheirTeam[1][k]))
			YUVClass[i][7][k]=1;
			}
		}
	}
}

void CVideoDlg::SearchObject()
{
/*		if(flag==TRUE)
	{
	return;
	}*/
//	flag=TRUE;
//	num=-1;
//	num2=-1;
//	myone =false;
	for (i=0;i<5;i++)
	{
		isScanMyteam[i]=false;
		isScanTheirteam[i]=false;
	}
	isScanBall=false;
	BallRectNum = 0;	 
	MyLableRectNum = 0;
	TheirLableRectNum = 0;
	ObjectRectNum = 0;

	double ball_Percent;
	double mylable_Percent;
	double thlable_Percent;
//	double pink_Percent;
	ball_Percent=0.15;
	mylable_Percent=0.10;
	thlable_Percent=0.10;
//	pink_Percent=0.02;


	m_pConfigDlg->m_ObjectDia=8;
	m_pConfigDlg->m_ObjectPercent=0.10;

	their_yellow=false;

	//CG400Capture(hcg400,false);
	
	/////////////////////////辩识物体////////////////////////
//	m_pConfigDlg->UpdateData(FALSE);
	for(j = m_FieldRect.top; j < m_FieldRect.bottom; j++)
		for(i = m_FieldRect.left; i < m_FieldRect.right; i+=2)		
		{
			 bIsSkip = FALSE;
			for(n = 0; n < ObjectRectNum; n++)
			{			
				if (isInRect(i,j,ObjectRect[n]))				
				{
					bIsSkip = TRUE;
					n=ObjectRectNum;
				}
			}
			if(bIsSkip)
				continue;
			GetPixelColor(i, j, Get_RGB);
			RGB_TO_YUV(Get_RGB,Col_YUV);
		//	if(IsMeetRestrict(Col_YUV, m_pConfigDlg->m_upper_Field, m_pConfigDlg->m_lower_Field))
			if(YUVClass[Col_YUV[2]][8][2]
			 &&YUVClass[Col_YUV[1]][8][1]
			 &&YUVClass[Col_YUV[0]][8][0])
			{//去掉场地
				continue;
			}
		///	else if(IsMeetRestrict(Col_YUV,m_pConfigDlg->m_upper_MyTeamLable, m_pConfigDlg->m_lower_MyTeamLable))
			else if(YUVClass[Col_YUV[2]][0][2]
			      &&YUVClass[Col_YUV[1]][0][1]
			      &&YUVClass[Col_YUV[0]][0][0])
			{//识别我队标志
				GetPixelColor(i+1, j, Get_RGB);
				RGB_TO_YUV(Get_RGB,Col_YUV);
				if(YUVClass[Col_YUV[2]][0][2]
			      &&YUVClass[Col_YUV[1]][0][1]
			      &&YUVClass[Col_YUV[0]][0][0])
				{
				int tempCentreGravity[2];
				int tempCentreGravity2[2];
				int tempCentreGravity3[2];
				int tempCentre[3][3];
				int lable=0;
				ScanRect.left = i - m_pConfigDlg->m_ObjectDia/2;
				ScanRect.top = j- m_pConfigDlg->m_ObjectDia/2;
				ScanRect.right = i + m_pConfigDlg->m_ObjectDia;
				ScanRect.bottom = j + m_pConfigDlg->m_ObjectDia;
				if( ScanNearRect(ScanRect, 
								 mylable_Percent,
								 m_pConfigDlg->m_upper_MyTeamLable,
								 m_pConfigDlg->m_lower_MyTeamLable,
								 tempCentreGravity,
								 0) )
				{
					for (int i=0;i<3;i++)
					{
					 for (int j=0;j<3;j++)
					 {
						 tempCentre[i][j]=0;
					 }
						ScanRect3[i].left=0;
						ScanRect3[i].right=0;
						ScanRect3[i].top=0;
						ScanRect3[i].bottom=0;
					}
					lable=0;

					if(MyLableRectNum < 5)
					{						
						for(l=tempCentreGravity[0]-2*m_pConfigDlg->m_ObjectDia;l<tempCentreGravity[0]+2*m_pConfigDlg->m_ObjectDia;l++)
							for(m=tempCentreGravity[1]-2*m_pConfigDlg->m_ObjectDia;m<tempCentreGravity[1]+2*m_pConfigDlg->m_ObjectDia;m++)							
							{									 
								if(isInRect(m,l,ScanRect3[0]))
									 continue;
								if(isInRect(m,l,ScanRect3[1]))
									 continue;
								if(isInRect(m,l,ScanRect3[2]))
									 continue;
								
								GetPixelColor(m, l, Get_RGB);
								RGB_TO_YUV(Get_RGB,Col_YUV);
								if((YUVClass[Col_YUV[2]][1][2]&&YUVClass[Col_YUV[1]][1][1]&&YUVClass[Col_YUV[0]][1][0])
									||
									/*	(YUVClass[Col_YUV[2]][2][2]&&YUVClass[Col_YUV[1]][2][1]&&YUVClass[Col_YUV[0]][2][0])
									||*/
									(YUVClass[Col_YUV[2]][3][2]&&YUVClass[Col_YUV[1]][3][1]&&YUVClass[Col_YUV[0]][3][0]))
								{	
								   GetPixelColor(m, l, Get_RGB);
								   RGB_TO_YUV(Get_RGB,Col_YUV);
								   if((YUVClass[Col_YUV[2]][1][2]&&YUVClass[Col_YUV[1]][1][1]&&YUVClass[Col_YUV[0]][1][0])
									||
								/*	(YUVClass[Col_YUV[2]][2][2]&&YUVClass[Col_YUV[1]][2][1]&&YUVClass[Col_YUV[0]][2][0])
									||*/
									(YUVClass[Col_YUV[2]][3][2]&&YUVClass[Col_YUV[1]][3][1]&&YUVClass[Col_YUV[0]][3][0]))
								   {	
									if (YUVClass[Col_YUV[2]][1][2]
									&&YUVClass[Col_YUV[1]][1][1]
									&&YUVClass[Col_YUV[0]][1][0])									
									num=1;
								/*	if (YUVClass[Col_YUV[2]][2][2]
									&&YUVClass[Col_YUV[1]][2][1]
									&&YUVClass[Col_YUV[0]][2][0])									
									num=2;*/
									if (YUVClass[Col_YUV[2]][3][2]
									&&YUVClass[Col_YUV[1]][3][1]
									&&YUVClass[Col_YUV[0]][3][0])								
									num=3;									
									ScanRect2.left = m - m_pConfigDlg->m_ObjectDia;
									ScanRect2.top = l;
									ScanRect2.right = m + m_pConfigDlg->m_ObjectDia;
									ScanRect2.bottom = l + m_pConfigDlg->m_ObjectDia;
									if(ScanNearRect(ScanRect2, 											
											 m_pConfigDlg->m_ObjectPercent,
											 m_pConfigDlg->m_upper_MyTeam[k],
											 m_pConfigDlg->m_lower_MyTeam[k],
											 tempCentreGravity2
											 ,
											 num) )
										{
												tempCentre[lable][0]=tempCentreGravity2[0];
												tempCentre[lable][1]=tempCentreGravity2[1];
												tempCentre[lable][2]=num;
												ScanRect3[lable].left=tempCentreGravity2[1] - m_pConfigDlg->m_ObjectDia;
												ScanRect3[lable].top = tempCentreGravity2[0]-m_pConfigDlg->m_ObjectDia;
												ScanRect3[lable].right = tempCentreGravity2[1] +m_pConfigDlg->m_ObjectDia;
												ScanRect3[lable].bottom = tempCentreGravity2[0] + m_pConfigDlg->m_ObjectDia;
												lable++;
												if (lable==3)
												{

⌨️ 快捷键说明

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