📄 square.cpp
字号:
}
//绘制带方向的方形区域
void Square::drawWithDirection(IplImage* img,int r,int g,int b)
{
if(!isIdentified)return;
CvPoint ptt;
ptt.x=X;
ptt.y=Y;
cvLine(img, ConnerPoint[0] , ptt, CV_RGB(255,255,255), 1, 8, 0 );
//ptt.x=X;
//ptt.y=Y;
//cvLine( img, ConnerPoint[1] , ptt, CV_RGB(255,255,0), 1, 8, 0 );
//ptt.x=X;
//ptt.y=Y;
//cvLine( img, ConnerPoint[2] , ptt, CV_RGB(0,255,0), 1, 8, 0 );
CvFont font;
cvInitFont(&font,10,0.4,0.4,0.2,0, 8 );
char info[80];
//显示每个点的内部识别编号
sprintf((char *)info,"(%d,%d)", row,col);
ptt.x=X;
ptt.y=Y;
cvPutText(img,info, ptt,&font, CV_RGB(255,0,255));
ptt.x=X+1;
ptt.y=Y;
cvPutText(img,info, ptt,&font, CV_RGB(255,255,0));
}
//绘制方形区域内容
void Square::drawAll(IplImage* img,int r,int g,int b)
{
if(r+g+b==0)
{
CvRNG rng=cvRNG((unsigned)this->X);
int icolor = cvRandInt(&rng);
cvFillConvexPoly(img, ConnerPoint,4,CV_RGB(icolor&255, (icolor>>8)&255, (icolor>>16)&255), 4, 0 );
}else
{
cvFillConvexPoly(img, ConnerPoint,4,CV_RGB(r,g,b), 4, 0 );
}
}
//判断一个点是否在方形区域内
bool Square::isInSquare(int Point_x,int Point_y)
{
if(Point_x<rect.Left || Point_x>rect.Right || Point_y<rect.Top || Point_y>rect.Bottom)
{
return false;
}
CvPoint pt;
pt.x=Point_x;
pt.y=Point_y;
if(inArea4(pt, ConnerPoint))
{
return true;
}
else
{
return false;
}
}
//绘制一个用于识别方形区域和他的ID点
void Square::drawWithIDPoint(IplImage* img)
{
//绘制
CvPoint DrawPoint[4];
DrawPoint[0].x = 0;
DrawPoint[0].y = 0;
DrawPoint[1].x = width;
DrawPoint[1].y = 0;
DrawPoint[2].x = width;
DrawPoint[2].y = height;
DrawPoint[3].x = 0;
DrawPoint[3].y = height;
cvFillConvexPoly(img, DrawPoint,4,CV_RGB(255,255,255), 4, 0 );
CvPoint ptt1;
CvPoint ptt2;
ptt1.x=0;
ptt1.y=0;
ptt2.x=width;
ptt2.y=0;
cvLine( img, ptt1, ptt2, CV_RGB(255,0,0), 2, 8, 0 );
ptt1.x=width;
ptt1.y=0;
ptt2.x=width;
ptt2.y=height;
cvLine( img, ptt1, ptt2, CV_RGB(255,0,0), 2, 8, 0 );
ptt1.x=width;
ptt1.y=height;
ptt2.x=0;
ptt2.y=height;
cvLine( img, ptt1, ptt2, CV_RGB(255,0,0), 2, 8, 0 );
ptt1.x=0;
ptt1.y=height;
ptt2.x=0;
ptt2.y=0;
cvLine( img, ptt1, ptt2, CV_RGB(255,0,0), 2, 8, 0 );
ptt1.x=width/3;
ptt1.y=0;
ptt2.x=width/3;
ptt2.y=height;
cvLine( img, ptt1, ptt2, CV_RGB(255,0,0), 2, 8, 0 );
ptt1.x=width*2/3;
ptt1.y=0;
ptt2.x=width*2/3;
ptt2.y=height;
cvLine( img, ptt1, ptt2, CV_RGB(255,0,0), 2, 8, 0 );
ptt1.x=0;
ptt1.y=height/3;
ptt2.x=width;
ptt2.y=height/3;
cvLine( img, ptt1, ptt2, CV_RGB(255,0,0), 2, 8, 0 );
ptt1.x=0;
ptt1.y=height*2/3;
ptt2.x=width;
ptt2.y=height*2/3;
cvLine( img, ptt1, ptt2, CV_RGB(255,0,0), 2, 8, 0 );
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(IDMatrix[i][j]!=0)
{
ptt1.x =(width/6)+(i*width)/3;
ptt1.y =(height/6)+(j*height)/3;
cvCircle( img, ptt1, 3, CV_RGB(255,0,0), 3, 8, 0 );
}
}
}
CvFont font;
cvInitFont(&font,10,0.4,0.4,0.2,0, 8 );
char info[80];
//显示每个点的内部识别编号
sprintf((char *)info,"InnerID:%d", InnerID);
ptt1.x=21;
ptt1.y=height+10;
cvPutText(img,info, ptt1,&font, CV_RGB(0,0,255));
ptt1.x=20;
ptt1.y=height+10;
cvPutText(img,info, ptt1,&font, CV_RGB(255,255,0));
}
//绘制四个顶点
void Square::drawConner(IplImage* img)
{
//CvFont font;
//CvPoint pt;
//cvInitFont(&font,10,0.4,0.4,0.2,0, 8 );
//char info[80];
//for(int i=0;i<4;i++)
//{
// //显示每个点的内部识别编号
// sprintf((char *)info,"%d", i);
// pt.x=ConnerPoint[i].x;
// pt.y=ConnerPoint[i].y;
// cvPutText(img,info, pt,&font, CV_RGB(255,0,0));
//}
}
//增加一个用于识别方形区域的ID点
void Square::addIDPoint(int Point_x,int Point_y)
{
int i=-1;
int j=-1;
if(Point_x==ConnerPoint[0].x && Point_y==ConnerPoint[0].y)
{
i=0;
j=0;
}
else if(Point_x==ConnerPoint[1].x && Point_y==ConnerPoint[1].y)
{
i=2;
j=0;
}
else if(Point_x==ConnerPoint[2].x && Point_y==ConnerPoint[2].y)
{
i=2;
j=2;
}
else if(Point_x==ConnerPoint[3].x && Point_y==ConnerPoint[3].y)
{
i=0;
j=2;
}else
{
double xx=0;
double yy=0;
CaculateXY(xx,yy,width,height,Point_x,Point_y,ConnerPoint[0].x,ConnerPoint[0].y,ConnerPoint[1].x,ConnerPoint[1].y,ConnerPoint[2].x,ConnerPoint[2].y,ConnerPoint[3].x,ConnerPoint[3].y);
if(xx>0 && xx<=width/3)i=0;
if(xx>width/3 && xx<=width*2/3)i=1;
if(xx>width*2/3 && xx<=width)i=2;
if(yy>0 && yy<=height/3)j=0;
if(yy>height/3 && yy<=height*2/3)j=1;
if(yy>height*2/3 && yy<=height)j=2;
}
if(i!=-1 && j!=-1)
{
//区分顺时针和逆时针两种情况处理
if(isDeasil)
{
//顺时针
IDMatrix[i][j]=1;
}else
{
//逆时针
IDMatrix[j][i]=1;
}
}
//updateInnerID();
}
void Square::updateInnerID(ConnArea* points,int ID)
{
//if(IDMatrix[0][0]!=0 || IDMatrix[0][2]!=0 || IDMatrix[2][0]!=0 || IDMatrix[2][2]!=0)
//{
// way=-1;
//}
if(IDMatrix[0][0]!=0)
{
isIdentified=true;
InnerID=0;
InnerID+=(long)(IDMatrix[0][0]*(long double)pow((long double)10,(long double)8));
InnerID+=(long)(IDMatrix[1][0]*(long double)pow((long double)10,(long double)7));
InnerID+=(long)(IDMatrix[2][0]*(long double)pow((long double)10,(long double)6));
InnerID+=(long)(IDMatrix[0][1]*(long double)pow((long double)10,(long double)5));
InnerID+=(long)(IDMatrix[1][1]*(long double)pow((long double)10,(long double)4));
InnerID+=(long)(IDMatrix[2][1]*(long double)pow((long double)10,(long double)3));
InnerID+=(long)(IDMatrix[0][2]*(long double)pow((long double)10,(long double)2));
InnerID+=(long)(IDMatrix[1][2]*(long double)pow((long double)10,(long double)1));
InnerID+=(long)(IDMatrix[2][2]*(long double)pow((long double)10,(long double)0));
if(isDeasil)
{
//顺序 0 1 2 3
//不变
}
else
{
//顺序 0 3 2 1
switch2Point(ConnerPoint[1],ConnerPoint[3]);
switch2Border(BorderIDs[1],BorderIDs[3]);
//switch2Border(BorderIDs[0],BorderIDs[1]);
//switch2Border(BorderIDs[1],BorderIDs[2]);
//switch2Border(BorderIDs[2],BorderIDs[3]);
//ConnerIDInDeasil[0]=0;
//ConnerIDInDeasil[1]=3;
//ConnerIDInDeasil[2]=2;
//ConnerIDInDeasil[3]=1;
}
}
else if(IDMatrix[2][0]!=0)
{
isIdentified=true;
InnerID=0;
InnerID+=(long)(IDMatrix[2][0]*(long double)pow((long double)10,(long double)8));
InnerID+=(long)(IDMatrix[2][1]*(long double)pow((long double)10,(long double)7));
InnerID+=(long)(IDMatrix[2][2]*(long double)pow((long double)10,(long double)6));
InnerID+=(long)(IDMatrix[1][0]*(long double)pow((long double)10,(long double)5));
InnerID+=(long)(IDMatrix[1][1]*(long double)pow((long double)10,(long double)4));
InnerID+=(long)(IDMatrix[1][2]*(long double)pow((long double)10,(long double)3));
InnerID+=(long)(IDMatrix[0][0]*(long double)pow(( long double)10,(long double)2));
InnerID+=(long)(IDMatrix[0][1]*(long double)pow((long double)10,(long double)1));
InnerID+=(long)(IDMatrix[0][2]*(long double)pow((long double)10,(long double)0));
if(isDeasil)
{
//顺序 1 2 3 0
switch2Point(ConnerPoint[0],ConnerPoint[1]);
switch2Point(ConnerPoint[1],ConnerPoint[2]);
switch2Point(ConnerPoint[2],ConnerPoint[3]);
switch2Border(BorderIDs[0],BorderIDs[1]);
switch2Border(BorderIDs[1],BorderIDs[2]);
switch2Border(BorderIDs[2],BorderIDs[3]);
//ConnerIDInDeasil[0]=1;
//ConnerIDInDeasil[1]=2;
//ConnerIDInDeasil[2]=3;
//ConnerIDInDeasil[3]=0;
}
else
{
//顺序 3 2 1 0
switch2Point(ConnerPoint[0],ConnerPoint[3]);
switch2Point(ConnerPoint[1],ConnerPoint[2]);
switch2Border(BorderIDs[0],BorderIDs[3]);
switch2Border(BorderIDs[1],BorderIDs[2]);
//switch2Border(BorderIDs[0],BorderIDs[1]);
//switch2Border(BorderIDs[1],BorderIDs[2]);
//switch2Border(BorderIDs[2],BorderIDs[3]);
//ConnerIDInDeasil[0]=3;
//ConnerIDInDeasil[1]=2;
//ConnerIDInDeasil[2]=1;
//ConnerIDInDeasil[3]=0;
}
}
else if(IDMatrix[2][2]!=0)
{
isIdentified=true;
InnerID=0;
InnerID+=(long)(IDMatrix[2][2]*(long double)pow((long double)10,(long double)8));
InnerID+=(long)(IDMatrix[1][2]*(long double)pow((long double)10,(long double)7));
InnerID+=(long)(IDMatrix[0][2]*(long double)pow((long double)10,(long double)6));
InnerID+=(long)(IDMatrix[2][1]*(long double)pow((long double)10,(long double)5));
InnerID+=(long)(IDMatrix[1][1]*(long double)pow((long double)10,(long double)4));
InnerID+=(long)(IDMatrix[0][1]*(long double)pow((long double)10,(long double)3));
InnerID+=(long)(IDMatrix[2][0]*(long double)pow((long double)10,(long double)2));
InnerID+=(long)(IDMatrix[1][0]*(long double)pow((long double)10,(long double)1));
InnerID+=(long)(IDMatrix[0][0]*(long double)pow((long double)10,(long double)0));
if(isDeasil)
{
//顺序 2 3 0 1
switch2Point(ConnerPoint[0],ConnerPoint[2]);
switch2Point(ConnerPoint[1],ConnerPoint[3]);
switch2Border(BorderIDs[0],BorderIDs[2]);
switch2Border(BorderIDs[1],BorderIDs[3]);
//ConnerIDInDeasil[0]=2;
//ConnerIDInDeasil[1]=3;
//ConnerIDInDeasil[2]=0;
//ConnerIDInDeasil[3]=1;
}
else
{
//顺序 2 1 0 3
switch2Point(ConnerPoint[0],ConnerPoint[2]);
switch2Border(BorderIDs[0],BorderIDs[2]);
//switch2Border(BorderIDs[0],BorderIDs[1]);
//switch2Border(BorderIDs[1],BorderIDs[2]);
//switch2Border(BorderIDs[2],BorderIDs[3]);
//ConnerIDInDeasil[0]=2;
//ConnerIDInDeasil[1]=1;
//ConnerIDInDeasil[2]=0;
//ConnerIDInDeasil[3]=3;
}
}
else if(IDMatrix[0][2]!=0)
{
isIdentified=true;
InnerID=0;
InnerID+=(long)(IDMatrix[0][2]*(long double)pow((long double)10,(long double)8));
InnerID+=(long)(IDMatrix[0][1]*(long double)pow((long double)10,(long double)7));
InnerID+=(long)(IDMatrix[0][0]*(long double)pow((long double)10,(long double)6));
InnerID+=(long)(IDMatrix[1][2]*(long double)pow((long double)10,(long double)5));
InnerID+=(long)(IDMatrix[1][1]*(long double)pow((long double)10,(long double)4));
InnerID+=(long)(IDMatrix[1][0]*(long double)pow((long double)10,(long double)3));
InnerID+=(long)(IDMatrix[2][2]*(long double)pow((long double)10,(long double)2));
InnerID+=(long)(IDMatrix[2][1]*(long double)pow((long double)10,(long double)1));
InnerID+=(long)(IDMatrix[2][0]*(long double)pow((long double)10,(long double)0));
if(isDeasil)
{
//顺序 3 0 1 2
switch2Point(ConnerPoint[0],ConnerPoint[3]);
switch2Point(ConnerPoint[3],ConnerPoint[2]);
switch2Point(ConnerPoint[2],ConnerPoint[1]);
switch2Border(BorderIDs[0],BorderIDs[3]);
switch2Border(BorderIDs[3],BorderIDs[2]);
switch2Border(BorderIDs[2],BorderIDs[1]);
//ConnerIDInDeasil[0]=3;
//ConnerIDInDeasil[1]=0;
//ConnerIDInDeasil[2]=1;
//ConnerIDInDeasil[3]=2;
}
else
{ //顺序 1 0 3 2
switch2Point(ConnerPoint[0],ConnerPoint[1]);
switch2Point(ConnerPoint[2],ConnerPoint[3]);
switch2Border(BorderIDs[0],BorderIDs[1]);
switch2Border(BorderIDs[2],BorderIDs[3]);
//switch2Border(BorderIDs[0],BorderIDs[1]);
//switch2Border(BorderIDs[1],BorderIDs[2]);
//switch2Border(BorderIDs[2],BorderIDs[3]);
//ConnerIDInDeasil[0]=1;
//ConnerIDInDeasil[1]=0;
//ConnerIDInDeasil[2]=3;
//ConnerIDInDeasil[3]=2;
}
}
NearedSquare[0]=getNearedSquare(points,BorderIDs[0],BorderIDs[1],ID);
NearedSquare[1]=getNearedSquare(points,BorderIDs[1],BorderIDs[2],ID);
NearedSquare[2]=getNearedSquare(points,BorderIDs[2],BorderIDs[3],ID);
NearedSquare[3]=getNearedSquare(points,BorderIDs[3],BorderIDs[0],ID);
}
//绘制最小矩形区域
//DrawPoint(resultimg,squares[2].rect.Left,squares[2].rect.Top,1,1);
//DrawPoint(resultimg,squares[2].rect.Right ,squares[2].rect.Top,2,1);
//DrawPoint(resultimg,squares[2].rect.Right ,squares[2].rect.Bottom ,3,1);
//DrawPoint(resultimg,squares[2].rect.Left,squares[2].rect.Bottom ,4,1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -