c#实现识别图片上的验证码数字
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
public void imgdo(Bitmap img) { //去色 Bitmap btp = img; Color c = new Color(); int rr, gg, bb; for ( int i = 0; i < btp.Width; i++) { for ( int j = 0; j < btp.Height; j++) { //取图片当前的像素点 c = btp.GetPixel(i, j); rr = c.R; gg = c.G; bb = c.B; //改变颜色 if (rr == 102 && gg == 0 && bb == 0) { //重新设置当前的像素点 btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255)); } if (rr == 153 && gg == 0 && bb == 0) { //重新设置当前的像素点 btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255)); } if (rr == 153 && gg == 0 && bb == 51) { //重新设置当前的像素点 btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255)); } if (rr == 153 && gg == 43 && bb == 51) { //重新设置当前的像素点 btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255)); } if (rr == 255 && gg == 255 && bb == 0) { //重新设置当前的像素点 btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255)); } if (rr == 255 && gg == 255 && bb == 51) { //重新设置当前的像素点 btp.SetPixel(i, j, Color.FromArgb(255, 255, 255, 255)); } } } btp.Save( "d:\\去除相关颜色.png" ); pictureBox2.Image = Image.FromFile( "d:\\去除相关颜色.png" ); //灰度 Bitmap bmphd = btp; for ( int i = 0; i < bmphd.Width; i++) { for ( int j = 0; j < bmphd.Height; j++) { //取图片当前的像素点 var color = bmphd.GetPixel(i, j); var gray = ( int )(color.R * 0.001 + color.G * 0.700 + color.B * 0.250); //重新设置当前的像素点 bmphd.SetPixel(i, j, Color.FromArgb(gray, gray, gray)); } } bmphd.Save( "d:\\灰度.png" ); pictureBox27.Image = Image.FromFile( "d:\\灰度.png" ); //二值化 Bitmap erzhi = bmphd; Bitmap orcbmp; int nn = 3; int w = erzhi.Width; int h = erzhi.Height; BitmapData data = erzhi.LockBits( new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); unsafe { byte * p = ( byte *)data.Scan0; byte [,] vSource = new byte [w, h]; int offset = data.Stride - w * nn; for ( int y = 0; y < h; y++) { for ( int x = 0; x < w; x++) { vSource[x, y] = ( byte )((( int )p[0] + ( int )p[1] + ( int )p[2]) / 3); p += nn; } p += offset; } erzhi.UnlockBits(data); Bitmap bmpDest = new Bitmap(w, h, PixelFormat.Format24bppRgb); BitmapData dataDest = bmpDest.LockBits( new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); p = ( byte *)dataDest.Scan0; offset = dataDest.Stride - w * nn; for ( int y = 0; y < h; y++) { for ( int x = 0; x < w; x++) { p[0] = p[1] = p[2] = ( int )vSource[x, y] > 161 ? ( byte )255 : ( byte )0; //p[0] = p[1] = p[2] = (int)GetAverageColor(vSource, x, y, w, h) > 50 ? (byte)255 : (byte)0; p += nn; } p += offset; } bmpDest.UnlockBits(dataDest); orcbmp = bmpDest; orcbmp.Save( "d:\\二值化.png" ); pictureBox29.Image = Image.FromFile( "d:\\二值化.png" ); } //OCR的值 if (orcbmp != null ) { string result = Ocr(orcbmp); label32.Text = result.Replace( "\n" , "\r\n" ).Replace( " " , "" ); } } |
C#识别验证码图片通用类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
using System; using System.Collections.Generic; using System.Text; using System.Collections; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace BallotAiying2 { class UnCodebase { public Bitmap bmpobj; public UnCodebase(Bitmap pic) { bmpobj = new Bitmap(pic); //转换为Format32bppRgb } /// <summary> /// 根据RGB,计算灰度值 /// </summary> /// <param name="posClr">Color值</param> /// <returns>灰度值,整型</returns> private int GetGrayNumColor(System.Drawing.Color posClr) { return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16; } /// <summary> /// 灰度转换,逐点方式 /// </summary> public void GrayByPixels() { for ( int i = 0; i < bmpobj.Height; i++) { for ( int j = 0; j < bmpobj.Width; j++) { int tmpValue = GetGrayNumColor(bmpobj.GetPixel(j, i)); bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue, tmpValue, tmpValue)); } } } /// <summary> /// 去图形边框 /// </summary> /// <param name="borderWidth"></param> public void ClearPicBorder( int borderWidth) { for ( int i = 0; i < bmpobj.Height; i++) { for ( int j = 0; j < bmpobj.Width; j++) { if (i < borderWidth || j < borderWidth || j > bmpobj.Width - 1 - borderWidth || i > bmpobj.Height - 1 - borderWidth) bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255)); } } } /// <summary> /// 灰度转换,逐行方式 /// </summary> public void GrayByLine() { Rectangle rec = new Rectangle(0, 0, bmpobj.Width, bmpobj.Height); BitmapData bmpData = bmpobj.LockBits(rec, ImageLockMode.ReadWrite, bmpobj.PixelFormat); // PixelFormat.Format32bppPArgb); // bmpData.PixelFormat = PixelFormat.Format24bppRgb; IntPtr scan0 = bmpData.Scan0; int len = bmpobj.Width * bmpobj.Height; int [] pixels = new int [len]; Marshal.Copy(scan0, pixels, 0, len); //对图片进行处理 int GrayValue = 0; for ( int i = 0; i < len; i++) { GrayValue = GetGrayNumColor(Color.FromArgb(pixels)); pixels = ( byte )(Color.FromArgb(GrayValue, GrayValue, GrayValue)).ToArgb(); //Color转byte } bmpobj.UnlockBits(bmpData); } /// <summary> /// 得到有效图形并调整为可平均分割的大小 /// </summary> /// <param name="dgGrayValue">灰度背景分界值</param> /// <param name="CharsCount">有效字符数</param> /// <returns></returns> public void GetPicValidByValue( int dgGrayValue, int CharsCount) { int posx1 = bmpobj.Width; int posy1 = bmpobj.Height; int posx2 = 0; int posy2 = 0; for ( int i = 0; i < bmpobj.Height; i++) //找有效区 { for ( int j = 0; j < bmpobj.Width; j++) { int pixelValue = bmpobj.GetPixel(j, i).R; if (pixelValue < dgGrayValue) //根据灰度值 { if (posx1 > j) posx1 = j; if (posy1 > i) posy1 = i; if (posx2 < j) posx2 = j; if (posy2 < i) posy2 = i; }; }; }; // 确保能整除 int Span = CharsCount - (posx2 - posx1 + 1) % CharsCount; //可整除的差额数 if (Span < CharsCount) { int leftSpan = Span / 2; //分配到左边的空列 ,如span为单数,则右边比左边大1 if (posx1 > leftSpan) posx1 = posx1 - leftSpan; if (posx2 + Span - leftSpan < bmpobj.Width) posx2 = posx2 + Span - leftSpan; } //复制新图 Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1); bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat); } /// <summary> /// 得到有效图形,图形为类变量 /// </summary> /// <param name="dgGrayValue">灰度背景分界值</param> /// <param name="CharsCount">有效字符数</param> /// <returns></returns> public void GetPicValidByValue( int dgGrayValue) { int posx1 = bmpobj.Width; int posy1 = bmpobj.Height; int posx2 = 0; int posy2 = 0; for ( int i = 0; i < bmpobj.Height; i++) //找有效区 { for ( int j = 0; j < bmpobj.Width; j++) { int pixelValue = bmpobj.GetPixel(j, i).R; if (pixelValue < dgGrayValue) //根据灰度值 { if (posx1 > j) posx1 = j; if (posy1 > i) posy1 = i; if (posx2 < j) posx2 = j; if (posy2 < i) posy2 = i; }; }; }; //复制新图 Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1); bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat); } /// <summary> /// 得到有效图形,图形由外面传入 /// </summary> /// <param name="dgGrayValue">灰度背景分界值</param> /// <param name="CharsCount">有效字符数</param> /// <returns></returns> public Bitmap GetPicValidByValue(Bitmap singlepic, int dgGrayValue) { int posx1 = singlepic.Width; int posy1 = singlepic.Height; int posx2 = 0; int posy2 = 0; for ( int i = 0; i < singlepic.Height; i++) //找有效区 { for ( int j = 0; j < singlepic.Width; j++) { int pixelValue = singlepic.GetPixel(j, i).R; if (pixelValue < dgGrayValue) //根据灰度值 { if (posx1 > j) posx1 = j; if (posy1 > i) posy1 = i; if (posx2 < j) posx2 = j; if (posy2 < i) posy2 = i; }; }; }; //复制新图 Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1); return singlepic.Clone(cloneRect, singlepic.PixelFormat); } /// <summary> /// 平均分割图片 /// </summary> /// <param name="RowNum">水平上分割数</param> /// <param name="ColNum">垂直上分割数</param> /// <returns>分割好的图片数组</returns> public Bitmap [] GetSplitPics( int RowNum, int ColNum) { if (RowNum == 0 || ColNum == 0) return null ; int singW = bmpobj.Width / RowNum; int singH = bmpobj.Height / ColNum; Bitmap [] PicArray= new Bitmap[RowNum*ColNum]; Rectangle cloneRect; for ( int i = 0; i < ColNum; i++) //找有效区 { for ( int j = 0; j < RowNum; j++) { cloneRect = new Rectangle(j*singW, i*singH, singW , singH); PicArray[i*RowNum+j]=bmpobj.Clone(cloneRect, bmpobj.PixelFormat); //复制小块图 } } return PicArray; } /// <summary> /// 返回灰度图片的点阵描述字串,1表示灰点,0表示背景 /// </summary> /// <param name="singlepic">灰度图</param> /// <param name="dgGrayValue">背前景灰色界限</param> /// <returns></returns> public string GetSingleBmpCode(Bitmap singlepic, int dgGrayValue) { Color piexl; string code = "" ; for ( int posy = 0; posy < singlepic.Height; posy++) for ( int posx = 0; posx < singlepic.Width; posx++) { piexl = singlepic.GetPixel(posx, posy); if (piexl.R < dgGrayValue) // Color.Black ) code = code + "1" ; else code = code + "0" ; } return code; } } } |
c#实现识别图片上的验证码数字的更多相关文章
- Python3.x:如何识别图片上的文字
Python3.x:如何识别图片上的文字 安装pytesseract库,必须先安装其依赖的PIL及tesseract-ocr,其中PIL为图像处理库,而后面的tesseract-ocr则为google ...
- python 识别图片上的数字
https://blog.csdn.net/qq_31446377/article/details/81708006 ython 3.6 版本 Pytesseract 图像验证码识别 环境: (1) ...
- C#识别图片上的数字
通过Emgu实现对图片上的数字进行识别. 前期步骤: 1.下载Emgu安装文件,我的版本是2.4.2.1777.3.0版本则实现对中文的支持. 2.安装后需填写环境变量,环境变量Path值后加入Emg ...
- 分享C#识别图片上的数字
通过Emgu实现对图片上的数字进行识别.前期步骤:1.下载Emgu安装文件,我的版本是2.4.2.1777.3.0版本则实现对中文的支持.2.安装后需填写环境变量,环境变量Path值后加入Emgu安装 ...
- 如何大批量的识别图片上的文字,批量图片文字识别OCR软件系统
软件不需要安装,直接双击打开就可以用,废话不多说直接上图好了,方便说明问题 批量图片OCR(批量名片识别.批量照片识别等)识别,然后就下来研究了一下,下面是成果 使用步骤:打开单个图片识别,导入文件夹 ...
- 机器学习进阶-项目实战-信用卡数字识别 1.cv2.findContour(找出轮廓) 2.cv2.boudingRect(轮廓外接矩阵位置) 3.cv2.threshold(图片二值化操作) 4.cv2.MORPH_TOPHAT(礼帽运算突出线条) 5.cv2.MORPH_CLOSE(闭运算图片内部膨胀) 6. cv2.resize(改变图像大小) 7.cv2.putText(在图片上放上文本)
7. cv2.putText(img, text, loc, text_font, font_scale, color, linestick) # 参数说明:img表示输入图片,text表示需要填写的 ...
- PHP识别简单的图片上面的数字(可扩展)
1.场景 最近在学习图片处理,就是特意把数字生成一个图片,然后再用程序去识别图片的数字.这就有了一下的学习过程. 2.原理分析 2.1 首先是将图片像素化,二值化,然后和字模去对比(需要相对于配置字模 ...
- KNN识别图像上的数字及python实现
领导让我每天手工录入BI系统中的数据并判断数据是否存在异常,若有异常点,则检测是系统问题还是业务问题.为了解放双手,我决定写个程序完成每天录入管理驾驶舱数据的任务.首先用按键精灵录了一套脚本把系统中的 ...
- python爬虫20 | 小帅b教你如何使用python识别图片验证码
当你在爬取某些网站的时候 对于你的一些频繁请求 对方会阻碍你 常见的方式就是使用验证码 验证码的主要功能 就是区分你是人还是鬼(机器人) 人 想法设法的搞一些手段来对付技术 而 技术又能对付人们的想法 ...
随机推荐
- Vue项目的打包
vue项目的打包 更改config文件夹下的index.js里的assetsPublicPath路径 将 “/” 改为 “./” build: { env: require('./prod. ...
- DevExpress ASP.NET Core Controls 2019发展蓝图(No.4)
本文主要为大家介绍DevExpress ASP.NET Core Controls 2019年的官方发展蓝图,更多精彩内容欢迎持续收藏关注哦~ [DevExpress ASP.NET Controls ...
- Ceph集群更换public_network网络
1.确保ceph集群是连通状态 这里,可以先把机器配置为以前的x.x.x.x的网络,确保ceph集群是可以通的.这里可以执行下面的命令查看是否连通,显示HEALTH_OK则表示连通 2.获取monma ...
- C# 连蒙带骗不知所以然的搞定USB下位机读写
公司用了一台发卡机,usb接口,半双工,给了个dll,不支持线程操作,使得UI线程老卡. 懊恼了,想自己直接通过usb读写,各种百度,然后是无数的坑,最终搞定. 现将各种坑和我自己的某些猜想记录一下, ...
- cocos大量对象使用动作注意事项
,,} for i,v in ipairs(area) do if gameResult == v then ]) do local scale_1 = cc.ScaleTo:create(0.05, ...
- 20165214 2018-2019-2 《网络对抗技术》Exp3 免杀原理与实践 Week5
<网络对抗技术>Exp3 免杀原理与实践 Week5 一.实验内容 1.正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion,加壳工具,使用shell ...
- 转自:阮一峰 Git远程操作详解
对于git的一些指令还是有些生疏,而且在课堂上讲的只是简单介绍了一些功能,并没有具体深入,看到这篇文章之后,觉得比较详细,所以转了过来. Git是目前最流行的版本管理系统,学会Git几乎成了开发者 ...
- python之三级目录
#python之三级目录低配版 menu = { '北京':{ '朝阳':{ '国贸':{ 'CICC':{ }, 'HP':{ }, '渣打银行':{ }, 'CCTV':{ }, }, '望京': ...
- ATL编程初级教程(图文事例)(VC6)
http://blog.csdn.net/titilima/archive/2004/07/18/44273.aspx 介绍 本教程的目的是告诉你如何使用ATL创建一个COM服务器,并使用Visual ...
- MySQL在高内存、IO利用率上的几个优化点
以下优化都是基于CentOS系统下的一些MySQL优化整理,有不全或有争议的地方望继续补充完善. 一.mysql层面优化 1. innodb_flush_log_at_trx_commit 设置为2设 ...