这篇文章主要介绍了c#实现识别图片上的验证码数字的方法,本文给大家汇总了2种方法,有需要的小伙伴可以参考下。

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#实现识别图片上的验证码数字的更多相关文章

  1. Python3.x:如何识别图片上的文字

    Python3.x:如何识别图片上的文字 安装pytesseract库,必须先安装其依赖的PIL及tesseract-ocr,其中PIL为图像处理库,而后面的tesseract-ocr则为google ...

  2. python 识别图片上的数字

    https://blog.csdn.net/qq_31446377/article/details/81708006 ython 3.6 版本 Pytesseract 图像验证码识别 环境: (1) ...

  3. C#识别图片上的数字

    通过Emgu实现对图片上的数字进行识别. 前期步骤: 1.下载Emgu安装文件,我的版本是2.4.2.1777.3.0版本则实现对中文的支持. 2.安装后需填写环境变量,环境变量Path值后加入Emg ...

  4. 分享C#识别图片上的数字

    通过Emgu实现对图片上的数字进行识别.前期步骤:1.下载Emgu安装文件,我的版本是2.4.2.1777.3.0版本则实现对中文的支持.2.安装后需填写环境变量,环境变量Path值后加入Emgu安装 ...

  5. 如何大批量的识别图片上的文字,批量图片文字识别OCR软件系统

    软件不需要安装,直接双击打开就可以用,废话不多说直接上图好了,方便说明问题 批量图片OCR(批量名片识别.批量照片识别等)识别,然后就下来研究了一下,下面是成果 使用步骤:打开单个图片识别,导入文件夹 ...

  6. 机器学习进阶-项目实战-信用卡数字识别 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表示需要填写的 ...

  7. PHP识别简单的图片上面的数字(可扩展)

    1.场景 最近在学习图片处理,就是特意把数字生成一个图片,然后再用程序去识别图片的数字.这就有了一下的学习过程. 2.原理分析 2.1 首先是将图片像素化,二值化,然后和字模去对比(需要相对于配置字模 ...

  8. KNN识别图像上的数字及python实现

    领导让我每天手工录入BI系统中的数据并判断数据是否存在异常,若有异常点,则检测是系统问题还是业务问题.为了解放双手,我决定写个程序完成每天录入管理驾驶舱数据的任务.首先用按键精灵录了一套脚本把系统中的 ...

  9. python爬虫20 | 小帅b教你如何使用python识别图片验证码

    当你在爬取某些网站的时候 对于你的一些频繁请求 对方会阻碍你 常见的方式就是使用验证码 验证码的主要功能 就是区分你是人还是鬼(机器人) 人 想法设法的搞一些手段来对付技术 而 技术又能对付人们的想法 ...

随机推荐

  1. int 跟 Integer 的关系

    Integer是对象 Int是类型 比如 boolean 和Boolean就也不一样,long和Long等等 作为参数传递时要注意 要进行转换如下 int到Integer: int a=3; Inte ...

  2. DevExpress v18.2新版亮点——DevExtreme篇(二)

    行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExtreme Complete Sub ...

  3. struts拦截器的知识

    如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Action里面必须最后一定要引用struts2自带的拦截器缺省堆栈defaultStack,如下(这里我是引用了str ...

  4. 以方法调用的原理解释Ruby中“puts ‘Hello‘”

    这里尽管缺少消息发送所需要的点(.)以及该消息的显示接收者,却依然发送了消息puts并传递了参数“Hello”给一个对象:默认对象self.在程序运行期间,虽然作为self的对象通过特定规则发生改变, ...

  5. Oracle启动和关闭数据库

    本机只安装一个数据库的情况下sqlplus / as sysdba启动数据库startup关闭数据库shutdown immediate

  6. python模块之_正则 re_configparser_logging_hashlib

    正则表达式的内容放在最下面了 configparser 模块: #!/usr/bin/env python # coding:utf-8 import configparser # 专门用于操作配置文 ...

  7. Python 从入门到实践 试一试 参考代码

    这两天学习Python 看了python从入门到实践的书籍,里面有课后题“试一试” 然后就跟着写了,代码在以下地址,如果需要自取 https://files.cnblogs.com/files/fud ...

  8. [JsonSchema] 关于接口测试 Json 格式比对核心算法实现 (Java 版)

    引言 为什么要自己重新造轮子,而不是采用第三方的JsonSchema方法进行实现存在以下痛点:1.我之前在网上找了很久,没有找到java版直接进行jsonschema生成的方法或直接比较的方法2.ht ...

  9. 解决 Linux 桌面亮度调整不工作

    工作原因开始使用Ubuntu.桌面环境为GNOME,不过亮度调整和桌面环境没多大关系. 思路: 不管是GNOME还是Unity,都会尝试自己去接管亮度调整,也就是去 /sys/class/backli ...

  10. AVL Tree Insertion

    Overview AVL tree is a special binary search tree, by definition, any node, its left tree height and ...