📄 recimg.cpp
字号:
// RecImg.cpp: implementation of the CRecImg class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "RecImg.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CRecImg::CRecImg()
{
}
CRecImg::~CRecImg()
{
}
double CRecImg::m_dOutputListInt[]={0,1,2,3,4,5,6,7,8,9};
char CRecImg::m_cOutputListInt[]="0123456789";
CString CRecImg::m_strChinese[]={"浙","苏","沪"};
char CRecImg::RecognChar(CImage &ImgIn,double &dRecVal)
{
return m_cOutputListChar[m_BpNetChar.Recogn(ImgIn,dRecVal)];
}
char CRecImg::RecognA4(CImage &ImgIn,double &dRecVal)
{
return m_cOutputListA4[m_NetA4.Recogn(ImgIn,dRecVal)];
}
char CRecImg::Recogn7T(CImage &ImgIn,double &dRecVal)
{
return m_cOutputList7T[m_Net7T.Recogn(ImgIn,dRecVal)];
}
char CRecImg::RecognPR8(CImage &ImgIn,double &dRecVal)
{
return m_cOutputListPR8[m_NetPR8.Recogn(ImgIn,dRecVal)];
}
char CRecImg::RecognCDG0(CImage &ImgIn,double &dRecVal)
{
return m_cOutputListCDG0[m_NetCDG0.Recogn(ImgIn,dRecVal)];
}
void CRecImg::TrainCDG0(CString strPath)
{
TrainSimulateChar(strPath,m_cOutputListCDG0);
}
char CRecImg::RecognInt(CImage &ImgIn,double &dRecVal)
{
return m_cOutputListInt[m_BpNetInt.Recogn(ImgIn,dRecVal)];
}
char CRecImg::RecognInt1(CImage &ImgIn,double &dRecVal)
{
return m_cOutputListInt[m_BpNetInt1.Recogn1(ImgIn,dRecVal)];
}
BOOL CRecImg::LoadBpNetInt(CString strPath)
{
m_BpNetInt.LoadBpNet(strPath);
return TRUE;
}
BOOL CRecImg::LoadBpNetInt1(CString strPath)
{
m_BpNetInt1.LoadBpNet(strPath);
return TRUE;
}
BOOL CRecImg::LoadBpNetSimilar()
{
CString strPath;
strPath.Format("%sannCDG0.ann",GetAppPath());
m_NetCDG0.LoadBpNet(strPath);
strPath.Format("%sann7T.ann",GetAppPath());
m_Net7T.LoadBpNet(strPath);
strPath.Format("%sannA4.ann",GetAppPath());
m_NetA4.LoadBpNet(strPath);
strPath.Format("%sannPR89.ann",GetAppPath());
m_NetPR8.LoadBpNet(strPath);
return TRUE;
}
void CRecImg::TrainSimilar(CString strPath,int nType)
{
if (nType==SIMILAR_CDG0)
{
TrainSimulateChar(strPath,m_cOutputListCDG0);
}
else if (nType==SIMILAR_7T)
{
TrainSimulateChar(strPath,m_cOutputList7T);
}
else if (nType==SIMILAR_PR89)
{
TrainSimulateChar(strPath,m_cOutputListPR8);
}
else if (nType==SIMILAR_A4)
{
TrainSimulateChar(strPath,m_cOutputListA4);
}
}
char CRecImg::RecognSimilar(CImage &ImgIn,double &dRecVal,int nType)
{
if (nType==SIMILAR_CDG0)
{
return m_cOutputListCDG0[m_NetCDG0.Recogn(ImgIn,dRecVal)];
}
else if (nType==SIMILAR_7T)
{
return m_cOutputList7T[m_Net7T.Recogn(ImgIn,dRecVal)];
}
else if (nType==SIMILAR_PR89)
{
return m_cOutputListPR8[m_NetPR8.Recogn(ImgIn,dRecVal)];
}
else if (nType==SIMILAR_A4)
{
return m_cOutputListA4[m_NetA4.Recogn(ImgIn,dRecVal)];
}
return '?';
}
BOOL CRecImg::LoadBpNetCDG0(CString strPath)
{
m_NetCDG0.LoadBpNet(strPath);
return TRUE;
}
BOOL CRecImg::LoadBpNetChar(CString strPath)
{
m_BpNetChar.LoadBpNet(strPath);
return TRUE;
}
void CRecImg::TrainInt(CString strOutPutPath,int nType)
{
CString strPath;
int nSizeChar = sizeof(m_cOutputListInt)/sizeof(char)-1;
CString PathList[10][200];
int nPerNum = 200;
CMatrix mInput;
mInput.Zeros(nPerNum*nSizeChar,176);
BYTE byFea[256];
CMatrix mTarget;
mTarget.Zeros(nPerNum*nSizeChar,nSizeChar);
for(int i=0;i<nSizeChar;i++)
{
CFileFind filefind;
CString strFolder;
strFolder.Format("%sExaple\\%c\\",GetAppPath(),m_cOutputListInt[i]);
int nTop = 0;
if(filefind.FindFile(strFolder+"*.jpg"))
{
while (filefind.FindNextFile())
{
if(nTop>=nPerNum)
{
break;
}
PathList[i][nTop] = strFolder+filefind.GetFileName();
nTop++;
}
if(nTop<nPerNum)
{
PathList[i][nTop] = strFolder+filefind.GetFileName();
nTop++;
}
}
if(filefind.FindFile(strFolder+"*.bmp"))
{
while (filefind.FindNextFile())
{
if(nTop>=nPerNum)
{
break;
}
PathList[i][nTop] = strFolder+filefind.GetFileName();
nTop++;
}
if(nTop<nPerNum)
{
PathList[i][nTop] = strFolder+filefind.GetFileName();
nTop++;
}
}
}
CImage ImgIn;
int nLenFea;
for(int k=0;k<nPerNum;k++)
{
for(int j=0;j<nSizeChar;j++)
{
if (!ImgIn.Load(PathList[j][k]))
{
AfxMessageBox("无法读取图象");
return ;
continue ;
}
m_BpNetInt.GetFeature(ImgIn,byFea,nLenFea);
for(int nPix = 0;nPix<nLenFea;nPix++)
{
mInput.Set(k*nSizeChar+j,nPix,byFea[nPix]);
}
mTarget.Set(k*nSizeChar+j,j,1);
}
}
m_BpNetInt.Create(mInput,mTarget,mInput.cols(),mInput.cols()/2,nSizeChar);
m_BpNetInt.learn();
m_BpNetInt.SaveBpNet(strOutPutPath);
CMatrix mOut = m_BpNetInt.simulate(mInput);
CFileDialog fd(FALSE);
if (fd.DoModal()==IDOK)
{
mOut.SaveAsText(fd.GetPathName());
}
}
void CRecImg::TrainInt1(CString strOutPutPath)
{
CString strPath;
int nSizeChar = sizeof(m_cOutputListInt)/sizeof(char)-1;
CString PathList[10][200];
int nPerNum = 200;
CMatrix mInput;
BYTE byFea[128];
memset(byFea,0,128);
mInput.Zeros(nPerNum*nSizeChar,128);
CMatrix mTarget;
mTarget.Zeros(nPerNum*nSizeChar,nSizeChar);
int i,j,k;
for(i=0;i<nSizeChar;i++)
{
CFileFind filefind;
CString strFolder;
strFolder.Format("%sExaple\\%c\\",GetAppPath(),m_cOutputListInt[i]);
int nTop = 0;
if(filefind.FindFile(strFolder+"*.jpg"))
{
while (filefind.FindNextFile())
{
if(nTop>=nPerNum)
{
break;
}
PathList[i][nTop] = strFolder+filefind.GetFileName();
nTop++;
}
if(nTop<nPerNum)
{
PathList[i][nTop] = strFolder+filefind.GetFileName();
nTop++;
}
}
if(filefind.FindFile(strFolder+"*.bmp"))
{
while (filefind.FindNextFile())
{
if(nTop>=nPerNum)
{
break;
}
PathList[i][nTop] = strFolder+filefind.GetFileName();
nTop++;
}
if(nTop<nPerNum)
{
PathList[i][nTop] = strFolder+filefind.GetFileName();
nTop++;
}
}
}
CImage ImgIn;
int nLenFea;
for(k=0;k<nPerNum;k++)
{
for(j=0;j<nSizeChar;j++)
{
if (!ImgIn.Load(PathList[j][k]))
{
AfxMessageBox("无法读取图象");
return ;
continue ;
}
ImgIn.Stretch(CSize(16,32));
int nS = 2;
nLenFea = 0;
int i1,j1,k1,l1;
for(i1=0;i1<ImgIn.GetHeight()/nS;i1++)
{
for(j1=0;j1<ImgIn.GetWidth()/nS;j1++)
{
byFea[nLenFea] = 0;
for(k1=0;k1<nS;k1++)
{
for(l1=0;l1<nS;l1++)
{
if (ImgIn.m_pR[(i1*nS+k1)*16+j1*nS+l1]==0)
{
byFea[nLenFea]++;
}
}
}
nLenFea += 1;
}
}
for(int nPix = 0;nPix<nLenFea;nPix++)
{
mInput.Set(k*nSizeChar+j,nPix,byFea[nPix]);
}
mTarget.Set(k*nSizeChar+j,j,1);
}
}
m_BpNetInt1.Create(mInput,mTarget,mInput.cols(),mInput.cols()/2,nSizeChar);
m_BpNetInt1.learn();
m_BpNetInt1.SaveBpNet(strOutPutPath);
CMatrix mOut = m_BpNetInt1.simulate(mInput);
CFileDialog fd(FALSE);
if (fd.DoModal()==IDOK)
{
mOut.SaveAsText(fd.GetPathName());
}
}
char CRecImg::m_cOutputListA4[] = "A4";
char CRecImg::m_cOutputList7T[] = "7T";
char CRecImg::m_cOutputListCDG0[] = "CDG";
char CRecImg::m_cOutputListPR8[] = "APR69";
char CRecImg::m_cOutputListChar[]="ABCDEFGHJKLMNPRSUVY";
void CRecImg::TrainSimulateChar(CString strOutPutPath,CString strListChar,int nPerNum)
{
ASSERT(nPerNum<200);
CString strPath;
int nSizeChar = strListChar.GetLength();
CString PathList[31][200];
CMatrix mInput;
mInput.Zeros(nPerNum*nSizeChar,176);
BYTE byFea[256];
CMatrix mTarget;
mTarget.Zeros(nPerNum*nSizeChar,nSizeChar);
for(int i=0;i<nSizeChar;i++)
{
CFileFind filefind;
CString strFolder;
strFolder.Format("%sExaple\\%c\\",GetAppPath(),strListChar.GetAt(i));
int nTop = 0;
if(filefind.FindFile(strFolder+"*.jpg"))
{
while (filefind.FindNextFile())
{
if(nTop>=nPerNum)
{
break;
}
PathList[i][nTop] = strFolder+filefind.GetFileName();
nTop++;
}
if(nTop<nPerNum)
{
PathList[i][nTop] = strFolder+filefind.GetFileName();
nTop++;
}
}
if(filefind.FindFile(strFolder+"*.bmp"))
{
while (filefind.FindNextFile())
{
if(nTop>=nPerNum)
{
break;
}
PathList[i][nTop] = strFolder+filefind.GetFileName();
nTop++;
}
if(nTop<nPerNum)
{
PathList[i][nTop] = strFolder+filefind.GetFileName();
nTop++;
}
}
}
CImage ImgIn;
int nLenFea;
for(int k=0;k<nPerNum;k++)
{
for(int j=0;j<nSizeChar;j++)
{
if (!ImgIn.Load(PathList[j][k]))
{
AfxMessageBox("无法读取图象");
return ;
continue ;
}
m_BpNetChar.GetFeature(ImgIn,byFea,nLenFea);
for(int nPix = 0;nPix<nLenFea;nPix++)
{
mInput.Set(k*nSizeChar+j,nPix,byFea[nPix]);
}
mTarget.Set(k*nSizeChar+j,j,1);
}
}
m_BpNetChar.Create(mInput,mTarget,mInput.cols(),mInput.cols()/2,nSizeChar);
m_BpNetChar.learn();
m_BpNetChar.SaveBpNet(strOutPutPath);
CMatrix mOut = m_BpNetChar.simulate(mInput);
CFileDialog fd(FALSE);
if (fd.DoModal()==IDOK)
{
mOut.SaveAsText(fd.GetPathName());
}
}
void CRecImg::TrainChar(CString strOutPutPath)
{
CString strPath;
int nSizeChar = sizeof(m_cOutputListChar)/sizeof(char)-1;
CString PathList[31][50];
int nPerNum = 50;
CMatrix mInput;
mInput.Zeros(nPerNum*nSizeChar,176);
BYTE byFea[256];
CMatrix mTarget;
mTarget.Zeros(nPerNum*nSizeChar,nSizeChar);
for(int i=0;i<nSizeChar;i++)
{
CFileFind filefind;
CString strFolder;
strFolder.Format("%sExaple\\%c\\",GetAppPath(),m_cOutputListChar[i]);
int nTop = 0;
if(filefind.FindFile(strFolder+"*.jpg"))
{
while (filefind.FindNextFile())
{
if(nTop>=nPerNum)
{
break;
}
PathList[i][nTop] = strFolder+filefind.GetFileName();
nTop++;
}
if(nTop<nPerNum)
{
PathList[i][nTop] = strFolder+filefind.GetFileName();
nTop++;
}
}
if(filefind.FindFile(strFolder+"*.bmp"))
{
while (filefind.FindNextFile())
{
if(nTop>=nPerNum)
{
break;
}
PathList[i][nTop] = strFolder+filefind.GetFileName();
nTop++;
}
if(nTop<nPerNum)
{
PathList[i][nTop] = strFolder+filefind.GetFileName();
nTop++;
}
}
}
CImage ImgIn;
int nLenFea;
for(int k=0;k<nPerNum;k++)
{
for(int j=0;j<nSizeChar;j++)
{
if (!ImgIn.Load(PathList[j][k]))
{
AfxMessageBox("无法读取图象");
return ;
}
m_BpNetChar.GetFeature(ImgIn,byFea,nLenFea);
for(int nPix = 0;nPix<nLenFea;nPix++)
{
mInput.Set(k*nSizeChar+j,nPix,byFea[nPix]);
}
mTarget.Set(k*nSizeChar+j,j,1);
}
}
m_BpNetChar.Create(mInput,mTarget,mInput.cols(),mInput.cols()/2,nSizeChar);
m_BpNetChar.learn();
m_BpNetChar.SaveBpNet(strOutPutPath);
CMatrix mOut = m_BpNetChar.simulate(mInput);
CFileDialog fd(FALSE);
if (fd.DoModal()==IDOK)
{
mOut.SaveAsText(fd.GetPathName());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -