利用神经网络算法的C#手写数字识别(二)

利用神经网络算法的C#手写数字识别(二)
 
本篇主要内容:
让项目编译通过,并能打开图片进行识别。
 
1. 从上一篇《利用神经网络算法的C#手写数字识别》中的源码地址下载源码与资源,
注意,两者都要下载,资源里有训练数据集。
2. 下载后源码项目用VS打开,第一遍是编译不过的,会提示参数不正确。
将资源中的DATA文件夹考入到编译目录下,如Bin\Debug下, 即可编译通过.
目录如下:
 
3. 上篇文中所述的打开一个图片并识别的功能在代码中是没有实现的。
本篇我们将在此项目中实现。
先看看实现后的效果:
打开一个测试图后:
 
 
 
 
可以看到当前的Char的rectangle位置还不对,
我们先把这部分的实现做出来。
 
4. NNTestPatterns类中加入新的方法与成员变量
 
 #region 潘正平新增加的
public List<Rectangle> _rowRctList;
public List<Rectangle> _currentWordRctsList;
public List<Rectangle> _currentCharRctsList;
public List<Bitmap> m_bitmaps;
int _irowIndex = 0;
int _iwordIndex = 0;
int _icharIndex;
Bitmap _originalBitmap;
Bitmap _currentRow;
Bitmap _currentWord;
 
/// <summary>
/// 潘正平新加
/// </summary>
/// <param name="bitmap"></param>
public void PatternRecognitionThread(Bitmap bitmap)
{
m_bitmaps = new List<Bitmap>();
_originalBitmap = bitmap;
if (_rowRctList == null)
{
_rowRctList = AForge.Imaging.Image.PatternRectangeBoundaryList
(_originalBitmap, 255, 30, 1, true, 5, 5);
_irowIndex = 0;
 
}
foreach (Rectangle rowRect in _rowRctList)
{
_currentRow = AForge.Imaging.ImageResize.ImageCrop
(_originalBitmap, rowRect);
if (_iwordIndex == 0)
{
_currentWordRctsList = AForge.Imaging.Image.PatternRectangeBoundaryList
(_currentRow, 255, 20, 10, false, 5, 5);
}
 
foreach (Rectangle wordRect in _currentWordRctsList)
{
_currentWord = AForge.Imaging.ImageResize.ImageCrop
(_currentRow, wordRect);
_iwordIndex++;
if (_icharIndex == 0)
{
_currentCharRctsList =
AForge.Imaging.Image.PatternRectangeBoundaryList
(_currentWord, 255, 1, 1, false, 5, 5);
}
 
foreach (Rectangle charRect in _currentCharRctsList)
{
Bitmap _currentChar = AForge.Imaging.ImageResize.ImageCrop
(_currentWord, charRect);
_icharIndex++;
Bitmap bmptemp = AForge.Imaging.ImageResize.FixedSize
(_currentChar, 21, 21);
bmptemp = AForge.Imaging.Image.CreateColorPad
(bmptemp, Color.White, 4, 4);
bmptemp = AForge.Imaging.Image.CreateIndexedGrayScaleBitmap
(bmptemp);
byte[] graybytes = AForge.Imaging.Image.GrayscaletoBytes(bmptemp);
PatternRecognizingThread(graybytes);
m_bitmaps.Add(bmptemp);
}
string s = " \n";
_form.Invoke(_form._DelegateAddObject, new Object[] { 1, s });
if (_icharIndex == _currentCharRctsList.Count)
{
_icharIndex = 0;
}
}
if (_iwordIndex == _currentWordRctsList.Count)
{
_iwordIndex = 0;
}
}
}
#endregion

 


 
5. MainForm中增加 pictureBoxMain的Paint事件与对应方法.
增加打开图像文件菜单。
 
 #region 潘正平新增加的
private void OpenTestImagesToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!_bTestingDataReady || !_MinstTestingDatabase.m_bDatabaseReady)
{
MessageBox.Show("请先加载测试数据库!");
return;
}
OpenFileDialog fDlg = new OpenFileDialog();
fDlg.Filter = "*.bmp|*.bmp|*.jpg|*.jpg";
if (fDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Bitmap bmp = new Bitmap(fDlg.FileName);
//this.pictureBoxMain.Image = bmp;
Bitmap bmp2 = new Bitmap(bmp);
this.ImagePatternRecognization(bmp2);
}
}
 
private void ImagePatternRecognization(Bitmap bmp)
{
List<Mutex> mutexs = new List<Mutex>(2);
for (int i = 0; i < 2; i++)
{
var mutex = new Mutex();
mutexs.Add(mutex);
}
 
NNTessing = new NNTestPatterns(_NN, _Mnistdatabase, _Preference, _bDatabaseReady, null, null, this, mutexs);
var thread = new Thread(() => NNTessing.PatternRecognitionThread(bmp));
thread.Start();
while(thread.IsAlive)
{
Application.DoEvents();
Thread.Sleep(10);
}
this.pictureBoxMain.Image = bmp;
this.pictureBoxMain.Refresh();
}
 
NNTestPatterns NNTessing;
Pen pen = new Pen(Color.Red);
 
private void pictureBoxMain_Paint(object sender, PaintEventArgs e)
{
if (NNTessing != null)
{
if (NNTessing._currentCharRctsList != null)
{
e.Graphics.DrawRectangles(pen, NNTessing._currentCharRctsList.ToArray());
}
if (NNTessing._currentWordRctsList != null)
{
e.Graphics.DrawRectangles(pen, NNTessing._currentWordRctsList.ToArray());
}
if (NNTessing._rowRctList != null)
{
e.Graphics.DrawRectangles(pen, NNTessing._rowRctList.ToArray());
}
}
}
#endregion

 

 
6. 加入以上代码后编译就是我们看到的效果。
 
7. 一下步我们来看看最终识别的字符串以及的Char的Rectangle不在正确的位置。
代码修改后如下:
 
修改过程:理解几个rectangle的关系,有RowRct, wordRct, charRct 这几个均为相对位置。
显示识别结果放在主界面的右下方,
 
8. 针对以上描述修改的内容 NNTestPatterns.cs中:
#region 潘正平新增加的
public List<Rectangle> _rowRctList;
public List<Rectangle> _currentWordRctsList;
public List<Rectangle> _currentWordCharRctsList;
public List<Bitmap> m_bitmaps;
int _irowIndex = 0;
int _iwordIndex = 0;
int _icharIndex;
Bitmap _originalBitmap;
Bitmap _currentRow;
Bitmap _currentWord;
 
/// <summary>
/// 潘正平新加
/// </summary>
/// <param name="bitmap"></param>
public void PatternRecognitionThread(Bitmap bitmap)
{
_currentWordCharRctsList = new List<Rectangle>();
m_bitmaps = new List<Bitmap>();
_originalBitmap = bitmap;
if (_rowRctList == null)
{
_rowRctList = AForge.Imaging.Image.PatternRectangeBoundaryList
(_originalBitmap, 255, 30, 1, true, 5, 5);
_irowIndex = 0;
 
}
foreach (Rectangle rowRect in _rowRctList)
{
_currentRow = AForge.Imaging.ImageResize.ImageCrop
(_originalBitmap, rowRect);
if (_iwordIndex == 0)
{
_currentWordRctsList = AForge.Imaging.Image.PatternRectangeBoundaryList
(_currentRow, 255, 20, 10, false, 5, 5);
}
 
foreach (Rectangle wordRect in _currentWordRctsList)
{
_currentWord = AForge.Imaging.ImageResize.ImageCrop
(_currentRow, wordRect);
_iwordIndex++;
List<Rectangle> _currentCharRctsList = new List<Rectangle>();
if (_icharIndex == 0)
{
_currentCharRctsList = AForge.Imaging.Image.PatternRectangeBoundaryList(_currentWord, 255, 1, 1, false, 5, 5);
}
string strWord = string.Empty;
foreach (Rectangle charRect in _currentCharRctsList)
{
Bitmap _currentChar = AForge.Imaging.ImageResize.ImageCrop(_currentWord, charRect);
_icharIndex++;
Bitmap bmptemp = AForge.Imaging.ImageResize.FixedSize(_currentChar, 21, 21);
bmptemp = AForge.Imaging.Image.CreateColorPad(bmptemp, Color.White, 4, 4);
bmptemp = AForge.Imaging.Image.CreateIndexedGrayScaleBitmap(bmptemp);
byte[] graybytes = AForge.Imaging.Image.GrayscaletoBytes(bmptemp);
strWord += PatternRecognizingThread(graybytes);
m_bitmaps.Add(bmptemp);
//
Rectangle rctCharAbsolute = new Rectangle(wordRect.Left + charRect.Left + rowRect.Left, wordRect.Top + charRect.Top + rowRect.Top, charRect.Width, charRect.Height);
this._currentWordCharRctsList.Add(rctCharAbsolute);
}
string s = " \n";
_form.Invoke(_form._DelegateAddObject, new Object[] { 1, s });
_form.Invoke(_form._DelegateAddObject, new Object[] { 8, strWord });
if (_icharIndex == _currentCharRctsList.Count)
{
_icharIndex = 0;
}
}
if (_iwordIndex == _currentWordRctsList.Count)
{
_iwordIndex = 0;
}
}
}
#endregion

 


 
 

posted on 2018-07-25 17:01  youmeetmehere  阅读(930)  评论(2编辑  收藏  举报

导航