http://blog.csdn.net/stevenkylelee/article/details/8263890
------------------------------------------------
http://outofmemory.cn/code-snippet/2037/c-pojie-yanzhengma-example-code
http://outofmemory.cn/code-snippet/3086/C-yanzheng-code-identify-class
http://www.sufeinet.com/thread-1514-1-1.html
http://lichengguizy.blog.163.com/blog/static/117718586201201111404824/
--------------------------------------
public class GetImageValue
{
//设定图片RGB字符串
string[] ArrayList = new string[]{
"00011100011111110110001111000001110000011100000111000001110000011100000111000001011000110111111100011100", //0
"00111000111110001111100000011000000110000001100000011000000110000001100000011000000110001111111111111111", //1
"01111100111111101000001100000011000000110000011000001100000110000011000001100000110000001111111111111111", //2
"01111100111111111000001100000011000001100111100001111110000001110000001100000011100001111111111001111100", //3
"00001100000111000001110000111100011011000110110010001100110011001111111110111111000011000000110000001100", //4
"11111111111111111100000011000000110000001111100011111110000001110000001100000011100001111111111001111100", //5
"00011110001111110110000101100000110000001101111011101111111000111100000101000001011000110110111100011110", //6
"01111010001111110000000100000000000000110000011000000100000011000000100000011000000110000011000000110000", //7
"00111110011111110110001101100011011100100011111000111110011001111100000111000001111000110111111100111110", //8
"00011100011110110110001111000001110000011010001101111111001111010000000100000001010000110101110000111100", //9
"00111000111111101000010010000011100000111000001110000011000000111000001110000011110001001111011000111000", //10
"00001100011111000111110000001100000011000000110000001100000011000000110000001100000011000111110101111111", //11
"11111000110111000000010000000110000001100000110000011000001100000110000011000000100000000101111011111110", //12
"10111000111111100000001000000110000011001111000011110100000011100000011000000110000011101111110011111000", //13
"00000110000011100000111000001110000101100011011000100110011001001111111111111111000001000000011000000110", //14
"11111110111111101000000010000000100000001111000011110100000011100000011000000110000011101111110011111000", //15
"00111100011111101100001011000000100000001011110011111110110001111000001110000011110001101111111000111100", //16
"11101111111111110000001100000010000001100000010000001000000110000001000000110000001100000110000001100000", //17
"01111100111111101100011011000110110001000111110001111100110011101000001110000011110001101101111001111100", //18
"01111000111111101100011010000011100000111100011111111111011110110000001100000100100001101111110001111000", //19
"00111100011111111100001110000001110000011110001101111111001101010000000000000011010000010111111000111100", //20=9
"00111000111111001100011010000011100000011000001100000011100000111000001010000011110001101111010000101000", //21=0
"00111000010111010110001111000001110000011110001101111111001111010000000100000011010000100111011000111100", //22=9
"00000110000010100000111000010110001101100011011001000110011001101011101111111111000001100000011000000110", //23=4
"00011110001011110110000101000000110000001101101011111101011000111100000110000001010000110101111000011110", //24=6
"00111100011111101110001101000001110000011110001101111111001111010000000100000011000000110101101000111100", //25=9
"11111000111100000000011000000100000000100000110000011000001100000110000001000000100000001111110011111110" //26=2
};
/// <summary>
/// 获取图片验证码数字
/// </summary>
/// <returns></returns>
public string GetImageValues()
{
string url = "http://xxxx.xxxx/image";
WebRequest myWebRequest = WebRequest.Create(url);
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream ReceiveStream = myWebResponse.GetResponseStream();
Bitmap map = new Bitmap(ReceiveStream, false);
UnCodebase ucode = new UnCodebase(map);
ucode.GrayByPixels(); //灰度处理
Bitmap[] pics = ucode.readMap();
int[] gray = new int[4];
for (int j = 0; j < 4; j++)
{
gray[j] = ucode.GetSingleDgGrayValue(pics[j]);
}
string[] arr = new string[4];
for (int i = 0; i < 4; i++)
{
arr[i] = ucode.GetSingleBmpCode(pics[i], gray[i]);
}
string picnum = getPicnums(arr);
return picnum;
}
public string getPicnums(string[] arr)
{
string Code = "";
for (int i = 0; i < 4; i++)
{
string code = arr[i]; //得到代码串
for (int arrayIndex = 0; arrayIndex < ArrayList.Length; arrayIndex++)
{
//逐点判断特征码是否相同,允许误差!
char temp1, temp2;
int point = 0;
if (ArrayList[arrayIndex].Equals(code))
{
point = 0;
if (arrayIndex > 9)
{
if (arrayIndex == 20 || arrayIndex == 22 || arrayIndex == 25)
{
Code = Code + "9";
}
else if (arrayIndex == 21)
{
Code = Code + "0";
}
else if (arrayIndex == 23)
{
Code = Code + "4";
}
else if (arrayIndex == 24)
{
Code = Code + "6";
}
else if (arrayIndex == 26)
{
Code = Code + "2";
}
else
{
Code = Code + (arrayIndex - 10).ToString();
}
}
else
{
Code = Code + arrayIndex.ToString();
}
break;
}
else
{
//将字符串数组,直接转为单个字符进行对比,并记录不相同的点
for (int Comparison = 0; Comparison < code.Length; Comparison++)
{
temp1 = arr[i][Comparison];
temp2 = ArrayList[arrayIndex][Comparison];
if (temp1 != temp2)
{
point = point + 1;
}
}
}
//当不相同点的值小于10的时候,也就是说误差点小于10的时候则直接等于此数字,否则将跳出循环继续对下一个特征码进行判断
if (point < 10)
{
if (arrayIndex > 9)
{
if (arrayIndex == 20 || arrayIndex == 22 || arrayIndex == 25)
{
Code = Code + "9";
}
else if (arrayIndex == 21)
{
Code = Code + "0";
}
else if (arrayIndex == 23)
{
Code = Code + "4";
}
else if (arrayIndex == 24)
{
Code = Code + "6";
}
else if (arrayIndex == 26)
{
Code = Code + "2";
}
else
{
Code = Code + (arrayIndex - 10).ToString();
}
}
else
{
Code = Code + arrayIndex.ToString();
}
break;
}
}
}
return Code;
}
-------------------------图片处理类
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[i]));
pixels[i] = (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;
}
public Bitmap[] readMap()
{
string str;
RectangleF[] block = new RectangleF[4];
block[0] = new Rectangle(7, 3, 8, 13);
block[1] = new Rectangle(20, 3, 8, 13);
block[2] = new Rectangle(33, 3, 8, 13);
block[3] = new Rectangle(47, 3, 8, 13);
//分别克隆图片的四个部分
Bitmap[] s = new Bitmap[4];
s[0] = bmpobj.Clone(block[0], PixelFormat.DontCare);
s[1] = bmpobj.Clone(block[1], PixelFormat.DontCare);
s[2] = bmpobj.Clone(block[2], PixelFormat.DontCare);
s[3] = bmpobj.Clone(block[3], PixelFormat.DontCare);
return s;
}
/**/
/// <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;
}
/// <summary>
/// 得到单个灰度图像前景背景的临界值 最大类间方差法,yuanbao,2007.08
/// </summary>
/// <returns>前景背景的临界值</returns>
public int GetSingleDgGrayValue(Bitmap singlepic)
{
int[] pixelNum = new int[256]; //图象直方图,共256个点
int n, n1, n2;
int total; //total为总和,累计值
double m1, m2, sum, csum, fmax, sb; //sb为类间方差,fmax存储最大方差值
int k, t, q;
int threshValue = 1; // 阈值
int step = 1;
//生成直方图
for (int i = 0; i < singlepic.Width; i++)
{
for (int j = 0; j < singlepic.Height; j++)
{
//返回各个点的颜色,以RGB表示
pixelNum[singlepic.GetPixel(i, j).R]++; //相应的直方图加1
}
}
//直方图平滑化
for (k = 0; k <= 255; k++)
{
total = 0;
for (t = -2; t <= 2; t++) //与附近2个灰度做平滑化,t值应取较小的值
{
q = k + t;
if (q < 0) //越界处理
q = 0;
if (q > 255)
q = 255;
total = total + pixelNum[q]; //total为总和,累计值
}
pixelNum[k] = (int)((float)total / 5.0 + 0.5); //平滑化,左边2个+中间1个+右边2个灰度,共5个,所以总和除以5,后面加0.5是用修正值
}
//求阈值
sum = csum = 0.0;
n = 0;
//计算总的图象的点数和质量矩,为后面的计算做准备
for (k = 0; k <= 255; k++)
{
sum += (double)k * (double)pixelNum[k]; //x*f(x)质量矩,也就是每个灰度的值乘以其点数(归一化后为概率),sum为其总和
n += pixelNum[k]; //n为图象总的点数,归一化后就是累积概率
}
fmax = -1.0; //类间方差sb不可能为负,所以fmax初始值为-1不影响计算的进行
n1 = 0;
for (k = 0; k < 256; k++) //对每个灰度(从0到255)计算一次分割后的类间方差sb
{
n1 += pixelNum[k]; //n1为在当前阈值遍前景图象的点数
if (n1 == 0) { continue; } //没有分出前景后景
n2 = n - n1; //n2为背景图象的点数
if (n2 == 0) { break; } //n2为0表示全部都是后景图象,与n1=0情况类似,之后的遍历不可能使前景点数增加,所以此时可以退出循环
csum += (double)k * pixelNum[k]; //前景的“灰度的值*其点数”的总和
m1 = csum / n1; //m1为前景的平均灰度
m2 = (sum - csum) / n2; //m2为背景的平均灰度
sb = (double)n1 * (double)n2 * (m1 - m2) * (m1 - m2); //sb为类间方差
if (sb > fmax) //如果算出的类间方差大于前一次算出的类间方差
{
fmax = sb; //fmax始终为最大类间方差(otsu)
threshValue = k; //取最大类间方差时对应的灰度的k就是最佳阈值
}
}
return threshValue;
}
/// <summary>
/// 得到灰度图像前景背景的临界值 最大类间方差法,yuanbao,2007.08
/// </summary>
/// <returns>前景背景的临界值</returns>
public int GetDgGrayValue()
{
int[] pixelNum = new int[256]; //图象直方图,共256个点
int n, n1, n2;
int total; //total为总和,累计值
double m1, m2, sum, csum, fmax, sb; //sb为类间方差,fmax存储最大方差值
int k, t, q;
int threshValue = 1; // 阈值
int step = 1;
//生成直方图
for (int i = 0; i < bmpobj.Width; i++)
{
for (int j = 0; j < bmpobj.Height; j++)
{
//返回各个点的颜色,以RGB表示
pixelNum[bmpobj.GetPixel(i, j).R]++; //相应的直方图加1
}
}
//直方图平滑化
for (k = 0; k <= 255; k++)
{
total = 0;
for (t = -2; t <= 2; t++) //与附近2个灰度做平滑化,t值应取较小的值
{
q = k + t;
if (q < 0) //越界处理
q = 0;
if (q > 255)
q = 255;
total = total + pixelNum[q]; //total为总和,累计值
}
pixelNum[k] = (int)((float)total / 5.0 + 0.5); //平滑化,左边2个+中间1个+右边2个灰度,共5个,所以总和除以5,后面加0.5是用修正值
}
//求阈值
sum = csum = 0.0;
n = 0;
//计算总的图象的点数和质量矩,为后面的计算做准备
for (k = 0; k <= 255; k++)
{
sum += (double)k * (double)pixelNum[k]; //x*f(x)质量矩,也就是每个灰度的值乘以其点数(归一化后为概率),sum为其总和
n += pixelNum[k]; //n为图象总的点数,归一化后就是累积概率
}
fmax = -1.0; //类间方差sb不可能为负,所以fmax初始值为-1不影响计算的进行
n1 = 0;
for (k = 0; k < 256; k++) //对每个灰度(从0到255)计算一次分割后的类间方差sb
{
n1 += pixelNum[k]; //n1为在当前阈值遍前景图象的点数
if (n1 == 0) { continue; } //没有分出前景后景
n2 = n - n1; //n2为背景图象的点数
if (n2 == 0) { break; } //n2为0表示全部都是后景图象,与n1=0情况类似,之后的遍历不可能使前景点数增加,所以此时可以退出循环
csum += (double)k * pixelNum[k]; //前景的“灰度的值*其点数”的总和
m1 = csum / n1; //m1为前景的平均灰度
m2 = (sum - csum) / n2; //m2为背景的平均灰度
sb = (double)n1 * (double)n2 * (m1 - m2) * (m1 - m2); //sb为类间方差
if (sb > fmax) //如果算出的类间方差大于前一次算出的类间方差
{
fmax = sb; //fmax始终为最大类间方差(otsu)
threshValue = k; //取最大类间方差时对应的灰度的k就是最佳阈值
}
}
return threshValue;
}
}