车牌识别系统基于开发系统代码大全.doc
/ 图像字符识别毕业设计View.cpp : CMyView 类的实现/#include "stdafx.h"#include "图像字符识别毕业设计.h"#include "图像字符识别毕业设计Doc.h"#include "图像字符识别毕业设计View.h"#include ".图像字符识别毕业设计view.h"#ifdef _DEBUG#define new DEBUG_NEW#endif/ CMyViewIMPLEMENT_DYNCREATE(CMyView, CView)BEGIN_MESSAGE_MAP(CMyView, CView)/ 标准打印命令ON_COMMAND(ID_FILE_PRINT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CView:OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView:OnFilePrintPreview)ON_COMMAND(ID_FILE_OPEN, OnFileOpen)ON_COMMAND(ID_IMAGE_PARAMENT, OnImageParament)ON_COMMAND(ID_FIND_CARNUMBER, OnFindCarnumber)ON_COMMAND(ID_COLOR_CRAY, OnColorCray)ON_COMMAND(ID_MEDIAN5, OnMedian5)ON_COMMAND(ID_LOG_BORDDETEC, OnLogBorddetec)ON_COMMAND(ID_OTSU_THRESHOLD, OnOtsuThreshold)ON_COMMAND(ID_CATCH_NUMBERONCE, OnCatchNumberonce)ON_COMMAND(ID_Lap_Borddetec, OnLapBorddetec)ON_COMMAND(ID_CATCH_BNUMBER, OnCatchBnumber)ON_COMMAND(ID_GET_CARNUMBER, OnGetCarnumber)ON_COMMAND(ID_GET_HCARNUMBER, OnGetHcarnumber)ON_COMMAND(ID_NUMBER_CUT, OnNumberCut)ON_COMMAND(ID_ADD_ALL, OnAddAll)ON_COMMAND(ID_NUMBER_STUDY, OnNumberStudy)ON_COMMAND(ID_FIRSTNUMBER_RECOGNISE, OnFirstnumberRecognise)ON_COMMAND(ID_SECONDNUMBER_RECOGNISE, OnSecondnumberRecognise)ON_COMMAND(ID_THIRDNUMBER_RECOGNISE, OnThirdnumberRecognise)ON_COMMAND(ID_FOURTHNUMBER_RECOGNISE, OnFourthnumberRecognise)ON_COMMAND(ID_FIFTHNUMBER_RECOGNISE, OnFifthnumberRecognise)ON_COMMAND(ID_SISTHNUMBER_RECOGNISE, OnSisthnumberRecognise)ON_COMMAND(ID_SEVENTHNUMBER_RECOGNISE, OnSeventhnumberRecognise)ON_COMMAND(ID_ZIMU_STUDY, OnZimuStudy)ON_COMMAND(ID_HANZI_STUDY, OnHanziStudy)ON_COMMAND(ID_ZHIFUSHIBIE_ZONGHE, OnZhifushibieZonghe)END_MESSAGE_MAP()/ CMyView 构造/析构CMyView:CMyView()/ TODO: 在此处添加构造代码CMyView:CMyView()BOOL CMyView:PreCreateWindow(CREATESTRUCT& cs)/ TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或/ 样式return CView:PreCreateWindow(cs);/ CMyView 绘制void CMyView:OnDraw(CDC* pDC)CMyDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);if (!pDoc)return;/ TODO: 在此处为本机数据添加绘制代码if (!m_Image.IsNull()m_Image.Draw(pDC->m_hDC,0,0);/ CMyView 打印BOOL CMyView:OnPreparePrinting(CPrintInfo* pInfo)/ 默认准备return DoPreparePrinting(pInfo);void CMyView:OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: 打印前添加额外的初始化void CMyView:OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)/ TODO: 打印后添加清除过程/ CMyView 诊断#ifdef _DEBUGvoid CMyView:AssertValid() constCView:AssertValid();void CMyView:Dump(CDumpContext& dc) constCView:Dump(dc);CMyDoc* CMyView:GetDocument() const / 非调试版本是内联的ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc);return (CMyDoc*)m_pDocument;#endif /_DEBUG/ CMyView 消息处理程序void CMyView:OnFileOpen()/ TODO: 在此添加命令处理程序代码CString strFilter;CSimpleArray<GUID> aguidFileTypes;HRESULT hResult;/ 获取CImage支持的图像文件的过滤字符串hResult = m_Image.GetExporterFilterString(strFilter,aguidFileTypes,_T( "All Image Files") );if (FAILED(hResult) MessageBox("GetExporterFilter调用失败!");return;CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, strFilter);if(IDOK != dlg.DoModal() return;m_Image.Destroy();/ 将外部图像文件装载到CImage对象中hResult = m_Image.Load(dlg.GetFileName();if (FAILED(hResult) MessageBox("调用图像文件失败!");return;/ 设置主窗口标题栏内容CString str;str.LoadString(AFX_IDS_APP_TITLE);AfxGetMainWnd()->SetWindowText(str + " - " +dlg.GetFileName();Invalidate(); / 强制调用OnDrawvoid CMyView:OnImageParament()/ TODO: 在此添加命令处理程序代码CClientDC dc(this);HBRUSH hBrush;HPEN hPen;int i,x,y,k,nYChar;CString str;TEXTMETRIC tm;char *textbuf="像素行的存储顺序","像素宽度","像素高度","每像素位数","每行字节数"x=775;y=120;hPen=(HPEN) GetStockObject(WHITE_PEN);hBrush=(HBRUSH) GetStockObject(WHITE_BRUSH);dc.SelectObject(hPen);dc.SelectObject(hBrush);dc.GetTextMetrics(&tm);dc.SetTextColor(RGB(0,0,0);nYChar=(tm.tmHeight+tm.tmExternalLeading)*3/2;dc.TextOut(x+60,y-nYChar*3/2,"图像特征参数");for(i=0;i<5;i+)dc.TextOut (x,y+nYChar*i,textbufi);x+=175;k=m_Image.GetPitch();if(k<0)dc.TextOut (x,y+nYChar*0,"自下而上");elsedc.TextOut (x,y+nYChar*0,"自上而下");str.Format ("%d",abs(k);dc.TextOut (x,y+nYChar*4,str,lstrlen(str);str.Format ("%d",m_Image.GetWidth ();dc.TextOut (x,y+nYChar*1,str);str.Format ("%d",m_Image.GetHeight ();dc.TextOut (x,y+nYChar*2,str);str.Format ("%d",m_Image.GetBPP ();dc.TextOut (x,y+nYChar*3,str);if(m_Image.GetMaxColorTableEntries ()dc.TextOut (x-175,y+nYChar*6,"调色板单元数");k=m_Image.GetMaxColorTableEntries ();str.Format ("%d",k);dc.TextOut (x,y+nYChar*6,str);else dc.TextOut (x-175,y+nYChar*6,"真彩色图像,无调色板");void CMyView:OnFindCarnumber()/ TODO: 在此添加命令处理程序代码struct IMAGEPARAMENT intnWidth;intnHeight;intnBitCount; intnBytesPerLine;intnBytesPerPixel;intnNumColors;intnSize;void GetImageParament(CImage *pImg,struct IMAGEPARAMENT *ppImgParam)if (pImg->IsNull() return;ppImgParam->nWidth = pImg->GetWidth();ppImgParam->nHeight = pImg->GetHeight();ppImgParam->nBitCount = pImg->GetBPP();ppImgParam->nBytesPerLine = (pImg->GetWidth()*pImg->GetBPP()+31)/32*4;ppImgParam->nBytesPerPixel = pImg->GetBPP()/8;if (pImg->GetBPP()<=8) ppImgParam->nNumColors= 1 << pImg->GetBPP();else ppImgParam->nNumColors= 0;ppImgParam->nSize = ppImgParam->nBytesPerLine*ppImgParam->nHeight;void ImageCopy(CImage *pImgn,CImage *pImgm) structIMAGEPARAMENT P; RGBQUADColorTab256; CDC*pOrigDC,*pDC; GetImageParament(pImgm,&P); if (!pImgn->IsNull() pImgn->Destroy(); pImgn->Create(P.nWidth,P.nHeight,P.nBitCount,0); if (P.nNumColors>0) pImgm->GetColorTable(0,P.nNumColors,ColorTab);pImgn->SetColorTable(0,P.nNumColors,ColorTab); pOrigDC= CDC:FromHandle(pImgm->GetDC(); pDC = CDC:FromHandle(pImgn->GetDC(); pDC->BitBlt(0,0,P.nWidth,P.nHeight,pOrigDC,0,0,SRCCOPY); pImgm->ReleaseDC(); pImgn->ReleaseDC();void ColorToGray(CImage *pImgn,CImage *pImgm) inti, x, y;intgray256;BYTE*cbuf,*gbuf;RGBQUADColorTab256; CImagegpImg; CStringstr1; structIMAGEPARAMENT P;GetImageParament(pImgm,&P); if (P.nBitCount<8) return; gpImg.Create(P.nWidth,P.nHeight,8,0); for (i=0; i<256; i+)ColorTabi.rgbBlue = ColorTabi.rgbGreen = ColorTabi.rgbRed = i;gpImg.SetColorTable(0,256,ColorTab); if (pImgm->GetBPP()<=8) pImgm->GetColorTable(0,P.nNumColors,ColorTab);for (i=0; i<P.nNumColors; i+)grayi = (int) (0.11*ColorTabi.rgbBlue + 0.59*ColorTabi.rgbGreen + 0.30*ColorTabi.rgbRed);for (y=0; y<P.nHeight; y+) cbuf = (BYTE*) pImgm->GetPixelAddress(0,y); gbuf = (BYTE*) gpImg.GetPixelAddress(0,y);for (x=0; x<P.nWidth; x+) gbufx = (BYTE) graycbufx; else for (y=0; y<P.nHeight; y+) cbuf = (BYTE*) pImgm->GetPixelAddress(0,y); gbuf = (BYTE*) gpImg.GetPixelAddress(0,y);for (x=0,i=0; x<P.nWidth; x+,i+=P.nBytesPerPixel) gbufx = (BYTE) (0.11*cbufi + 0.59*cbufi+1 + 0.30*cbufi+2); ImageCopy(pImgn,&gpImg); gpImg.Destroy();void CMyView:OnColorCray()/ TODO: 在此添加命令处理程序代码CString str1;ColorToGray(&m_Image,&m_Image); str1.LoadString(AFX_IDS_APP_TITLE); AfxGetMainWnd()->SetWindowText(str1 + " - " +"灰度图");Invalidate();int MedValue(int *buf,int n,int m)int i,j,k,f;for(i=1;i<n;i+)for(j=n-1,f=0;j>=i;j-)if(bufj>bufj+1)k=bufj;bufj=bufj+1;bufj+1=k;f=1;if(f=0) break;return(bufm);int buff15;void Median5(BYTE *list0,BYTE*list1,int DX,int DY)int flag=5;for(int i=1;i<DY-1;i+)for(int j=1;j<DX-1;j+)buff10=list1i-1j;buff11=list1ij;buff12=list1i+1j;buff13=list1ij-1;buff14=list1ij+1;list0ij=(BYTE)MedValue(buff1,flag,flag/2);BYTE* Create2DList(CImage *pImg) structIMAGEPARAMENT P;inti;BYTE*list; GetImageParament(pImg,&P);list=(BYTE*)malloc(P.nHeight*sizeof(BYTE*); for (i=0;i<P.nHeight;i+) listi=(BYTE*) pImg->GetPixelAddress(0,i); return(list);void CMyView:OnMedian5()/ TODO: 在此添加命令处理程序代码struct IMAGEPARAMENT PP;BYTE *list1,*list0;ImageCopy(&m_Image1,&m_Image);list1=(BYTE *)Create2DList(&m_Image); list0=(BYTE *)Create2DList(&m_Image1); GetImageParament(&m_Image,&PP);Median5(list0,list1,PP.nWidth,PP.nHeight);ImageCopy(&m_Image,&m_Image1); CString str2; str2.LoadString(AFX_IDS_APP_TITLE); AfxGetMainWnd()->SetWindowText(str2 + " - " +"5点中值滤波");Invalidate();intMask33,Mask555,Scale,Offset; void WINAPI Template(BYTE *list0,BYTE *list1,int Dx,int Dy) int i,j,g; for (i=1;i<Dy-1;i+) for (j=1;j<Dx-1;j+) g = (Mask00*list1i-1j-1+Mask01*list1i-1j +Mask02*list1i-1j+1+Mask10*list1ij-1 +Mask11*list1ij +Mask12*list1ij+1 +Mask20*list1i+1j-1+Mask21*list1i+1j +Mask22*list1i+1j+1)/Scale+Offset; if (g>0xff) g=0xff; else if (g<0) g=0; list0ij = (BYTE) g; void WINAPI TemplateB(BYTE *list0,BYTE *list1,int Dx,int Dy,int t) / t x t 邻域处理共用程序 (t = 3 or 5)int i,j,g,m,n,*p; for (i=t/2;i<Dy-t/2;i+) for (j=t/2;j<Dx-t/2;j+) g=0;p=&Mask500;for (m=i-t/2;m<=i+t/2;m+) for (n=j-t/2;n<=j+t/2;n+) g+=(*p+)*list1mn; g=g/Scale+Offset;if (g>0xff) g=0xff;else if (g<0) g=0;list0ij = g; void WINAPI Laplacian(BYTE *list0,BYTE *list1,int Dx,int Dy)/拉普拉斯算法 Scale=1; Offset=1; Mask00=Mask02=Mask20=Mask22=-1; Mask01=Mask21=Mask10=Mask12=-1; Mask11=8; Template(list0,list1,Dx,Dy); Offset=0;void CMyView:OnLapBorddetec()/ TODO: 在此添加命令处理程序代码struct IMAGEPARAMENT PP;BYTE *list1,*list0;ImageCopy(&m_Image1,&m_Image);list1=(BYTE *)Create2DList(&m_Image); list0=(BYTE *)Create2DList(&m_Image1); GetImageParament(&m_Image,&PP);Laplacian(list0,list1,PP.nWidth,PP.nHeight); ImageCopy(&m_Image,&m_Image1); CString str2; str2.LoadString(AFX_IDS_APP_TITLE); AfxGetMainWnd()->SetWindowText(str2 + " - " +"laplacian变换");Invalidate();void WINAPI LoG(BYTE *list0,BYTE *list1,int Dx,int Dy) / 高斯型拉普拉斯算法int mask25= 0, 0,-1, 0, 0, 0,-1,-2,-1, 0, -1,-2,16,-2,-1, 0,-1,-2,-1, 0, 0, 0,-1, 0, 0; Scale=1;Offset=254; memcpy(Mask5,mask,25*sizeof(int); TemplateB(list0,list1,Dx,Dy,5); Offset=0;void CMyView:OnLogBorddetec()/ TODO: 在此添加命令处理程序代码struct IMAGEPARAMENT PP;BYTE *list1,*list0;ImageCopy(&m_Image1,&m_Image);list1=(BYTE *)Create2DList(&m_Image); list0=(BYTE *)Create2DList(&m_Image1); GetImageParament(&m_Image,&PP);LoG(list0,list1,PP.nWidth,PP.nHeight); ImageCopy(&m_Image,&m_Image1); CString str2; str2.LoadString(AFX_IDS_APP_TITLE); AfxGetMainWnd()->SetWindowText(str2 + " - " +"log变换");Invalidate();int MaxMin(double *tab,int flag) double max,min; int i,p,q; max=min=tab128; p=q=128; for (i=0;i<256;i+) if (tabi>max) max=tabi; p=i; if (tabi<min) min=tabi; q=i; if (flag=1) for (i=0;i<256;i+) tabi=120*(max-tabi)/(max-min); else for (i=0;i<256;i+) tabi=120*(tabi-min)/(max-min); p=q; return(p);void Release2DList(BYTE* list)free(list);void SetAllPalette(CImage *pImg, RGBQUAD *ColorTab) structIMAGEPARAMENT P; GetImageParament(pImg,&P);pImg->SetColorTable(0, P.nNumColors, ColorTab);void SetPalette(CImage *pImg,int n,int r,int g,int b)RGBQUAD ColorTab;ColorTab.rgbBlue = b; ColorTab.rgbGreen = g; ColorTab.rgbRed = r;pImg->SetColorTable(n, 1, &ColorTab);int WINAPI Otsu(long *pg) / 大津法取阈值 int i,j,p; double m0,m1,M0,M1,u,v,w256; M0=M1=0; for (i=0;i<256;i+) M0+=pgi; M1+=pgi*i; for (j=0;j<256;j+) m0=m1=0; for (i=0;i<=j;i+) m0+=pgi; m1+=pgi*i; if (m0) u=m1/m0; else u=0; if (M0-m0) v=(M1-m1)/(M0-m0); else v=0; wj=m0*(M0-m0)*(u-v)*(u-v); p=MaxMin(w,1); return(p);longpg256;void Threshold(CImage *pImgm,double ff) structIMAGEPARAMENT P;BYTE*list,*lpBuf;RGBQUADColorTab256; intx,y,thre;/int t;/inttab256,buf20,bn; GetImageParament(pImgm,&P); list=(BYTE*) Create2DList(pImgm);/histog(list,pg,0,0,P.nWidth,P.nHeight); thre=Otsu(pg); for (y=0;y<P.nHeight;y+) lpBuf=(BYTE*) pImgm->GetPixelAddress(0,y);for (x=0;x<P.nWidth;x+,lpBuf+) if (*lpBuf>=thre) *lpBuf=1;else *lpBuf=0; memset(ColorTab,0,1024);SetAllPalette(pImgm,ColorTab); SetPalette(pImgm,1,255,255,255);Release2DList(list);void CMyView:OnOtsuThreshold()/二值化响应函数/ TODO: 在此添加命令处理程序代码 Threshold(&m_Image,0);CString str2; str2.LoadString(AFX_IDS_APP_TITLE); AfxGetMainWnd()->SetWindowText(str2 + " - " +"OTSU法二值化");Invalidate();int InImage(CImage *pImg,int x,int y) structIMAGEPARAMENT P; GetImageParament(pImg,&P); if (x<0)|(y<0)|(x>=P.nWidth)|(y>=P.nHeight) return 0;else return 1;DWORD GetPixelValue(CImage *pImg,int x,int y) structIMAGEPARAMENT P;BYTE*lp; DWORDdd=0;GetImageParament(pImg,&P);if (P.nBitCount<8)|(!InImage(pImg,x,y) return 0;lp = (BYTE*) pImg->GetPixelAddress(x,y); memcpy(&dd,lp,P.nBytesPerPixel);return(dd);void SetPixelValue(CImage *pImg,int x,int y,DWORD c) structIMAGEPARAMENT P;BYTE*lp; GetImageParament(pImg,&P);if (P.nBitCount<8)|(!InImage(pImg,x,y) return;lp = (BYTE*) pImg->GetPixelAddr