ReconizeDlg.cpp
上传用户:chinamans
上传日期:2016-04-11
资源大小:14485k
文件大小:25k
- // ReconizeDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Reconize.h"
- #include "ReconizeDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- BYTE Buffer[1000][1000][3]; // 24位真彩色图// 0--blue 1--green 2--red
- BYTE Buffer1[1000][1000]; // 灰度图
- BYTE BufferReg[1000][1000]; //二值图
- BYTE pByte[3200000]; //内存镜像文件
- int index1,index2,index3,index4; //分别是以下四个数组的上界标识.
- int PointLT[50000][3] ,PointRT[50000][3] ,PointRB[50000][3] ,PointLB[50000][3] ;
- int indexRect;
- int Rect[50000][4]; //匹配的矩形记录,里面放的是PointLT等..中对应坐标的索引.
- int avgPointLT[50000][3] ,avgPointRT[50000][3] ,avgPointRB[50000][3] ,avgPointLB[50000][3] ;//依次对应匹配的平均矩形的左顶点,右顶点,右底点,左底点底坐标
- int avgindexRect; //匹配的平均矩形记录
- int HaveJumpRectLT[50000][3],HaveJumpRectRT[50000][3],HaveJumpRectRB[50000][3],HaveJumpRectLB[50000][3];//含有足够跳变的矩形坐标记录
- int JumpindexRect; //含有足够跳变的矩形数目
- ///////////////////////////////////////////////////////////////////////////
- // 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()
- /////////////////////////////////////////////////////////////////////////////
- // CReconizeDlg dialog
- CReconizeDlg::CReconizeDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CReconizeDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CReconizeDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CReconizeDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CReconizeDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CReconizeDlg, CDialog)
- //{{AFX_MSG_MAP(CReconizeDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_OPEN_FILE, OnOpenFile)
- ON_BN_CLICKED(IDC_TO_GRAY, OnToGray)
- ON_BN_CLICKED(IDC_FILE_EXIT, OnFileExit)
- ON_BN_CLICKED(IDC_WHITE_BLACK, OnWhiteBlack)
- ON_BN_CLICKED(IDC_DRAW_EDGE, OnDrawEdge)
- ON_BN_CLICKED(IDC_DRAW_RECT, OnDrawRect)
- ON_BN_CLICKED(IDC_QUICK_RECONIZE, OnQuickReconize)
- ON_BN_CLICKED(IDC_GET_RECT, OnGetRect)
- ON_COMMAND(IDC_DRAW_EDGE, OnDrawEdge)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CReconizeDlg message handlers
- BOOL CReconizeDlg::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
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CReconizeDlg::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 CReconizeDlg::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.
- HCURSOR CReconizeDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CReconizeDlg::OnOpenFile()
- {
- // TODO: Add your control notification handler code here
- CFileDialog OpenDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,"位图文件(*.BMP)|*.bmp|ALL FILES(*.*)|*.*");
- if(OpenDlg.DoModal()==IDOK)
- {
- InvalidateRect(NULL);
- FillMatrix(OpenDlg.GetPathName());
- DrawBMP();
- }
- }
- void CReconizeDlg::DrawBMP()//绘制bmp
- {
- CClientDC dc(this);
- int i,j;
- for(i=0;i<m_Width;i++) //rgb
- for(j=0;j<m_Height;j++)
- dc.SetPixel(i+1,j+1,RGB(Buffer[i][j][2],Buffer[i][j][1],Buffer[i][j][0]) ); //why 0--blue 1--green 2--red
- ////////////////////////////////////////////////////////
- }
- void CReconizeDlg::FillMatrix(CString filename) //将原始图形文件读入数组Buffer
- {
- ////////////////////////////////////////////////////
- m_Width=0;
- m_Height=0;
- CFile fbmp;
- if (fbmp.Open(filename,CFile::modeRead)==FALSE)
- {
- MessageBox("file access error");
- return;
- }
- char head1,head2;
- fbmp.Read(&head1,1);
- fbmp.Read(&head2,1);
- if(head1!='B' || head2!='M' )
- {
- MessageBox("none acceptable bmp file");
- fbmp.Close();
- return;
- }
- ////////////////////////////////////////////////////
- DWORD filesize=fbmp.GetLength();
- fbmp.Seek(0,CFile::begin);
- //fbmp.ReadHuge(pByte,filesize);
- fbmp.Read(pByte, filesize);
- fbmp.Close();
- BYTE *p;
- p=pByte+18;
- int tmpWidth,tmpHeight;
- tmpWidth= *p + (*(p+1)) *256 + (*(p+2)) *256*256 + (*(p+3)) *256*256*256 ;
- p=pByte+22;
- tmpHeight= *p + (*(p+1)) *256 + (*(p+2)) *256*256 + (*(p+3)) *256*256*256 ;
- CString tmpStr1;
- tmpStr1.Format("BMP file size :width %d , height %d",tmpWidth,tmpHeight);
- MessageBox(tmpStr1);
- if(tmpWidth>1000 || tmpHeight >1000)
- {
- MessageBox("too huge bmp file to deal");
- return;
- }
- p=pByte+10;
- int bmpflg;
- bmpflg= *p + (*(p+1)) *256 + (*(p+2)) *256*256 + (*(p+3)) *256*256*256 ;
- p=pByte+bmpflg;
- int i,j,k;
- for(i=tmpHeight-1;i>=0;i--)
- for(j=0;j<tmpWidth;j++)
- for(k=0;k<3;k++)
- {
- Buffer[j][i][k]=*p;
- p++;
- }
- m_Width=tmpWidth;
- m_Height=tmpHeight;
- }
- void CReconizeDlg::GetGrayBmp()
- {
- // TODO: Add your command handler code here
- //二值化。
- int i,j;
- // x=0.30*red+0.59*green+0.11*blue;
- for(i=0;i<m_Width;i++)
- for(j=0;j<m_Height;j++)
- Buffer1[i][j]=(BYTE)( 0.11*Buffer[i][j][0]+0.59*Buffer[i][j][1]+0.30*Buffer[i][j][2] );
- }
- void CReconizeDlg::OnToGray()
- {
- // TODO: Add your control notification handler code here
- int i,j;
- GetGrayBmp();
- CClientDC dc(this);
- for(i=0;i<m_Width;i++) //rgb
- for(j=0;j<m_Height;j++)
- dc.SetPixel(i+1,j+1,RGB(Buffer1[i][j],Buffer1[i][j],Buffer1[i][j]) );
- }
- void CReconizeDlg::OnFileExit()
- {
- // TODO: Add your control notification handler code here
- exit(0);
- }
- void CReconizeDlg::OnWhiteBlack()
- {
- // TODO: Add your control notification handler code here
- DrawGetregion();
- EraseDot2();
- DrawRegion();
- }
- void CReconizeDlg::DrawGetregion() //计算出二值图
- {
- // TODO: Add your command handler code here
- int i,j;
- int defvalue=15;//阈值点
- for(i=0;i<m_Width;i++)
- for(j=0;j<m_Height;j++)
- BufferReg[i][j]=0;
- for(i=0;i<m_Height;i++)
- for(j=0;j<m_Width-1;j++)
- {
- if( abs(Buffer1[j][i]-Buffer1[j+1][i])>defvalue )
- BufferReg[j][i]=255;
- }
-
- for(i=0;i<m_Width;i++)
- for(j=0;j<m_Height-1;j++)
- {
- if( abs(Buffer1[i][j]-Buffer1[i][j+1])>defvalue )
- BufferReg[i][j]=255;
- }
- }
- void CReconizeDlg::DrawRegion() //绘出二值图
- {
- // TODO: Add your command handler code here
- int i,j;
- CClientDC dc(this);
- for(i=0;i<m_Width;i++) //rgb
- for(j=0;j<m_Height;j++)
- dc.SetPixel(i+1,j+1,RGB( BufferReg[i][j],
- BufferReg[i][j],
- BufferReg[i][j] ) );
- }///////////////////////////////////////////////////////////
- void CReconizeDlg::EraseDot()
- { //m_Width,m_Height
- int i,j;
- for(i=1;i<m_Width-1;i++)
- for(j=1;j<m_Height-1;j++)
- {
- if( (BufferReg[i][j]!=BufferReg[i-1][j-1]) && (BufferReg[i][j]!=BufferReg[i][j-1])
- && (BufferReg[i][j]!=BufferReg[i+1][j-1]) && (BufferReg[i][j]!=BufferReg[i-1][j])
- && (BufferReg[i][j]!=BufferReg[i+1][j]) && (BufferReg[i][j]!=BufferReg[i-1][j+1])
- &&(BufferReg[i][j]!=BufferReg[i][j+1]) && (BufferReg[i][j]!=BufferReg[i+1][j+1])
- )
- BufferReg[i][j]=255-BufferReg[i][j];
- }///////////////////////////////////////
- }
- void CReconizeDlg::EraseDot2()
- {
- EraseDot();
- int i,j;
- for(i=4;i<m_Width-4;i++)
- for(j=4;j<m_Height-4;j++)
- {
- if( BufferReg[i-1][j-1]==BufferReg[i][j-1]
- && BufferReg[i-1][j-1]==BufferReg[i+1][j-1]
- && BufferReg[i-1][j-1]==BufferReg[i+2][j-1]
- && BufferReg[i-1][j-1]==BufferReg[i-1][j]
- && BufferReg[i-1][j-1]==BufferReg[i+2][j]
- && BufferReg[i-1][j-1]==BufferReg[i-1][j+1]
- && BufferReg[i-1][j-1]==BufferReg[i+2][j+1]
- && BufferReg[i-1][j-1]==BufferReg[i-1][j+2]
- && BufferReg[i-1][j-1]==BufferReg[i][j+2]
- && BufferReg[i-1][j-1]==BufferReg[i+1][j+2]
- && BufferReg[i-1][j-1]==BufferReg[i+2][j+2]
- )
- {
- BufferReg[i][j]=BufferReg[i-1][j-1];
- BufferReg[i+1][j]=BufferReg[i-1][j-1];
- BufferReg[i][j+1]=BufferReg[i-1][j-1];
- BufferReg[i+1][j+1]=BufferReg[i-1][j-1];
- }
-
- }
- }
- void CReconizeDlg::Getpts()
- {
- // TODO: Add your command handler code here
- int i,j,k,l;
- int flag1,flag2,flag3,flag4;
- int iflag;
- //初始化
- index1=0; index2=0; index3=0; index4=0;
- for(i=0;i<10000;i++)
- for(j=0;j<3;j++)
- {
- PointLT[i][j]=-1;
- PointRT[i][j]=-1;
- PointLB[i][j]=-1;
- PointRB[i][j]=-1;
- }
- int step=2;
- int longOffset=30;
- int shortOffset=2;
- int whitepercent=48;
- ///////////////////////////////////////////////////////////////////////
- //求左上角
- flag1=0; flag2=0; flag3=0; flag4=0;
- iflag=0; index1=0;
- for(i=longOffset;i<m_Width-longOffset;i++)
- {
- for(j=longOffset;j<m_Height-longOffset;j++)
- {
- for(k=1;k<=longOffset;k++)
- for(l=1;l<=shortOffset;l++)
- {
- if(BufferReg[i+k][j-l]==255)
- flag1+=1;
- }
- for(k=1;k<=longOffset;k++)
- for(l=1;l<=shortOffset;l++)
- {
- if(BufferReg[i+k][j+l]==255)
- flag2+=1;
- }
- for(k=1;k<=shortOffset;k++)
- for(l=1;l<=longOffset;l++)
- {
- if(BufferReg[i+k][j+l]==255)
- flag3+=1;
- }
- for(k=1;k<=shortOffset;k++)
- for(l=1;l<=longOffset;l++)
- {
- if(BufferReg[i-k][j+l]==255)
- flag4+=1;
- }
- if( (flag2>=whitepercent) && (flag3>=whitepercent) )
- {
- iflag=1;
- j+=step;
- PointLT[index1][0]=i;
- PointLT[index1][1]=j;
- PointLT[index1][2]=1;
- index1++;
- }
- /////////////////////////////////////////////////
- flag1=0; flag2=0; flag3=0; flag4=0;
- //////////////////////////////////////////////////
- }
- if(iflag==1)
- {
- i+=step;
- iflag=0;
- }
- }
- ////////////////////////////////////////////////////////////
- //求右上角
- flag1=0; flag2=0; flag3=0; flag4=0;
- iflag=0; index2=0;
- for(i=longOffset;i<m_Width-longOffset;i++)
- {
- for(j=longOffset;j<m_Height-longOffset;j++)
- {
- for(k=1;k<=longOffset;k++)
- for(l=1;l<=shortOffset;l++)
- {
- if(BufferReg[i-k][j-l]==255)
- flag1+=1;
- }
- for(k=1;k<=longOffset;k++)
- for(l=1;l<=shortOffset;l++)
- {
- if(BufferReg[i-k][j+l]==255)
- flag2+=1;
- }
- for(k=1;k<=shortOffset;k++)
- for(l=1;l<=longOffset;l++)
- {
- if(BufferReg[i-k][j+l]==255)
- flag3+=1;
- }
- for(k=1;k<=shortOffset;k++)
- for(l=1;l<=longOffset;l++)
- {
- if(BufferReg[i+k][j+l]==255)
- flag4+=1;
- }
- if( (flag2>=whitepercent) && (flag3>=whitepercent) )
- {
- iflag=1;
- j+=step;
- PointRT[index2][0]=i;
- PointRT[index2][1]=j;
- PointRT[index2][2]=1;
- index2++;
- }
- /////////////////////////////////////////////////
- flag1=0; flag2=0; flag3=0; flag4=0;
- //////////////////////////////////////////////////
- }
- if(iflag==1)
- {
- i+=step;
- iflag=0;
- }
- }
- /////////////////////////////////////////////////
- //求右下角
- flag1=0; flag2=0; flag3=0; flag4=0;
- iflag=0; index3=0;
- for(i=longOffset;i<m_Width-longOffset;i++)
- {
- for(j=longOffset;j<m_Height-longOffset;j++)
- {
- for(k=1;k<=longOffset;k++)
- for(l=1;l<=shortOffset;l++)
- {
- if(BufferReg[i-k][j+l]==255)
- flag1+=1;
- }
- for(k=1;k<=longOffset;k++)
- for(l=1;l<=shortOffset;l++)
- {
- if(BufferReg[i-k][j-l]==255)
- flag2+=1;
- }
- for(k=1;k<=shortOffset;k++)
- for(l=1;l<=longOffset;l++)
- {
- if(BufferReg[i-k][j-l]==255)
- flag3+=1;
- }
- for(k=1;k<=shortOffset;k++)
- for(l=1;l<=longOffset;l++)
- {
- if(BufferReg[i+k][j-l]==255)
- flag4+=1;
- }
- if( (flag2>=whitepercent) && (flag3>=whitepercent) )
- {
- iflag=1;
- j+=step;
- PointRB[index3][0]=i;
- PointRB[index3][1]=j;
- PointRB[index3][2]=1;
- index3++;
- }
- /////////////////////////////////////////////////
- flag1=0; flag2=0; flag3=0; flag4=0;
- //////////////////////////////////////////////////
- }
- if(iflag==1)
- {
- i+=step;
- iflag=0;
- }
- }
- ///////////////////////////////////////////////////////////
- //求左下角
- flag1=0; flag2=0; flag3=0; flag4=0;
- iflag=0; index4=0;
- for(i=longOffset;i<m_Width-longOffset;i++)
- {
- for(j=longOffset;j<m_Height-longOffset;j++)
- {
- for(k=1;k<=longOffset;k++)
- for(l=1;l<=shortOffset;l++)
- {
- if(BufferReg[i+k][j+l]==255)
- flag1+=1;
- }
- for(k=1;k<=longOffset;k++)
- for(l=1;l<=shortOffset;l++)
- {
- if(BufferReg[i+k][j-l]==255)
- flag2+=1;
- }
- for(k=1;k<=shortOffset;k++)
- for(l=1;l<=longOffset;l++)
- {
- if(BufferReg[i+k][j-l]==255)
- flag3+=1;
- }
- for(k=1;k<=shortOffset;k++)
- for(l=1;l<=longOffset;l++)
- {
- if(BufferReg[i-k][j-l]==255)
- flag4+=1;
- }
- if( (flag2>=whitepercent) && (flag3>=whitepercent) )
- {
- iflag=1;
- j+=step;
- PointLB[index4][0]=i;
- PointLB[index4][1]=j;
- PointLB[index4][2]=1;
- index4++;
- }
- /////////////////////////////////////////////////
- flag1=0; flag2=0; flag3=0; flag4=0;
- //////////////////////////////////////////////////
- }
- if(iflag==1)
- {
- i+=step;
- iflag=0;
- }
- }
- /////////////////////////////////////////////////////////////////////
- }
- void CReconizeDlg::OnDrawEdge() //求出满足要求的四个顶点,并且绘制出来
- {
- // TODO: Add your command handler code here
- int i,j,k;
- Getpts() ;
- /////// 绘出左上角
- //CString tmpStr1;
- //tmpStr1.Format("左上角已求出:%d",index1);
- //MessageBox(tmpStr1);
- CClientDC dc(this);
- DrawRegion();
- for(i=0;i<index1;i++)
- {
- for(j=-1;j<=1;j++)
- for(k=-1;k<=1;k++)
- {
- dc.SetPixel(PointLT[i][0]+j,PointLT[i][1]+k,RGB(255,0,0) );
- }
- }
- /////// 绘出右上角
- ///tmpStr1.Format("右上角已求出: %d",index2);
- //MessageBox(tmpStr1);
- DrawRegion();
- for(i=0;i<index2;i++)
- {
- for(j=-1;j<=1;j++)
- for(k=-1;k<=1;k++)
- {
- dc.SetPixel(PointRT[i][0]+j,PointRT[i][1]+k,RGB(255,0,0) );
- }
- }
- /////// 绘出右下角
- //tmpStr1.Format("右下角已求出: %d",index3);
- //MessageBox(tmpStr1);
- DrawRegion();
- for(i=0;i<index3;i++)
- {
- for(j=-1;j<=1;j++)
- for(k=-1;k<=1;k++)
- {
- dc.SetPixel(PointRB[i][0]+j,PointRB[i][1]+k,RGB(255,0,0) );
- }
- }
- /////// 绘出左下角
- //tmpStr1.Format("左下角已求出: %d",index4);
- //MessageBox(tmpStr1);
- DrawRegion();
- for(i=0;i<index4;i++)
- {
- for(j=-1;j<=1;j++)
- for(k=-1;k<=1;k++)
- {
- dc.SetPixel(PointLB[i][0]+j,PointLB[i][1]+k,RGB(255,0,0) );
- }
- }
- ////////////////////////////////////////////////
- }
- void CReconizeDlg::OnDrawRect()
- {
- // TODO: Add your control notification handler code here
- DrawAvgrect();
-
- }
- void CReconizeDlg::OnQuickReconize()
- {
- // TODO: Add your control notification handler code here
- OnToGray(); //求出灰度图
- OnWhiteBlack();//求出二值图
- OnDrawEdge();//描点
- OnGetRect() ;//求矩形
- OnDrawRect(); //求出平均矩形
- }
- void CReconizeDlg::DrawRect() //绘制矩形。
- {
- // TODO: Add your command handler code here
- MatchRect2();
- //CString tmpStr1;
- //tmpStr1.Format("acceptable rects: %d",indexRect);
- //MessageBox(tmpStr1);
- CClientDC dc(this);
- //OnDrawOrig();
- int i;
- for(i=0;i<indexRect;i++)
- {
- dc.MoveTo( PointLT[Rect[i][0]][0] , PointLT[Rect[i][0]][1] );
- dc.LineTo( PointRT[Rect[i][1]][0] , PointRT[Rect[i][1]][1] );
- dc.MoveTo( PointRT[Rect[i][1]][0] , PointRT[Rect[i][1]][1] );
- dc.LineTo( PointRB[Rect[i][2]][0] , PointRB[Rect[i][2]][1] );
- dc.MoveTo( PointRB[Rect[i][2]][0] , PointRB[Rect[i][2]][1] );
- dc.LineTo( PointLB[Rect[i][3]][0] , PointLB[Rect[i][3]][1] );
- dc.MoveTo( PointLB[Rect[i][3]][0] , PointLB[Rect[i][3]][1] );
- dc.LineTo( PointLT[Rect[i][0]][0] , PointLT[Rect[i][0]][1] );
- }
- }
- void CReconizeDlg::MatchRect2()
- {
- indexRect=0;
- int i,j,k,l;
- for(i=0;i<50000;i++)
- for(j=0;j<4;j++)
- Rect[i][j]=-1;
- ////////////i PointLT ; j PointRT ; k PointRB ; l PointLB
- double x,y,rate, x1,y1,rate1;
- for(i=0;i<index1;i++)
- {
- Rect[indexRect][0]=i;
- for(j=0;j<index2;j++)
- {
- if( PointRT[j][0]>(PointLT[i][0]+80) && abs(PointRT[j][1]-PointLT[i][1])<10 )//符合要求的右上点
- {
- for(k=0;k<index3;k++)
- {
- if( abs(PointRB[k][0]-PointRT[j][0])<10 && PointRB[k][1]>(PointRT[j][1]+40) )//符合要求的右下点
- {
- for(l=0;l<index4;l++)
- {
- if( PointLB[l][0]<PointRB[k][0]-80 && abs(PointLB[l][1]-PointRB[k][1])<10
- && abs(PointLB[l][0]-PointLT[i][0])<10 && PointLB[l][1]>PointLT[i][1]+40 )//符合要求的左下点
- {
- x = PointRT[j][0]-PointLT[i][0];
- y = PointRB[k][1]-PointRT[j][1]+0.001;
- x1= PointRB[k][0]-PointLB[l][0];
- y1= PointLB[l][1]-PointLT[i][1]+0.001;
- rate = x/y;
- rate1= x1/y1;
- if(rate>=2.7 && rate<=3.3 && rate1>=2.7 && rate1<=3.3)
- {
- Rect[indexRect][0]=i;
- Rect[indexRect][1]=j;
- Rect[indexRect][2]=k;
- Rect[indexRect][3]=l;
- indexRect++;
- }
- }
- }//for(l=0;l<index4;l++)
- }
- }//for(k=0;k<index3;k++)
- }
- }//for(j=0;j<index2;j++)
- }//for(i=0;i<index1;i++)
- }
- void CReconizeDlg::DrawAvgrect() //求出平均矩形。
- {
- // TODO: Add your command handler code here
- AvgRect();
- DrawBMP();
-
-
- CClientDC dc(this);
- CPen p(PS_SOLID,1,RGB(255,0,0));
- dc.SelectObject(p);
- DrawBMP();
- int i;
- for(i=0;i<avgindexRect;i++)
- {
- dc.MoveTo( avgPointLT[i][0] , avgPointLT[i][1] );
- dc.LineTo( avgPointRT[i][0] , avgPointRT[i][1] );
- dc.MoveTo( avgPointRT[i][0] , avgPointRT[i][1] );
- dc.LineTo( avgPointRB[i][0] , avgPointRB[i][1] );
-
- dc.MoveTo( avgPointRB[i][0] , avgPointRB[i][1] );
- dc.LineTo( avgPointLB[i][0] , avgPointLB[i][1] );
-
- dc.MoveTo( avgPointLB[i][0] , avgPointLB[i][1] );
- dc.LineTo( avgPointLT[i][0] , avgPointLT[i][1] );
- }
- }
- bool CReconizeDlg::MatchRectPoint(int i,int j)
- { int Distance,k; //两个矩形的顶点相距参数
- bool flag=true;
- Distance=25;
- for(k=0;flag&&k<2;k++)
- if(abs(PointLT[Rect[i][0]][k]-PointLT[Rect[j][0]][k])<=Distance
- &&abs(PointRT[Rect[i][1]][k]-PointRT[Rect[j][1]][k])<=Distance
- &&abs(PointRB[Rect[i][2]][k]-PointRB[Rect[j][2]][k])<=Distance
- &&abs(PointLB[Rect[i][3]][k]-PointLB[Rect[j][3]][k])<=Distance
- )
- flag=true;
- else flag=false;
- return(flag);
- }
- void CReconizeDlg::AvgRect()
- {
- bool AlreadyRect[50000];
- int i,j,k,count,tempLT[2],tempRT[2],tempRB[2],tempLB[2];
- avgindexRect=0;
- for(i=0;i<2;i++)
- {
- tempLT[i]=0;
- tempLB[i]=0;
- tempRT[i]=0;
- tempRB[i]=0;
- }
- for( i=0;i<50000;i++)
- {
- AlreadyRect[i]=false;
- }
-
- for( i=0;i<indexRect;i++)
- {
- if(!AlreadyRect[i])
- {
- count=1;
- for(k=0;k<2;k++)
- {
- tempLT[k]=PointLT[Rect[i][0]][k];
- tempLB[k]=PointLB[Rect[i][3]][k];
- tempRT[k]=PointRT[Rect[i][1]][k];
- tempRB[k]=PointRB[Rect[i][2]][k];
- }
- for(j=i+1;j<indexRect;j++)
- {
- if(!AlreadyRect[j])
- if(MatchRectPoint(i,j))
- {
- AlreadyRect[i]=true;
- AlreadyRect[j]=true;
- count++;
- for(k=0;k<2;k++)
- {
- tempLT[k]+=PointLT[Rect[j][0]][k];
- tempLB[k]+=PointLB[Rect[j][3]][k];
- tempRT[k]+=PointRT[Rect[j][1]][k];
- tempRB[k]+=PointRB[Rect[j][2]][k];
- }
- }
- }
- for(k=0;k<2;k++)
- {
- avgPointLT[avgindexRect][k]=int(tempLT[k]/(count*1.0));
- avgPointRT[avgindexRect][k]=int(tempRT[k]/(count*1.0));
- avgPointRB[avgindexRect][k]=int(tempRB[k]/(count*1.0));
- avgPointLB[avgindexRect][k]=int(tempLB[k]/(count*1.0));
- }
- avgindexRect++;
- }
- }
- }
- void CReconizeDlg::JumpCal(int index)
- {
- int point[6][2];
- point[0][0]= (avgPointLT[index][0] + avgPointLB[index][0])/2;
- point[2][0]= point[0][0];
- point[4][0]= point[0][0];
- point[1][0]= (avgPointRT[index][0] + avgPointRB[index][0])/2;
- point[3][0]= point[1][0];
- point[5][0]= point[1][0];
-
- point[0][1]= int(( avgPointLT[index][1]*0.75+avgPointLB[index][1]*0.25
- +avgPointRT[index][1]*0.75+avgPointRB[index][1]*0.25 )/2);
- point[1][1]=point[0][1];
- point[2][1]= int(( avgPointLT[index][1]*0.5+avgPointLB[index][1]*0.5
- +avgPointRT[index][1]*0.5+avgPointRB[index][1]*0.5 )/2);
- point[3][1]=point[2][1];
- point[4][1]= int(( avgPointLT[index][1]*0.25+avgPointLB[index][1]*0.75
- +avgPointRT[index][1]*0.25+avgPointRB[index][1]*0.75 )/2);
- point[5][1]=point[4][1];
- int jump[3],i;
- for(i=0;i<3;i++)
- jump[i]=0;
- int length=point[1][0]-point[0][0]-1;
- int BufLine[800],j;
- float sumHd;
- int avgHd;
- int prePt,diffhappen;
- for(i=0;i<3;i++)
- {
- sumHd=0;
- for(j=0;j<length;j++)
- {
- BufLine[j]=Buffer1[ point[i*2][0]+j+1 ][point[i*2][1]];
- sumHd+=BufLine[j];
- }
- avgHd=(int)(sumHd/length);
-
- for(j=0;j<length;j++)
- if(BufLine[j]> avgHd)
- BufLine[j]=1;
- else
- BufLine[j]=0;
- prePt=0;
- diffhappen=0;
- for(j=1;j<length;j++)
- {
- if(diffhappen==1)//是否j之前一点已经与prePt不同
- {
- if( (j-prePt)>=(length/9) && BufLine[j]==BufLine[prePt] )
- {
- jump[i]++;
- prePt=j;
- diffhappen=0;
- }
- }
- else
- if(BufLine[j]!=BufLine[prePt])
- diffhappen=1;
- }
- }
-
- int enoughjump=7;
- if(jump[0]>=enoughjump && jump[1]>=enoughjump && jump[2]>=enoughjump)
- {
- HaveJumpRectLT[JumpindexRect][0]=avgPointLT[index][0];
- HaveJumpRectLT[JumpindexRect][1]=avgPointLT[index][1];
- HaveJumpRectRT[JumpindexRect][0]=avgPointRT[index][0];
- HaveJumpRectRT[JumpindexRect][1]=avgPointRT[index][1];
-
- HaveJumpRectRB[JumpindexRect][0]=avgPointRB[index][0];
- HaveJumpRectRB[JumpindexRect][1]=avgPointRB[index][1];
-
- HaveJumpRectLB[JumpindexRect][0]=avgPointLB[index][0];
- HaveJumpRectLB[JumpindexRect][1]=avgPointLB[index][1];
- JumpindexRect++;
- }
- }
- int CReconizeDlg::PickColor(int index)
- {
- int i,j,blueNum,whiteNum,blackNum,yellowNum,allNum;
- blueNum=0;
- blackNum=0;
- yellowNum=0;
- whiteNum=0;
- for(i= (HaveJumpRectLT[index][0]+HaveJumpRectLB[index][0]) /2 ;
- i< (HaveJumpRectRT[index][0]+HaveJumpRectRB[index][0]) /2;
- i++)
- for(j=(HaveJumpRectLT[index][1]+HaveJumpRectRT[index][1])/2;
- j=(HaveJumpRectLB[index][1]+HaveJumpRectRB[index][1])/2;
- j++)
- {
- if(2*Buffer[i][j][0]-Buffer[i][j][1]-Buffer[i][j][2]>0)
- blueNum++;
- if((Buffer[i][j][0]>=200)
- &&(Buffer[i][j][0]>=200)
- &&(Buffer[i][j][0]>=200)
- &&(abs(Buffer[i][j][0]-Buffer[i][j][1])<25)
- &&(abs(Buffer[i][j][0]-Buffer[i][j][2])<25)
- &&(abs(Buffer[i][j][1]-Buffer[i][j][2])<25)
- )
- whiteNum++;
- if( (Buffer[i][j][2]+Buffer[i][j][1]-2*Buffer[i][j][0])>0
- && ( abs(Buffer[i][j][2]-Buffer[i][j][1])<20) )
- yellowNum++;
- if( (Buffer[i][j][0]<=55)
- &&(Buffer[i][j][0]<=55)
- &&(Buffer[i][j][0]>=55)
- &&(abs(Buffer[i][j][0]-Buffer[i][j][1])<25)
- &&(abs(Buffer[i][j][0]-Buffer[i][j][2])<25)
- &&(abs(Buffer[i][j][1]-Buffer[i][j][2])<25)
- )
- blackNum++;
- }
- allNum=i*j;
- if( (blueNum/ (1.0*allNum) >0.5) && (whiteNum/(1.0*allNum)>0.1) )
- return 1;
- if((yellowNum/ (1.0*allNum) >0.5) && (blackNum/(1.0*allNum)>0.1))
- return 2;
- if((blackNum/ (1.0*allNum) >0.5) && (whiteNum/(1.0*allNum)>0.1))
- return 3;
- if((whiteNum/ (1.0*allNum) >0.5) && (blackNum/(1.0*allNum)>0.1))
- return 4;
- return 5;
- }
- void CReconizeDlg::OnGetRect()
- {
- // TODO: Add your control notification handler code here
- DrawRect();
- }