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

📄 square.cpp

📁 用于系统跟踪的程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "stdafx.h"
#include "square.h"
#include "aglrothim.h"
#pragma warning(disable : 4996)

//判断元素是否包含在方块数组中
bool isInSquares(Square* square,int& squareCount,int X, int Y)
{
	for(int i=0;i<squareCount;i++)
	{
		if(square[i].X==X && square[i].Y==Y)
			return true;
	}
	return false;
}
int getIndex(CvPoint Pt,CvPoint Pts[4])
{
	for(int i=0;i<4;i++)
	{
		if(Pt.x==Pts[i].x && Pt.y==Pts[i].y )
		{	
			return i;
		}
	}
	return -1;
}
//传递方形区域的迭代函数
bool transferSquareID(Square* square,ConnArea* points,int squareCount, int CurrentID,int row,int col)
{
 
	if(!square[CurrentID].isIdentified)
	{
		square[CurrentID].updateInnerID(points,CurrentID);		
	}
	if(square[CurrentID].row==-1 && square[CurrentID].col==-1)
	{
		square[CurrentID].row=row;
		square[CurrentID].col=col;

	}else if(square[CurrentID].row!=row || square[CurrentID].col!=col)
	{
		//return false;
	} 

	int NewID=-1;


	//参考文件“方形区域ID传递示意.vsd”

	//上
	NewID=square[CurrentID].NearedSquare[0];
	if(NewID>=0 && NewID<squareCount)
	{
		if(!square[NewID].isIdentified)
		{
			int index=getIndex(square[CurrentID].ConnerPoint[0],square[NewID].ConnerPoint);
			if(square[NewID].isDeasil)
			{
				index=index+1;
				if(index==4)index=0;
			}else
			{
				index=index-1;
				if(index==-1)index=3;
			}
			if(index!=-1)
			{
				square[NewID].addIDPoint(square[NewID].ConnerPoint[index].x,square[NewID].ConnerPoint[index].y); 
				square[NewID].updateInnerID(points,NewID);
				transferSquareID(square,points,squareCount,NewID,row+1,col);
				 
			}
		}
		
	}
	//右
	NewID=square[CurrentID].NearedSquare[1];
	if(NewID>=0 && NewID<squareCount)
	{
		if(!square[NewID].isIdentified)
		{
			int index=getIndex(square[CurrentID].ConnerPoint[1],square[NewID].ConnerPoint);
			//if(square[NewID].isDeasil)
			//{
			//	index=index+1;
			//	if(index==4)index=0;
			//}else
			//{
			//	index=index-1;
			//	if(index==-1)index=3;
			//}
			if(index!=-1)
			{
				square[NewID].addIDPoint(square[NewID].ConnerPoint[index].x,square[NewID].ConnerPoint[index].y); 
				square[NewID].updateInnerID(points,NewID);
				transferSquareID(square,points,squareCount,NewID,row,col+1);
			}
		}
	}

	//下
	NewID=square[CurrentID].NearedSquare[2];
	if(NewID>=0 && NewID<squareCount)
	{
		if(!square[NewID].isIdentified)
		{
			int index=getIndex(square[CurrentID].ConnerPoint[3],square[NewID].ConnerPoint);
			//if(square[NewID].isDeasil)
			//{
			//	index=index+1;
			//	if(index==4)index=0;
			//}else
			//{
			//	index=index-1;
			//	if(index==-1)index=3;
			//}
			if(index!=-1)
			{
				square[NewID].addIDPoint(square[NewID].ConnerPoint[index].x,square[NewID].ConnerPoint[index].y); 
				square[NewID].updateInnerID(points,NewID);
				transferSquareID(square,points,squareCount,NewID,row-1,col);
			}
		}
		
	}

	//左
	NewID=square[CurrentID].NearedSquare[3];
	if(NewID>=0 && NewID<squareCount)
	{
		if(!square[NewID].isIdentified)
		{
			int index=getIndex(square[CurrentID].ConnerPoint[0],square[NewID].ConnerPoint);
			if(square[NewID].isDeasil)
			{
				index=index-1;
				if(index==-1)index=3;
			}else
			{
				index=index+1;
				if(index==4)index=0;
			}
			if(index!=-1)
			{
				square[NewID].addIDPoint(square[NewID].ConnerPoint[index].x,square[NewID].ConnerPoint[index].y); 
				square[NewID].updateInnerID(points,NewID);
				transferSquareID(square,points,squareCount,NewID,row,col-1);
			}
		}	
	}
	return true;
}

//扫描方形区域使用的迭代函数
void ScanSquare(IplImage* img,ConnArea* points,int id,int PointCount,int& level,int Index0,int IDList[5],Square* square,int& squareCount)
{  
	IDList[level]=id;
	level++;
	if(id<0 || id>=PointCount)
	{
		level--;
		return;
	}
	if(points[id].Index<=0 && points[id].Index>=250)
	{
		level--;
		return;
	}
	if(level>5)
	{
		level--;
		return;
	}
	if(level==5)
	{
		if(points[id].Index == Index0)
		{

			//找到方形区域
			int id0=IDList[0];
			int id1=IDList[1];
			int id2=IDList[2];
			int id3=IDList[3];
		
			double squareX=(points[id0].X+points[id1].X+points[id2].X+points[id3].X)/4;
			double squareY=(points[id0].Y+points[id1].Y+points[id2].Y+points[id3].Y)/4;

			//判断新找到的方块是否已经存在于数组中
			if(!isInSquares(square,squareCount,(int)squareX,int(squareY)))
			{

				if(points[id0].SquareCount<4)
				{
					points[id0].SquareID[points[id0].SquareCount]=squareCount;
					points[id0].SquareCount++;
				}
				if(points[id1].SquareCount<4)
				{
					points[id1].SquareID[points[id1].SquareCount]=squareCount;
					points[id1].SquareCount++;
				}
				if(points[id2].SquareCount<4)
				{
					points[id2].SquareID[points[id2].SquareCount]=squareCount;
					points[id2].SquareCount++;
				}
				if(points[id3].SquareCount<4)
				{
					points[id3].SquareID[points[id3].SquareCount]=squareCount;
					points[id3].SquareCount++;
				}
				square[squareCount].clear();
				square[squareCount].BorderIDs[0]=IDList[0];
				square[squareCount].BorderIDs[1]=IDList[1];
				square[squareCount].BorderIDs[2]=IDList[2];
				square[squareCount].BorderIDs[3]=IDList[3];

				for(int i=0;i<4;i++)
				{
					int x=(int)points[IDList[i]].X;
					int y=(int)points[IDList[i]].Y;
					if(x<square[squareCount].rect.Left)square[squareCount].rect.Left=x;
					if(x>square[squareCount].rect.Right)square[squareCount].rect.Right=x;
					if(y<square[squareCount].rect.Top)square[squareCount].rect.Top =y;
					if(y>square[squareCount].rect.Bottom )square[squareCount].rect.Bottom=y;
				}


			
				///////!!!!!!!!!!!!!!!!!!!!!!!!!							
				///////在半圆墙壁上四边形的四个顶点好像不是四个边的交线!							
				///////	可以尝试使用四个边线标志点的中心!!!!!!!!						
				///////!!!!!!!!!!!!!!!!!!!!!!!!!			
				//square[squareCount].ConnerPoint[0]=getPointOfIntersection(points[IDList[0]].wx ,points[IDList[0]].wy,points[IDList[0]].x0,points[IDList[0]].y0,points[IDList[1]].wx ,points[IDList[1]].wy,points[IDList[1]].x0,points[IDList[1]].y0);
				//square[squareCount].ConnerPoint[1]=getPointOfIntersection(points[IDList[1]].wx ,points[IDList[1]].wy,points[IDList[1]].x0,points[IDList[1]].y0,points[IDList[2]].wx ,points[IDList[2]].wy,points[IDList[2]].x0,points[IDList[2]].y0);
				//square[squareCount].ConnerPoint[2]=getPointOfIntersection(points[IDList[2]].wx ,points[IDList[2]].wy,points[IDList[2]].x0,points[IDList[2]].y0,points[IDList[3]].wx ,points[IDList[3]].wy,points[IDList[3]].x0,points[IDList[3]].y0);
				//square[squareCount].ConnerPoint[3]=getPointOfIntersection(points[IDList[3]].wx ,points[IDList[3]].wy,points[IDList[3]].x0,points[IDList[3]].y0,points[IDList[0]].wx ,points[IDList[0]].wy,points[IDList[0]].x0,points[IDList[0]].y0);
				square[squareCount].ConnerPoint[0].x =(int)points[IDList[0]].X;
				square[squareCount].ConnerPoint[0].y =(int)points[IDList[0]].Y;	
				square[squareCount].ConnerPoint[1].x =(int)points[IDList[1]].X;
				square[squareCount].ConnerPoint[1].y =(int)points[IDList[1]].Y;	
				square[squareCount].ConnerPoint[2].x =(int)points[IDList[2]].X;
				square[squareCount].ConnerPoint[2].y =(int)points[IDList[2]].Y;	
				square[squareCount].ConnerPoint[3].x =(int)points[IDList[3]].X;
				square[squareCount].ConnerPoint[3].y =(int)points[IDList[3]].Y;	
				square[squareCount].X=(int)squareX;
				square[squareCount].Y=(int)squareY;

				
				double A=getAreaOfTriangleNoAbs(square[squareCount].ConnerPoint[0].x,square[squareCount].ConnerPoint[0].y,square[squareCount].ConnerPoint[1].x,square[squareCount].ConnerPoint[1].y,square[squareCount].ConnerPoint[2].x,square[squareCount].ConnerPoint[2].y);
				square[squareCount].isDeasil=(A>0);
				 
				
				squareCount++;
			}

			//(*(img->imageData+0+3*(img->width*(int)Yscan+(int)Xscan)))
		}
		level--;
		return;
	}
	if(points[id].NearConnAreaIndex1-1>=0 && points[id].NearConnAreaIndex1-1<250)
		ScanSquare(img,points,points[id].NearConnAreaIndex1-1,PointCount,level,Index0,IDList,square,squareCount);
	if(points[id].NearConnAreaIndex2-1>=0 && points[id].NearConnAreaIndex2-1<250)
		ScanSquare(img,points,points[id].NearConnAreaIndex2-1,PointCount,level,Index0,IDList,square,squareCount);
	
	level--;
	return;
}

 
//得到某个方向区域相邻的方形区域的函数
int Square::getNearedSquare(ConnArea* points,int BorderID1,int BorderID2,int CurrentID)
{
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4;j++)
		{
			int ID1=points[BorderID1].SquareID[i];
			int ID2=points[BorderID2].SquareID[j];
			if(ID1==ID2 && ID1>=0 && ID1!=CurrentID)
			{
				return ID1;
			}
		}
	}
	return -1;
}
//交换两个边
void Square::switch2Border(int &BorderID1,int &BorderID2)
{
	int b=BorderID1;
	BorderID1=BorderID2;
	BorderID2=b;
}

//交换两个点
void Square::switch2Point(CvPoint &pt1,CvPoint &pt2)
{
	CvPoint pt;
	pt.x=pt1.x;
	pt.y=pt1.y;

	pt1.x=pt2.x;
	pt1.y=pt2.y;

	pt2.x=pt.x;
	pt2.y=pt.y;
}

//初始化
Square::Square()
{
	BorderIDs[0]=-1;
	BorderIDs[1]=-1;
	BorderIDs[2]=-1;
	BorderIDs[3]=-1;
 
	rect.Top=10000;
	rect.Left=10000;
	rect.Bottom=-10000;
	rect.Right=-10000;

	ConnerPoint[0].x=0;
	ConnerPoint[0].y=0;
	ConnerPoint[1].x=0;
	ConnerPoint[1].y=0;
	ConnerPoint[2].x=0;
	ConnerPoint[2].y=0;
	ConnerPoint[3].x=0;
	ConnerPoint[3].y=0;

	X=0;
	Y=0;

	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			IDMatrix[i][j]=0;
		}
	}

	width=90;
	height=90;
	InnerID=0;


	row=-1;
	col=-1;
	//ConnerIDInDeasil[0]=0;
	//ConnerIDInDeasil[1]=0;
	//ConnerIDInDeasil[2]=0;
	//ConnerIDInDeasil[3]=0;

	isDeasil=false;
	isIDSquare=false;
	isIdentified=false;
 
	
	Compute_X=0;
	Compute_Y=0;
	Compute_Z=0;
	Compute_Alpha=0;
	Compute_Beta=0;
	Compute_Gama=0;
}

void Square::clear()
{
	BorderIDs[0]=-1;
	BorderIDs[1]=-1;
	BorderIDs[2]=-1;
	BorderIDs[3]=-1;
 
	rect.Top=10000;
	rect.Left=10000;
	rect.Bottom=-10000;
	rect.Right=-10000;

	ConnerPoint[0].x=0;
	ConnerPoint[0].y=0;
	ConnerPoint[1].x=0;
	ConnerPoint[1].y=0;
	ConnerPoint[2].x=0;
	ConnerPoint[2].y=0;
	ConnerPoint[3].x=0;
	ConnerPoint[3].y=0;

	X=0;
	Y=0;

	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			IDMatrix[i][j]=0;
		}
	}

	width=90;
	height=90;
	InnerID=0;


	row=-1;
	col=-1;
	//ConnerIDInDeasil[0]=0;
	//ConnerIDInDeasil[1]=0;
	//ConnerIDInDeasil[2]=0;
	//ConnerIDInDeasil[3]=0;

	isDeasil=false;
	isIDSquare=false;
	isIdentified=false;


	Compute_X=0;
	Compute_Y=0;
	Compute_Z=0;
	Compute_Alpha=0;
	Compute_Beta=0;
	Compute_Gama=0;
}

//绘制方形区域边界
void Square::drawBorder(IplImage* img,int r,int g,int b)
{

	cvLine(img,ConnerPoint[0],ConnerPoint[1],CV_RGB(r,g,b), 1, 4, 0 );
	cvLine(img,ConnerPoint[1],ConnerPoint[2],CV_RGB(r,g,b), 1, 4, 0 );
	cvLine(img,ConnerPoint[2],ConnerPoint[3],CV_RGB(r,g,b), 1, 4, 0 );
	cvLine(img,ConnerPoint[3],ConnerPoint[0],CV_RGB(r,g,b), 1, 4, 0 );

	//for(int i=0;i<4;i++)
	//{
	//	CvFont font;
	//	cvInitFont(&font,10,0.4,0.4,0.2,0, 8 );
	//	char  info[80];
	//	//显示每个点的内部识别编号
	//	sprintf((char *)info,"(%d)", this->BorderIDs[i]); 
	//	cvPutText(img,info, ConnerPoint[i],&font, CV_RGB(255,0,255));
	//}

⌨️ 快捷键说明

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