利用神经网络算法的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. 基于云开发 CloudBase 搭建在线视频会议应用教程

    基于云开发 CloudBase 搭建在线视频会议应用 在线视频会议应用是基于浏览器的能力 WebRTC 以及 腾讯云开发 CloudBase 能力构建而成的应用. 在云开发的助力下, 一个复杂的在线会 ...

  2. mds的cpu占用问题分析以及解决办法

    前言 mds是ceph里面处理文件接口的组件,一旦使用文件系统,不可避免的会出现一种场景就是目录很多,目录里面的文件很多,而mds是一个单进程的组件,现在虽然有了muti mds,但稳定的使用的大部分 ...

  3. stm32串口的配置方案

    最近老板要我去做控制方面的内容,所以买了一块正点原子的开发板,现在是研究了一下usart.c,函数的代码如下: void USART1_IRQHandler(void) { u8 Res; #ifde ...

  4. [从源码学设计]蚂蚁金服SOFARegistry之程序基本架构

    [从源码学设计]蚂蚁金服SOFARegistry之程序基本架构 0x00 摘要 之前我们通过三篇文章初步分析了 MetaServer 的基本架构,MetaServer 这三篇文章为我们接下来的工作做了 ...

  5. PHP一些不一样的思路

    大多数来自p牛 SQL注入(left join) 源代码 <?php $link = mysqli_connect('localhost', 'root', 'root'); mysqli_se ...

  6. tp5 日志的用途以及简单使用

    相信大家对日志这个词都很熟悉,那么日志通常是用来做什么的呢? 找错误和监控 正常来说,日志对维运的帮助是最大的,特别是服务器或者是程序出现错误的时候. 那么现在我们就来看看,tp框架的日志是怎么设置的 ...

  7. 来吧,展示!SpringBoot OSS 整合全过程,没见过比这更详细的了

    前言 阿里云对象存储服务(Object Storage Service,简称 OSS),是阿里云提供的海量.安全.低成本.高可靠的云存储服务.其数据设计持久性不低于 99.9999999999%(12 ...

  8. 如何合理利用iMindMap中的模板创建思维导图

    思维导图的制作并不是一项简单的工作,尤其是对许多工作或学习有特殊要求的朋友而言,当我们需要应对不同场景制作不同的思维导图时,总不能都靠自己从头制作,这样难度比较大也比较耗时.而iMindMap(win ...

  9. pip更新报错问题

    pip更新错误如下: WARNING: You are using pip version 20.1.1; however, version 20.2 is available. You should ...

  10. Leetcode 双周赛#32 题解

    1540 K次操作转变字符串 #计数 题目链接 题意 给定两字符串\(s\)和\(t\),要求你在\(k\)次操作以内将字符串\(s\)转变为\(t\),其中第\(i\)次操作时,可选择如下操作: 选 ...