利用神经网络算法的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
 
 

利用神经网络算法的C#手写数字识别(二)的更多相关文章

  1. 【机器学习】k-近邻算法应用之手写数字识别

    上篇文章简要介绍了k-近邻算法的算法原理以及一个简单的例子,今天再向大家介绍一个简单的应用,因为使用的原理大体差不多,就没有没有过多的解释. 为了具有说明性,把手写数字的图像转换为txt文件,如下图所 ...

  2. 卷积神经网络应用于tensorflow手写数字识别(第三版)

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...

  3. 基于sk_learn的k近邻算法实现-mnist手写数字识别且要求97%以上精确率

    1. 导入需要的库 from sklearn.datasets import fetch_openml import numpy as np from sklearn.neighbors import ...

  4. MLP 之手写数字识别

    0. 前言 前面我们利用 LR 模型实现了手写数字识别,但是效果并不好(不到 93% 的正确率). LR 模型从本质上来说还只是一个线性的分类器,只不过在线性变化之后加入了非线性单调递增 sigmoi ...

  5. 利用神经网络算法的C#手写数字识别

    欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 下载Demo - 2.77 MB (原始地址):handwritten_character_recognition.zip 下载源码 - 70. ...

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

    利用神经网络算法的C#手写数字识别 转发来自云加社区,用于学习机器学习与神经网络 欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 下载Demo - 2.77 MB (原始地址):handwri ...

  7. C#中调用Matlab人工神经网络算法实现手写数字识别

    手写数字识别实现 设计技术参数:通过由数字构成的图像,自动实现几个不同数字的识别,设计识别方法,有较高的识别率 关键字:二值化  投影  矩阵  目标定位  Matlab 手写数字图像识别简介: 手写 ...

  8. 实现手写数字识别(数据集50000张图片)比较3种算法神经网络、灰度平均值、SVM各自的准确率—Jason niu

    对手写数据集50000张图片实现阿拉伯数字0~9识别,并且对结果进行分析准确率, 手写数字数据集下载:http://yann.lecun.com/exdb/mnist/ 首先,利用图片本身的属性,图片 ...

  9. 利用c++编写bp神经网络实现手写数字识别详解

    利用c++编写bp神经网络实现手写数字识别 写在前面 从大一入学开始,本菜菜就一直想学习一下神经网络算法,但由于时间和资源所限,一直未展开比较透彻的学习.大二下人工智能课的修习,给了我一个学习的契机. ...

随机推荐

  1. 借助boost bind/function来实现基于对象编程。

    boost bind/function库的使用: 替换了stl中mem_fun,bind1st,bin2nd等函数.用户注册回调函数需要利用boost/bind转化成库中boost/function格 ...

  2. JS中 `=+` 是什么?

    JS中 =+ 是什么? 依然是赋值 =是赋值,+代表后面的数字为正数,同理=-代表后面的数字为负数 用处 相当于告诉编译器,即将赋值的数值类型为数字类型,不要把数字当作字符串去拼接 示例 functi ...

  3. python编码规范以及推导式的编写

    一.python 的编码规范

  4. php(tp5)生成条形码

    因为公司业务需要,研究了一下条形码 1.下载barcodegen扩展包 官网地址:https://www.barcodebakery.com 2.下载完后解压至 extend 文件夹里面,然后复制以下 ...

  5. scala的异常处理try catch

    object Test { def main(args: Array[String]) { try { val f = new FileReader("input.txt") } ...

  6. 深度分析:面试阿里,字节跳动,美团90%被问到的List集合,看完还不懂算我输

    1 List集合 1.1 List概述 在Collection中,List集合是有序的,可对其中每个元素的插入位置进行精确地控制,可以通过索引来访问元素,遍历元素. 在List集合中,我们常用到Arr ...

  7. springboot打jar包将引用的第三方包、配置文件(.properties、.xml)、静态资源打在包外

    1.外置配置文件 Springboot读取核心配置文件(.properties)的外部配置文件调用方式为 jar包当前目录下的/config目录 因此要外置配置文件就在jar所在目录新建config文 ...

  8. C语言讲义——冒泡排序(bubble sort)

    冒泡排序三步走: 循环 交换 回一手 一个数和其它数比较(循环) 每个数都要做这种比较(再一层循环) 准备工作 #include <stdio.h> void sort(int arr[] ...

  9. 【原创】视频+文字:详解VBA解决数独问题

    [说在前面]: 之前,我在微信朋友圈看到一个同事发了一个状态,说的是她在家辅导孩子做作业,一个数独的题目,好像没有做出来.我看了下,我也做不出来,后来仔细想了下,花了两个多小时时间,用Python编了 ...

  10. 一万三千字的HashMap面试必问知识点详解

    目录 概论 Hasmap 的继承关系 hashmap 的原理 解决Hash冲突的方法 开放定址法 再哈希法 链地址法 建立公共溢出区 hashmap 最终的形态 Hashmap 的返回值 HashMa ...