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

📄 feature.cpp

📁 车牌识别(改定位)武汉理工大学
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		delete []byFea;
		return FALSE;
	}
	CArchive myar(&file,CArchive::store);
	m_BpNet.Serialize(myar); 
	myar.Close();
	file.Write(&m_nFeaType,sizeof(int));
	file.Write(&nSizeChar,sizeof(int));
	char pCh[1024];
	strcpy(pCh,strListChar);
	file.Write(pCh,nSizeChar);
	file.Close();
	CMatrix mOut = m_BpNet.simulate(mInput);
	CFileDialog fd(FALSE);
	if (fd.DoModal()==IDOK)
	{
		mOut.SaveAsText(fd.GetPathName());
	}
	delete []byFea;
	return TRUE;
}
BOOL CRecBase::Train(CString strOutPath,CString strListChar,int nPerNum)
{
	ASSERT(nPerNum<=200);
	ASSERT(m_pFeature!=NULL);
	CString strPath;
	int nSizeChar = strListChar.GetLength();
	
	CString PathList[31][200];	
	
	CMatrix mInput;
	mInput.Zeros(nPerNum*nSizeChar,m_pFeature->GetFeaLen());
	BYTE *byFea = new BYTE[m_pFeature->GetFeaLen()];
	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]))
			{
				CString strTemp;
				strTemp.Format("无法读取图象:%c第%d图",strListChar.GetAt(j),k);
				AfxMessageBox(strTemp);
				delete []byFea;
				return FALSE;
			}
			nLenFea = m_pFeature->GetFeaLen();
			m_pFeature->GetFeature(ImgIn,byFea);	
			for(int nPix = 0;nPix<nLenFea;nPix++)
			{
				mInput.Set(k*nSizeChar+j,nPix,byFea[nPix]);
			}
			mTarget.Set(k*nSizeChar+j,j,1);
		}
	}
	
	m_BpNet.Create(mInput,mTarget,mInput.cols(),mInput.cols()/2,nSizeChar);
	m_BpNet.learn();

	CFile file;
	if (!file.Open(strOutPath,CFile::modeCreate|CFile::modeWrite)) {
		delete []byFea;
		return FALSE;
	}
	CArchive myar(&file,CArchive::store);
	m_BpNet.Serialize(myar); 
	myar.Close();
	file.Write(&m_nFeaType,sizeof(int));
	file.Write(&nSizeChar,sizeof(int));
	char pCh[1024];
	strcpy(pCh,strListChar);
	file.Write(pCh,nSizeChar);
	file.Close();
	CMatrix mOut = m_BpNet.simulate(mInput);
	CFileDialog fd(FALSE);
	if (fd.DoModal()==IDOK)
	{
		mOut.SaveAsText(fd.GetPathName());
	}
	delete []byFea;
	return TRUE;
}
char CRecBase::Recogn(CImage &ImgIn,double &dRecVal,char * cResult,double *dVal)
{
	ASSERT(m_pFeature);
	int nLenFea = m_pFeature->GetFeaLen();
	BYTE *byFea = new BYTE[nLenFea];
	m_pFeature->GetFeature(ImgIn,byFea);
	CMatrix mIn;
	CMatrix mOut;
	mIn.SetData(1,nLenFea,byFea);
	delete []byFea;
	mOut = m_BpNet.simulate(mIn);	
	int nResult;
	dRecVal = mOut.Max(nResult);	
	if(cResult!=NULL&&dVal!=NULL)
	{
	}
	return m_strListChar[nResult];
}

//////////////////////////////////////////////////////////////////////
// CHVLRFeature Class
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CHVLRFeature::CHVLRFeature()
{

}

CHVLRFeature::~CHVLRFeature()
{

}
int CHVLRFeature::GetFeaLen()
{
	return 105;
}
BOOL CHVLRFeature::GetFeature(CImage &ImgIn,BYTE *pFea)
{
	if(ImgIn.GetWidth()!=16||ImgIn.GetHeight()!=32)
	{
		ImgIn.Stretch(CSize(16,32));
	}
	int i,j,k,l;
	BYTE pData[64];
	BYTE nH,nV,nL,nR,nS;
	int nDim = 6;

	for(i=0;i<ImgIn.GetHeight()-nDim;i+=4)
	{
		for(j=0;j<ImgIn.GetWidth()-nDim;j+=4)
		{
			for(k=0;k<nDim;k++)
			{
				BYTE * pTemp = ImgIn.m_pR+(i+k)*ImgIn.GetWidth()+j;
				for(l=0;l<nDim;l++)
				{
					pData[k*nDim+l] = pTemp[l];
				}
			}
			GetFeaNN(nH,nV,nL,nR,nS,pData,nDim);
			pFea[0] = nH;
			pFea[1] = nV;
			pFea[2] = nL;
			pFea[3] = nR;
			pFea[4] = nS;
			pFea += 5;
		}
	}
	return TRUE;
}
void CHVLRFeature::GetFeaNN(BYTE &nH, BYTE &nV, BYTE &nL, BYTE &nR, BYTE &nS, BYTE * pData,int nDim)
{
	int i,j;//i表示高,j表示宽
	nS = 0;
	nH = 0;
	nV = 0;
	nL = 0;
	nR = 0;
	//总数
	for(i=0;i<nDim;i++)
	{
		for(j=0;j<nDim;j++)
		{
			if (pData[i*nDim+j]==0)
			{
				nS++;
			}
		}
	}
	return;
	//横
	for(i=0;i<nDim;i++)
	{
		for(j=0;j<nDim-1;j++)
		{
			if (pData[i*nDim+j]==0&&pData[i*nDim+j+1]==0) 
			{
				nH++;
			}
		}
	}
	//竖
	for(i=0;i<nDim-1;i++)
	{
		for(j=0;j<nDim;j++)
		{
			if (pData[i*nDim+j]==0&&pData[(i+1)*nDim+j]==0) 
			{
				nV++;
			}
		}
	}
	//撇
	for(i=1;i<nDim;i++)
	{
		for(j=0;j<i-1;j++)
		{
			if (pData[i*nDim+j]==0&&pData[(i+1)*nDim+j-1]==0) 
			{
				nL++;
			}
		}
	}
	for(i=nDim-2;i>0;i--)
	{
		for(j=nDim-1;j>i;j--)
		{
			if (pData[i*nDim+j]==0&&pData[(i-1)*nDim+j+1]==0) 
			{
				nL++;
			}
		}
	}
	//捺
	for(j=0;j<nDim;j++)
	{
		for(i=0;i<j-1;i++)
		{
			if (pData[i*nDim+j]==0&&pData[(i+1)*nDim+j+1]==0)
			{
				nR++;
			}
		}
	}
	for(j=0;j<nDim-1;j++)
	{
		for(i=nDim-1;i>j;i--)
		{
			if (pData[i*nDim+j]==0&&pData[(i-1)*nDim+j-1]==0) 
			{
				nR++;
			}
		}
	}
}

BOOL CRecBase::SetFeaType(int nType)
{
	m_nFeaType = nType;
	if(m_pFeature)
	{
		delete m_pFeature;
		m_pFeature = NULL;
	}
	if(nType==0)
	{
		m_pFeature = new CFeaEdge;
	}
	else if (nType==1)
	{
		m_pFeature = new CFeaPosition;
	}
	else if(nType==2)
	{
		m_pFeature = new CHVLRFeature;
	}
	else if (nType==3)
	{
		m_pFeature = new CFeaKirsh;
	}
	else
	{
		m_pFeature = NULL;
	}
	return TRUE;
}

//////////////////////////////////////////////////////////////////////
// CFeaKirsh Class
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CFeaKirsh::CFeaKirsh()
{

}

CFeaKirsh::~CFeaKirsh()
{

}
int CFeaKirsh::m_nFeaLen = 192;
int CFeaKirsh::GetFeaLen()
{
	return m_nFeaLen;
}
BOOL CFeaKirsh::GetFeature(CImage &ImgIn,BYTE *pFea)
{
	int nLen = 0;
	CImage ImgTemp = ImgIn;
	ImgTemp.Stretch(CSize(16,32));
	//提取特征
	BYTE * pFeaT = pFea;
	BYTE byData[64];
	BYTE * pBuf = ImgTemp.m_pR;
	int i,j,k,l;
	
	int nWAndH = ImgTemp.GetHeight()+ImgTemp.GetWidth();
	memset(pFeaT,0,3*nWAndH);
	int nW = ImgTemp.GetWidth();
	//先宽后高
	//求垂直跳变次数
	for(i=0;i<ImgTemp.GetWidth();i++)
	{
		for(j=0;j<ImgTemp.GetHeight()-1;j++)
		{
			if (pBuf[j*nW+i]!=pBuf[(j+1)*nW+i]&&pBuf[j*nW+i]==0) 
			{
				pFeaT[i]++;
			}
		}
		if (pBuf[j*nW+i]==0) 
		{
			pFeaT[i]++;
		}
	}
	pFeaT += ImgTemp.GetWidth();
	nLen += ImgTemp.GetWidth();
	//水平跳变次数
	for(i=0;i<ImgTemp.GetHeight();i++)
	{
		for(j=0;j<ImgTemp.GetWidth()-1;j++)
		{
			if (pBuf[i*nW+j]!=pBuf[i*nW+j+1]&&pBuf[i*nW+j]==0) 
			{
				pFeaT[i]++;
			}
		}
		if (pBuf[i*nW+j]==0) 
		{
			pFeaT[i]++;
		}
	}
	pFeaT += ImgTemp.GetHeight();
	nLen += ImgTemp.GetHeight();
	
	//求垂直方向起始位置
	for(i=0;i<ImgTemp.GetWidth();i++)
	{
		pFeaT[i] = 0;
		pFeaT[i+nW] = ImgTemp.GetHeight()-1;
		for(j=0;j<ImgTemp.GetHeight();j++)
		{	
			if (pBuf[j*nW+i]==0) 
			{
				pFeaT[i] = j;
				break;
			}
		}		
		for(j=ImgTemp.GetHeight()-1;j>=0;j--)
		{
			if (pBuf[j*nW+i]==0) 
			{
				pFeaT[i+nW] = j;
				break;
			}
		}
	}
	pFeaT += 2*ImgTemp.GetWidth();
	nLen += 2*ImgTemp.GetWidth();
	//水平起始位置
	for(i=0;i<ImgTemp.GetHeight();i++)
	{
		pFeaT[i] = 0;
		pFeaT[i+nW] = ImgTemp.GetWidth()-1;
		for(j=0;j<ImgTemp.GetWidth();j++)
		{
			if (pBuf[i*nW+j]==0) 
			{
				pFeaT[i] = j;
				break;
			}
		}
		for(j=ImgTemp.GetWidth()-1;j>=0;j--)
		{
			if (pBuf[i*nW+j]==0) 
			{
				pFeaT[i+nW] = j;
				break;
			}
		}
	}
	pFeaT += 2*ImgTemp.GetHeight();
	nLen += 2*ImgTemp.GetHeight();
	//水平,垂直方向的黑点数
	memset(pFeaT,0,ImgTemp.GetWidth()+ImgTemp.GetHeight());
	for(i=0;i<ImgTemp.GetHeight();i++)
	{
		for(j=0;j<ImgTemp.GetWidth();j++)
		{
			if (pBuf[i*nW+j]==0) 
			{
				pFeaT[nW+i]++;
				pFeaT[j]++;
			}
		}
	}
	pFeaT += ImgTemp.GetHeight()+ImgTemp.GetWidth();
	nLen += ImgTemp.GetHeight()+ImgTemp.GetWidth();
	return TRUE;
}

⌨️ 快捷键说明

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