c#实现识别图片上的验证码数字
2021-06-25 11:06
标签:void 颜色 orm namespace ack 验证 idt oar runtime 这篇文章主要介绍了c#实现识别图片上的验证码数字的方法,本文给大家汇总了2种方法,有需要的小伙伴可以参考下。 C#识别验证码图片通用类 以上2则都是使用C#实现的orc识别的代码,希望对大家学习C#有所帮助。 ********转载:https://m.jb51.net/article/74533.htm c#实现识别图片上的验证码数字 标签:void 颜色 orm namespace ack 验证 idt oar runtime 原文地址:https://www.cnblogs.com/linybo/p/10126847.htmlpublic void imgdo(Bitmap img)
{
//去色
Bitmap btp = img;
Color c = new Color();
int rr, gg, bb;
for (int i = 0; i )
{
for (int j = 0; 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 )
{
for (int j = 0; 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 )
{
for (int x = 0; 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 )
{
for (int x = 0; 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(" ", "");
}
}
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
}
///
下一篇:angular 图片上传的功能