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

📄 dipview.cpp

📁 使用OpenCV和C++实现的车牌识别系统
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// dipView.cpp : implementation of the CDipView class
//

#include "stdafx.h"
#include "dip.h"

#include "dipDoc.h"
#include "dipView.h"
#include "afxdlgs.h"
#include "afxwin.h"
#include "math.h"
#include ".\dipview.h"
#include "stdlib.h"
#include "malloc.h"
#include "mainfrm.h"
#include "charview.h"
#include "recvchar.h"

#include "training.h"


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

/////////////////////////////////////////////////////////////////////////////
// CDipView

IMPLEMENT_DYNCREATE(CDipView, CView)

BEGIN_MESSAGE_MAP(CDipView, CView)
	//{{AFX_MSG_MAP(CDipView)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_WM_PAINT()
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	ON_COMMAND(ID_reopen, Onreopen)
	ON_WM_MOUSEMOVE()
	ON_WM_CREATE()
	ON_COMMAND(ID_LINE, OnLine)	
	ON_COMMAND(ID_F3, Onfse)
	ON_COMMAND(ID_F4, Onfz)
	ON_COMMAND(ID_MoHu, OnMoHu)
	ON_COMMAND(ID_ForHandle, OnForHandle)
	ON_COMMAND(ID_HSearch, OnHSearch)
	ON_COMMAND(ID_Banlance, OnBanlance)
	ON_COMMAND(ID_SubVertical, OnSubVertical)
	ON_COMMAND(ID_SubHorizontal, OnSubHorizontal)
	ON_COMMAND(ID_MidValue, OnMidValue)
	ON_COMMAND(ID_Average, OnAverage)
	ON_COMMAND(ID_Bool, OnBool)
	ON_COMMAND(ID_Choose, OnChoose)
	ON_COMMAND(ID_TRAINFROMVEHICLE, OnTrainfromvehicle)
	ON_COMMAND(ID_SAVESA, OnSave)
	ON_COMMAND(ID_READSA, OnLoad)
	ON_COMMAND(ID_CHANGE, OnChange)
	ON_COMMAND(ID_TRAIN, OnTrain)
	ON_COMMAND(ID_SAVEBP, OnSavebp)
	ON_COMMAND(ID_VSearch, OnVSearch)
	ON_COMMAND(ID_Locate, OnLocate)
	ON_COMMAND(ID_Sobel, OnSobel)
	ON_COMMAND(ID_VSplit, OnVSplit)
	ON_COMMAND(ID_CharacterRec, OnCharacterRecg)
	ON_COMMAND(ID_READBP, OnReadbp)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDipView construction/destruction

CDipView::CDipView()
//: Height(NULL)
//, TopRow(NULL)
//: Widths(NULL)
//, Left(NULL)
//: Rect_X(NULL)
//, Rect_Y(NULL)
//, Rect_W(NULL)
//, Rect_H(NULL)
//, Rect_Areas(0)
{
	// TODO: add construction code here
	//初始化
	m_pMemDC=new CDC;		//分配内存DC
	m_pBitmap=new CBitmap;	//分配背景图像的空间
	IsNewFile=true;			
	IsNewBitmap=true;
	m_pBmp=NULL;
	Height=new int[20];
	TopRow=new int[20];	
	Widths=new int[30];                     //Top' width
	Left=new int[30];                   //Top' top col
	Rect_X=new int[50];
    Rect_Y=new int[50]; 
	Rect_W=new int[50];
    Rect_H=new int[50];
	Rect_Areas=0;
	cRects=new crect[10];
	CharacterNum=0;
	cDatas=new cData[10];        //实际大小由CharacterNum确定
	char_ConnectPos=NULL;
	num_ConnectPos=NULL;
	han_ConnectPos=NULL;

}

CDipView::~CDipView()
{
	//释放内存
	delete m_pBitmap;
	delete m_pMemDC;
}

BOOL CDipView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDipView drawing

void CDipView::OnDraw(CDC* pDC)
{
	CDipDoc* pDoc = GetDocument();	
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	
	//如果已经读入一幅新的图像,则向CDC中写入新的图像信息
	if ((pDoc->ImgData!=NULL)&&IsNewFile)
	{
		//
	   ChageStatuBar(&"正在打开文件 ");
		//
		int i,j,k;
		k=0;
		BYTE r,g,b;
		for(i=0;i<pDoc->ImgHeight;i++)
			for(j=0;j<pDoc->ImgWidth;j++)
			{
				//从调色板中得到r,g,b分量
				r=pDoc->palette[pDoc->ImgData[k]].rgbBlue;	
				g=pDoc->palette[pDoc->ImgData[k]].rgbGreen;
				b=pDoc->palette[pDoc->ImgData[k]].rgbRed;
				pDC->SetPixel(j,i,(r<<16)|(g<<8)|b);
				k++;
			}
		IsNewFile=false;	//写完后,将文件标志位置为假
	    //恢复鼠标
		
		 
	    char * path =pDoc->FileName.GetBuffer(256) ;
		ChageStatuBar(path);
	}

       
}

/////////////////////////////////////////////////////////////////////////////
// CDipView printing

BOOL CDipView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CDipView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CDipView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CDipView diagnostics

#ifdef _DEBUG
void CDipView::AssertValid() const
{
	CView::AssertValid();
}
//setpentext
void CDipView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CDipDoc* CDipView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDipDoc)));
	return (CDipDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDipView message handlers

//打开一个BMP文件
void CDipView::OnFileOpen() 
{
	// TODO: Add your command handler code here
	CFileDialog mydlg(true);
	CString filename;
	mydlg.m_ofn.lpstrFilter="BMP files(*.bmp)\0*.bmp\0JPG files(*.jpg)\0*.jpg\0";

	//显示文件对话框
	if (mydlg.DoModal()==IDOK)
	{
		filename=mydlg.GetPathName();
		
		//读入文件
		CDipDoc * myDoc;
		myDoc=GetDocument();            //enable you to call the number
		                                //function of CDocment 
		myDoc->FileOpen(filename);
		
		//将文件标志位和图像标志位置为真
		IsNewFile=true;
		IsNewBitmap=true;

		//刷新屏幕
		myDoc->UpdateAllViews(NULL);
	}
}

void CDipView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	this->GetActiveWindow()->SetWindowText("数字图像处理程序");
	//设定从内存DC到屏幕DC的拷贝区域
	CRect rc(0,0,::GetSystemMetrics(SM_CXSCREEN),::GetSystemMetrics(SM_CYSCREEN));

	CBitmap * pOldBitmap=(CBitmap *)(m_pMemDC->SelectObject(m_pBitmap));

	if (IsNewBitmap)
		m_pMemDC->Rectangle(-1,-1,2000,2000);	//新图,清除背景
	else
		m_pMemDC->SelectObject(mymap);			//旧图,直接调用内存中的图像

	m_pMemDC->SelectClipRgn(NULL);
	OnDraw(m_pMemDC);							//向内存DC中画图像
	//拷贝到屏幕DC中
	dc.BitBlt(rc.left,rc.top,rc.Width(),rc.Height(),m_pMemDC,rc.left,rc.top,SRCCOPY);
		if (IsNewBitmap)
	{
		//保存历史图像,已备下次显示
		mymap=m_pMemDC->GetCurrentBitmap();
		IsNewBitmap=false;
	}

	m_pMemDC->SelectObject(pOldBitmap);

//	m_wait=theapp->LoadCursor(IDC_ARROW);
//	SetCursor(m_wait);
	// Do not call CView::OnPaint() for painting messages
}

void CDipView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
//	ChageStatuBar(&"请打开一个BMP格式的位图文件");	
	// TODO: Add your specialized code here and/or call the base class
	//初始化m_pMemDC和m_pBitmap
	if (m_pMemDC->GetSafeHdc()==NULL)
	{
		CClientDC dc(this);
		OnPrepareDC(&dc);
		//设定背景图像的大小
		CRect rc(0,0,::GetSystemMetrics(SM_CXSCREEN),::GetSystemMetrics(SM_CYSCREEN));
		m_pMemDC->CreateCompatibleDC(&dc);
		m_pBitmap->CreateCompatibleBitmap(&dc,rc.right,rc.bottom);
	}

	CDipDoc* pDoc=GetDocument();
	int i;
   for( i=0;i<pDoc->ImgWidth*pDoc->ImgHeight;i++)
	   img[i]=pDoc->ImgData[i];
	   
}



void CDipView::OnFileSave() 
{
	// TODO: Add your command handler code here
	CFileDialog mydlg(false);
	CString filename,promt;
	mydlg.m_ofn.lpstrFilter="BMP files(*.bmp)\0*.bmp\0\0";
	//打开存储对话框
	if (mydlg.DoModal()==IDOK)
	{
		filename=mydlg.GetPathName();
		if(filename.Find(".bmp")<=0)
			filename=filename+".bmp";
		
		//存储图像
		CDipDoc* pDoc = GetDocument();
		pDoc->FileSave(filename);
	}
}

//重新显示原始图像
void CDipView::Onreopen() 
{
	// TODO: Add your command handler code here
	CDipDoc* pDoc = GetDocument();
	pDoc->FileOpen();
	IsNewBitmap=true;
	IsNewFile=true;
	pDoc->UpdateAllViews(NULL);
}

void CDipView::OnMouseMove(UINT nFlags, CPoint point) 
{
		if (AfxGetApp() != NULL && AfxGetApp()->m_pMainWnd != NULL) {
		char buffer[256];
		CStatusBar* pStatus = (CStatusBar*) 
			AfxGetApp()->m_pMainWnd->GetDescendantWindow(AFX_IDW_STATUS_BAR);

		va_list argptr;	
		pStatus->SetPaneInfo(2,2,SBPS_STRETCH  ,7);       
		CString x;
		x.Format(_T("%d"),point.x); 

			CString y;
		y.Format(_T("%d"),point.y); 

		 CString zuobiao=" x= "+x + " , " + "y= " +y;
	char * fmt=zuobiao.GetBuffer(12);
		va_start(argptr, fmt);
		vsprintf(buffer, fmt, argptr);
		va_end(argptr);
       	if (pStatus != NULL)
		{
			pStatus->SetPaneText(2, buffer);
			pStatus->UpdateWindow();
		}
	}
	// TODO: Add your message handler code here and/or call default
	CView::OnMouseMove(nFlags, point);
}

void CDipView::ChageStatuBar(char * fmt)
{

	if (AfxGetApp() != NULL && AfxGetApp()->m_pMainWnd != NULL) {
		char buffer[256];
		CStatusBar* pStatus = (CStatusBar*) 
			AfxGetApp()->m_pMainWnd->GetDescendantWindow(AFX_IDW_STATUS_BAR);

		va_list argptr;
		va_start(argptr, fmt);
		vsprintf(buffer, fmt, argptr);
		va_end(argptr);
		if (pStatus != NULL) {
			pStatus->SetPaneText(0, buffer);
			pStatus->UpdateWindow();
		}
	}


}

//DEL BOOL CDipView::OnEraseBkgnd(CDC* pDC) 
//DEL {
//DEL 	// TODO: Add your message handler code here and/or call default
//DEL 	if (AfxGetApp() != NULL && AfxGetApp()->m_pMainWnd != NULL) {
//DEL 		char buffer[256];
//DEL 		CStatusBar* pStatus = (CStatusBar*) 
//DEL 			AfxGetApp()->m_pMainWnd->GetDescendantWindow(AFX_IDW_STATUS_BAR);
//DEL 
//DEL 		va_list argptr;
//DEL 	//	char * fmt =&" BMPBMPBMPBMP";
//DEL //gai kuandu
//DEL 		pStatus->SetPaneInfo(2,2,SBPS_STRETCH  ,7);
//DEL        //itoa(point.x,,10)
//DEL 	
//DEL 	char * fmt="请打开文件";
//DEL 		va_start(argptr, fmt);
//DEL 		vsprintf(buffer, fmt, argptr);
//DEL 		va_end(argptr);
//DEL        	if (pStatus != NULL)
//DEL 		{
//DEL 		//	pStatus
//DEL 			pStatus->SetPaneText(2, buffer);
//DEL 			pStatus->UpdateWindow();
//DEL 		}
//DEL 	}
//DEL 	return CView::OnEraseBkgnd(pDC);
//DEL }

int CDipView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	OnLoad();
	char_bp.Initialize(3434);
	char_bp.CreateBP(13,60,26);
	num_bp.Initialize(3434);
	num_bp.CreateBP(13,60,10);
	char_num_bp.Initialize(3434);
	char_num_bp.CreateBP(13,60,36);


// TODO: Add your specialized creation code here
//	ChageStatuBar(&"请打开一个BMP格式的位图文件");	
	return 0;
}

void CDipView::OnLine() 
{
	// TODO: Add your command handler code here
	CDipDoc* pDoc = GetDocument();	
	ASSERT_VALID(pDoc);
	Mdlg dlg;
	dlg.DoModal();
	a=dlg.m_edit1;
	b=dlg.m_edit2;
	for(int i=0;i<pDoc->ImgWidth*pDoc->ImgHeight;i++)
	{
		pDoc->ImgData[i]=a*pDoc->ImgData[i]+b;
		if(pDoc->ImgData[i]>255) pDoc->ImgData[i]=255;
		if(pDoc->ImgData[i]<0) pDoc->ImgData[i]=0;
	}

     IsNewFile=true;
     IsNewBitmap=true;
		 	
	Invalidate();


	
}

void CDipView::Onfse() 
{
	// TODO: Add your command handler code here
	CDipDoc* pDoc = GetDocument();	
	ASSERT_VALID(pDoc);
		for(int i=0;i<pDoc->ImgWidth*pDoc->ImgHeight;i++)
	{
		//pDoc->ImgData[i]=a*pDoc->ImgData[i]+b;
	  pDoc->ImgData[i]=255-pDoc->ImgData[i];
		//if(pDoc->ImgData[i]<0) pDoc->ImgData[i]=0;
	}
		 IsNewFile=true;
     IsNewBitmap=true;
		 	
	Invalidate();
	
}

void CDipView::Onfz() 
{
	// TODO: Add your command handler code here
		CDipDoc* pDoc = GetDocument();	

	ASSERT_VALID(pDoc);
	BYTE * ImgData1=new BYTE[pDoc->ImgWidth*pDoc->ImgHeight];
		for(int i=0;i<pDoc->ImgWidth*pDoc->ImgHeight;i++)
	{
		//pDoc->ImgData[i]=a*pDoc->ImgData[i]+b;
	  ImgData1[pDoc->ImgWidth*pDoc->ImgHeight-i]=pDoc->ImgData[i];
		//if(pDoc->ImgData[i]<0) pDoc->ImgData[i]=0;
	}
			for( i=0;i<pDoc->ImgWidth*pDoc->ImgHeight;i++)
	{
		//pDoc->ImgData[i]=a*pDoc->ImgData[i]+b;
			pDoc->ImgData[i]=ImgData1[i];
		//if(pDoc->ImgData[i]<0) pDoc->ImgData[i]=0;
	}
		 IsNewFile=true;
     IsNewBitmap=true;
		 	
	Invalidate();	
}

void CDipView::OnMoHu() 
{
	// TODO: Add your command handler code here
		CDipDoc* pDoc = GetDocument();
	int h=pDoc->ImgHeight;
	int w=pDoc->ImgWidth;
	BYTE * g=new BYTE[h*w];
	float temp;
	//标示图象的坐标
	int x,y;
	//标示算子的坐标
	int i,j;

	BYTE mb[3][3]={{-1,0,0},{0,0,0},{0,0,1}};
  
	for(x=1;x<h-1;x++)
	{
		for(y=1;y<w-1;y++)
		{
			temp=0;
		    for(i=0;i<3;i++)
	    	{
		    	for(j=0;j<3;j++)
				{
			    	temp=temp+(float( mb[i][j])*( float (pDoc->ImgData[(x-1+i)*w+(y-1+j)])));
				}				
			}		
			g[x*w+y]=int(temp);			
		}
	}
	for(x=1;x<h-1;x++)
		for(y=1;y<w-1;y++)
			pDoc->ImgData[x*w+y]=g[x*w+y];

	IsNewFile=true;
    IsNewBitmap=true;		 	
	Invalidate();	
}

void CDipView::OnForHandle() 
{
	// TODO: Add your command handler code here	
	 CDipDoc *p=GetDocument();
	 BYTE *temp=new BYTE[p->ImgHeight*p->ImgWidth];
	 int x,y;	

	for(x=0;x<p->ImgHeight;x++)
	{
		for(y=0;y<p->ImgWidth;y++)
		 {
			temp[(x*p->ImgWidth)+y]=p->ImgData[(x*p->ImgWidth)+y];
		}
	}

	 long double total=0;
	 float aver=0;
	 for(x=0;x<p->ImgHeight;x++)
	 {
		 for(y=0;y<p->ImgWidth;y++)
		 {	
			 total+=p->ImgData[x*p->ImgWidth+y];			
		 }
	 }
	 aver=(BYTE)(total/(float)(p->ImgHeight*p->ImgWidth));

	 float delt; 
	 float sub=0; 
	 for(x=0;x<p->ImgHeight;x++)
	 {
		 for(y=0;y<p->ImgWidth;y++)
		 {			
			 sub+=(p->ImgData[x*p->ImgWidth+y]-aver)*(p->ImgData[x*p->ImgWidth+y]-aver);			 
		 }
	 }
	 delt=(float)(sqrt(sub/(float)(p->ImgHeight*p->ImgWidth)));

	 BYTE judge;
	 judge=(BYTE)(delt+aver);
	 for(x=0;x<p->ImgHeight;x++)
	 {
		for(y=0;y<p->ImgWidth;y++)
		 {
			if(p->ImgData[(x*p->ImgWidth)+y]>=judge)
			{
				p->ImgData[(x*p->ImgWidth)+y]=255;
			}
			else
			{
				p->ImgData[(x*p->ImgWidth)+y]=0;
			}
		}
	}
	 
	 free(temp);
	 IsNewBitmap=true;
	 IsNewFile=true;
	 p->UpdateAllViews(NULL);
}

void CDipView::OnHSearch() 
{
	// TODO: Add your command handler code here
	CDipDoc *pDoc=GetDocument();

	int width,height;
	width=pDoc->ImgWidth;
	height=pDoc->ImgHeight;

	BYTE *g=new BYTE[width*height];

    int *Count= new int[height];
	int *TempCount=new int[height];
	int *maxChanges=new int[10];
	int *Rows=new int[10];                                  	
	
	int H[20];
	int Top[20];
	 
	for(int row=0;row<pDoc->ImgHeight;row++)
	 {
		for(int col=0;col<pDoc->ImgWidth;col++)
		{	
		   g[row*width+col]=pDoc->ImgData[row*width+col];			
		}
	 }
	 
	for(int c=0;c<height;c++)
	{
		Count[c]=0;
		TempCount[c]=0;
	}

	for(c=0;c<10;c++)

⌨️ 快捷键说明

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