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

📄 bptraindlg.cpp

📁 针对车牌识别的字符bp神经网络训练
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// BPTrainDlg.cpp : implementation file
//
#include "stdafx.h"
#include "io.h"
#include "globalvar.h"
#include "BPTrain.h"
#include "BPTrainDlg.h"
#include "math.h"
#include "dibapi.h"
 
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// CBPTrainDlg dialog

CBPTrainDlg::CBPTrainDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBPTrainDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBPTrainDlg)
	m_Chn = _T("");
	m_Let = _T("");
	m_Num = _T("0");
	m_Type = _T("数字");
	m_Path = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBPTrainDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBPTrainDlg)
	DDX_CBString(pDX, IDC_COMBO_CHN, m_Chn);
	DDX_CBString(pDX, IDC_COMBO_LET, m_Let);
	DDX_CBString(pDX, IDC_COMBO_NUM, m_Num);
	DDX_CBString(pDX, IDC_COMBO_TYPE, m_Type);
	DDX_Text(pDX, IDC_EDIT_PATH, m_Path);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBPTrainDlg, CDialog)
	//{{AFX_MSG_MAP(CBPTrainDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_CLOSEUP(IDC_COMBO_TYPE, OnCloseupComboType)
	ON_BN_CLICKED(IDC_BTN_BROWSE, OnBtnBrowse)
	ON_BN_CLICKED(IDCANCEL2, OnCancel2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBPTrainDlg message handlers

BOOL CBPTrainDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	GetDlgItem(IDC_COMBO_NUM)->ShowWindow(SW_SHOW);
	GetDlgItem(IDC_COMBO_LET)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_COMBO_CHN)->ShowWindow(SW_HIDE);

	char szPath[255], *pDest;
    int nPos;

    if(0 == GetModuleFileName(NULL, szPath, 255))
		return FALSE;
    
    pDest = strrchr(szPath, '\\');
    if(NULL != pDest)
    {
        nPos = pDest - szPath + 1;
        szPath[nPos] = '\0';
    }
    else
        return FALSE;
 
    
    /* 执行程序所在路径 */
    strcpy(g_acExeDir, szPath);

	m_bTrained=FALSE;

	/////start初始化权重
	int i,j;
	srand(4711);

    for (i=1; i<=HID1SIZE; i++) {
		for (j=0; j<=INPUTSIZE; j++) {
			InH1Weight[i][j].Weight = (((float) rand()) / RAND_MAX) * ((0.5)-(-0.5)) + (-0.5);
		}
    }
	for (i=1; i<=HID2SIZE; i++) {
		for (j=0; j<=HID1SIZE; j++) {
			H1H2Weight[i][j].Weight = (((float) rand()) / RAND_MAX) * ((0.5)-(-0.5)) + (-0.5);
		}
    }
	for (i=1; i<=OUTSIZE; i++) {
		for (j=0; j<=HID2SIZE; j++) {
			H2OutWeight[i][j].Weight = (((float) rand()) / RAND_MAX) * ((0.5)-(-0.5)) + (-0.5);
		}
    }
	/////end初始化权重

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CBPTrainDlg::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 CBPTrainDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}




void CBPTrainDlg::OnCloseupComboType() 
{
	// TODO: Add your control notification handler code here
		UpdateData();
	if (strcmp(m_Type,"数字")==0)
	{
		GetDlgItem(IDC_COMBO_NUM)->ShowWindow(SW_SHOW);
		GetDlgItem(IDC_COMBO_LET)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_COMBO_CHN)->ShowWindow(SW_HIDE);
	}
	else if (strcmp(m_Type,"字母")==0)
	{
		GetDlgItem(IDC_COMBO_NUM)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_COMBO_LET)->ShowWindow(SW_SHOW);
		GetDlgItem(IDC_COMBO_CHN)->ShowWindow(SW_HIDE);
	}
	else if (strcmp(m_Type,"汉字")==0)
	{
		GetDlgItem(IDC_COMBO_NUM)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_COMBO_LET)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_COMBO_CHN)->ShowWindow(SW_SHOW);
	}
}

void CBPTrainDlg::OnBtnBrowse() 
{
	// TODO: Add your control notification handler code here
	
	CString sFolder;   
	LPMALLOC pMalloc;   
	
	//Gets the Shell's default allocator   
	if(::SHGetMalloc(&pMalloc) == NOERROR)   
	{   
		BROWSEINFO  bi;   
		char pszBuffer[MAX_PATH];   
		LPITEMIDLIST pidl;   
		
		bi.hwndOwner = GetSafeHwnd();   
		bi.pidlRoot = NULL;   
		bi.pszDisplayName = pszBuffer;   
		bi.lpszTitle = _T("Select a directory...");   
		bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;   
		bi.lpfn = NULL;   
		bi.lParam = 0;   
		
		//This next call issues the  dialog box.   
		if((pidl = ::SHBrowseForFolder(&bi)) !=  NULL)   
		{   
			if(::SHGetPathFromIDList(pidl, pszBuffer))   
			{     
				//At this point pszBuffer contains the selected path   
				sFolder = pszBuffer;   
			}
			
			//Free the PIDL allocated by SHBrowseForFolder.   
			pMalloc->Free(pidl);   
		}
		//Release the shell's allocator.   
		pMalloc->Release();   
	}
	m_Path=sFolder;
	UpdateData(FALSE);
}

void CBPTrainDlg::OnOK() 
{
	UpdateData();
	if (m_Path=="")
	{
		AfxMessageBox("路径不能为空");
		return ;
	}
	if(-1 == _access(m_Path, 0))
	{
		AfxMessageBox("字元图像路径不存在,请检查!");
		return ;
	}

	//OUTSIZE 根据 m_Type而定
	if (strcmp(m_Type,"数字")==0)
		OUTSIZE=10;
	else if (strcmp(m_Type,"字母")==0)
		OUTSIZE=13;
	else  if (strcmp(m_Type,"汉字")==0)
		OUTSIZE=31;
	else
	{
		AfxMessageBox("输出层神经元数目无法确定!训练失败");
		return;
	}

	//设置Target值
	int iSelItem;
	CString ss;
	
	if (strcmp(m_Type,"数字")==0)
	{
		((CComboBox*)GetDlgItem(IDC_COMBO_NUM))->GetWindowText(ss);
		iSelItem=((CComboBox*)GetDlgItem(IDC_COMBO_NUM))->FindString(0,ss);
	}
	else if (strcmp(m_Type,"字母")==0)
	{
		((CComboBox*)GetDlgItem(IDC_COMBO_LET))->GetWindowText(ss);
		iSelItem=((CComboBox*)GetDlgItem(IDC_COMBO_LET))->FindString(0,ss);
	}
	else if (strcmp(m_Type,"汉字")==0)
	{
		((CComboBox*)GetDlgItem(IDC_COMBO_CHN))->GetWindowText(ss);
		ss=ss.Left(2);
		iSelItem=((CComboBox*)GetDlgItem(IDC_COMBO_CHN))->FindString(0,ss);
	}

	int i;
	for (i=0; i<MAXOUTSIZE; Target[i]=-1, i++); //ysirss modified
	Target[iSelItem]=1;

	unsigned char *image=NULL;
	CString strPath;
	HDIB tmp_hDIB;
	CPalette *tmp_palDIB;
	tmp_hDIB   = NULL;
	tmp_palDIB = NULL;
	
	image = (unsigned char *)malloc(Row * Col * 3);
	strPath=m_Path+"\\*.bmp";
	CString filename;
	BOOL bWorking = false;
	bWorking = m_filefinder.FindFile(strPath);
	if(!bWorking)
	{
		AfxMessageBox("目录'"+m_Path+"'中不存在字元图像,请检查!");
		return ;
	}
	
	while(bWorking)
	{
        bWorking =  m_filefinder.FindNextFile();
		filename = m_filefinder.GetFilePath();
	    
		////////读字元图像
		CFile f(filename,CFile::modeRead);  //read the file .bmp
		tmp_hDIB = ::ReadDIBFile(f);          //get the handle of the file .bmp
		f.Close();
        this->SetWindowText(filename);
		filename.ReleaseBuffer();

		LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) tmp_hDIB);   //lock
		tmp_palDIB = new CPalette;   //allocate memory for palette
		if (NULL == tmp_palDIB)
		{
			// we must be really low on memory
			::GlobalFree((HGLOBAL) tmp_hDIB);
			tmp_hDIB = NULL;
			free(image);
			return ;
		}
		if (NULL == ::CreateDIBPalette(tmp_hDIB, tmp_palDIB))//create the pallette
		{
			// DIB may not have a palette
			delete tmp_palDIB;
			tmp_palDIB = NULL;
		}
		
		DWORD dwWidth  = DIBWidth(lpDIB);
		DWORD dwHeight = DIBHeight(lpDIB);

⌨️ 快捷键说明

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