var image = document.querySelector(".greyfont2 img"); //如果要用在greasemonkey脚本里,可以把下面的代码放在image的onload事件里
var canvas = document.createElement('canvas');
var ctx = canvas.getContext("2d");
var numbers = [ //模板,依次是0-9十个数字对应的明暗值字符串
"111000111100000001100111001001111100001111100001111100001111100001111100001111100001111100100111001100000001111000111111111111111111111111111111",
"111000111100000111100000111111100111111100111111100111111100111111100111111100111111100111111100111100000000100000000111111111111111111111111111",
"100000111000000011011111001111111001111111001111110011111100111111001111110011111100111111001111111000000001000000001111111111111111111111111111",
"100000111000000001011111001111111001111110011100000111100000011111110001111111001111111001011110001000000011100000111111111111111111111111111111",
"111110011111100011111100011111000011110010011110010011100110011100110011000000000000000000111110011111110011111110011111111111111111111111111111",
"000000001000000001001111111001111111001111111000001111000000011111110001111111001111111001011110001000000011100000111111111111111111111111111111",
"111000011110000001100111101100111111001111111001000011000000001000111000001111100001111100100111000100000001111000011111111111111111111111111111",
"100000000100000000111111100111111101111111001111110011111110111111100111111101111111001111111001111110011111110011111111111111111111111111111111",
"110000011100000001100111001100111001100011011110000011110000011100110001001111100001111100000111000100000001110000011111111111111111111111111111",
"110000111100000001000111001001111100001111100000111000100000000110000100111111100111111001101111001100000011110000111111111111111111111111111111"];
var captcha = ""; //存放识别后的验证码
canvas.width = image.width;
canvas.height = image.height;
document.body.appendChild(canvas);
ctx.drawImage(image, 0, 0);
for (var i = 0; i < 4; i++) {
var pixels = ctx.getImageData(13 * i + 7, 3, 9, 16).data;
var ldString = "";
for (var j = 0,length = pixels.length; j < length; j += 4) {
ldString = ldString + (+(pixels[j] * 0.3 + pixels[j + 1] * 0.59 + pixels[j + 2] * 0.11 >= 140));
}
var comms = numbers.map(function (value) { //为了100%识别率,这里不能直接判断是否和模板字符串相等,因为可能有个别0被计算成1,或者相反
return ldString.split("").filter(function (v, index) {
return value[index] === v
}).length
});
captcha += comms.indexOf(Math.max.apply(null, comms)); //添加到识别好的验证码中
}
document.querySelector("input[name=validateCode]").value = captcha; //写入目标文本框麻烦把这段代码改写成ASP.NET版本的,麻烦各位大侠了!
var canvas = document.createElement('canvas');
var ctx = canvas.getContext("2d");
var numbers = [ //模板,依次是0-9十个数字对应的明暗值字符串
"111000111100000001100111001001111100001111100001111100001111100001111100001111100001111100100111001100000001111000111111111111111111111111111111",
"111000111100000111100000111111100111111100111111100111111100111111100111111100111111100111111100111100000000100000000111111111111111111111111111",
"100000111000000011011111001111111001111111001111110011111100111111001111110011111100111111001111111000000001000000001111111111111111111111111111",
"100000111000000001011111001111111001111110011100000111100000011111110001111111001111111001011110001000000011100000111111111111111111111111111111",
"111110011111100011111100011111000011110010011110010011100110011100110011000000000000000000111110011111110011111110011111111111111111111111111111",
"000000001000000001001111111001111111001111111000001111000000011111110001111111001111111001011110001000000011100000111111111111111111111111111111",
"111000011110000001100111101100111111001111111001000011000000001000111000001111100001111100100111000100000001111000011111111111111111111111111111",
"100000000100000000111111100111111101111111001111110011111110111111100111111101111111001111111001111110011111110011111111111111111111111111111111",
"110000011100000001100111001100111001100011011110000011110000011100110001001111100001111100000111000100000001110000011111111111111111111111111111",
"110000111100000001000111001001111100001111100000111000100000000110000100111111100111111001101111001100000011110000111111111111111111111111111111"];
var captcha = ""; //存放识别后的验证码
canvas.width = image.width;
canvas.height = image.height;
document.body.appendChild(canvas);
ctx.drawImage(image, 0, 0);
for (var i = 0; i < 4; i++) {
var pixels = ctx.getImageData(13 * i + 7, 3, 9, 16).data;
var ldString = "";
for (var j = 0,length = pixels.length; j < length; j += 4) {
ldString = ldString + (+(pixels[j] * 0.3 + pixels[j + 1] * 0.59 + pixels[j + 2] * 0.11 >= 140));
}
var comms = numbers.map(function (value) { //为了100%识别率,这里不能直接判断是否和模板字符串相等,因为可能有个别0被计算成1,或者相反
return ldString.split("").filter(function (v, index) {
return value[index] === v
}).length
});
captcha += comms.indexOf(Math.max.apply(null, comms)); //添加到识别好的验证码中
}
document.querySelector("input[name=validateCode]").value = captcha; //写入目标文本框麻烦把这段代码改写成ASP.NET版本的,麻烦各位大侠了!
解决方案 »
- DataTable 和DataTable
- Asp.Net 底层架构
- sql语句访问数据库进行删除数据处理的问题
- 消息附件怎么用数据库处理?
- 一个复选框删除的问题。。。谢谢各位啦。。。在线急等。。。
- 如何在后台改变页面文本框的值
- 一个大难题,问了好多人,都解决不了,真不知道怎么办?求各位高手帮忙解决一下!
- 很急的问题,拜托大家救救我,在FAT32下怎么给文件夹增加ASPNET权限
- 救急!!!毕设临交了,两个问题
- 如何把WebForm1中的控件状态传递给WebForm2?100分送上
- stream 以图片的格式下载到本地
- 【急】上传控件jquery uploadify,上传的时候报HTTP Error错误~~
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web.UI;
using System.Drawing.Drawing2D;
using System.IO;
namespace Validate
{
/// <summary>
/// 生成验证码的类
/// </summary>
public class ValidateNumber
{
public ValidateNumber()
{
}
/// <summary>
/// 验证码的最大长度
/// </summary>
public int MaxLength
{
get { return 10; }
}
/// <summary>
/// 验证码的最小长度
/// </summary>
public int MinLength
{
get { return 1; }
}
/// <summary>
/// 生成验证码
/// </summary>
/// <param name="length">指定验证码的长度</param>
/// <returns></returns>
public string CreateValidateNumber(int length)
{
int[] randMembers = new int[length]; //随机生成的数字集合
int[] validateNums = new int[length]; //抽取出来的随机数字集合
string validateNumberStr = "";
//生成起始序列值
int seekSeek = unchecked((int)DateTime.Now.Ticks);
Random seekRand = new Random(seekSeek);
int beginSeek = (int)seekRand.Next(0, Int32.MaxValue - length * 10000);
int[] seeks = new int[length];
for (int i = 0; i < length; i++)
{
beginSeek += 10000;
seeks[i] = beginSeek;
}
//生成随机数字
for (int i = 0; i < length; i++)
{
Random rand = new Random(seeks[i]);
int pownum = 1 * (int)Math.Pow(10, length);
randMembers[i] = rand.Next(pownum, Int32.MaxValue);
}
//抽取随机数字
for (int i = 0; i < length; i++)
{
string numStr = randMembers[i].ToString();
int numLength = numStr.Length;
Random rand = new Random();
int numPosition = rand.Next(0, numLength - 1);
validateNums[i] = Int32.Parse(numStr.Substring(numPosition, 1));
}
//生成验证码
for (int i = 0; i < length; i++)
{
validateNumberStr += validateNums[i].ToString();
}
return validateNumberStr;
}
/// <summary>
/// 创建验证码的图片
/// </summary>
/// <param name="containsPage">要输出到的page对象</param>
/// <param name="validateNum">验证码</param>
public void CreateValidateGraphic(Page containsPage, string validateNum)
{
Bitmap image = new Bitmap((int)Math.Ceiling(validateNum.Length * 12.5), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的干扰线
for (int i = 0; i < 25; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
Font font = new Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(validateNum, font, brush, 3, 2);
//画图片的前景干扰点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
//保存图片数据
MemoryStream stream = new MemoryStream();
image.Save(stream, ImageFormat.Jpeg);
//输出图片
containsPage.Response.Clear();
containsPage.Response.ContentType = "image/jpeg";
containsPage.Response.BinaryWrite(stream.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}
/// <summary>
/// 得到验证码图片的长度
/// </summary>
/// <param name="validateNumLength">验证码的长度</param>
/// <returns></returns>
public static int GetImageWidth(int validateNumLength)
{
return (int)(validateNumLength * 12.5);
}
/// <summary>
/// 得到验证码的高度
/// </summary>
/// <returns></returns>
public static double GetImageHeight()
{
return 22.5;
}
}
}
既然知道你的需求了,识别验证码,那么去百度一个c#版本的不就行了么?
纯用C#写了CSDN验证码识别程序!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;namespace 验证码处理
{
class VerifyCode
{
public Bitmap bmpobj; public VerifyCode(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); ////输出
//GCHandle gch = GCHandle.Alloc(pixels, GCHandleType.Pinned);
//bmpOutput = new Bitmap(bmpobj.Width, bmpobj.Height, bmpData.Stride, bmpData.PixelFormat, gch.AddrOfPinnedObject());
//gch.Free();
} /// <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;
} /// <summary>
/// 得到灰度图像前景背景的临界值 最大类间方差法
/// </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; // 阈值
//生成直方图
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;
}
/// <summary>
/// 去掉杂点(适合杂点/杂线粗为1)
/// </summary>
/// <param name="dgGrayValue">背前景灰色界限</param>
/// <returns></returns>
public void ClearNoise(int dgGrayValue, int MaxNearPoints)
{
Color piexl;
int nearDots = 0;
//逐点判断
for (int i = 0; i < bmpobj.Width; i++)
for (int j = 0; j < bmpobj.Height; j++)
{
piexl = bmpobj.GetPixel(i, j);
if (piexl.R < dgGrayValue)
{
nearDots = 0;
//判断周围8个点是否全为空
if (i == 0 || i == bmpobj.Width - 1 || j == 0 || j == bmpobj.Height - 1) //边框全去掉
{
bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255));
}
else
{
if (bmpobj.GetPixel(i - 1, j - 1).R < dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i, j - 1).R < dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i + 1, j - 1).R < dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i - 1, j).R < dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i + 1, j).R < dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i - 1, j + 1).R < dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i, j + 1).R < dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i + 1, j + 1).R < dgGrayValue) nearDots++;
} if (nearDots < MaxNearPoints)
bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255)); //去掉单点 && 粗细小3邻边点
}
else //背景
bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255));
}
} /// <summary>
/// 3×3中值滤波除杂
/// </summary>
/// <param name="dgGrayValue"></param>
public void ClearNoise(int dgGrayValue)
{
int x, y;
byte[] p = new byte[9]; //最小处理窗口3*3
byte s;
//byte[] lpTemp=new BYTE[nByteWidth*nHeight];
int i, j;
//--!!!!!!!!!!!!!!下面开始窗口为3×3中值滤波!!!!!!!!!!!!!!!!
for (y = 1; y < bmpobj.Height - 1; y++) //--第一行和最后一行无法取窗口
{
for (x = 1; x < bmpobj.Width - 1; x++)
{
//取9个点的值
p[0] = bmpobj.GetPixel(x - 1, y - 1).R;
p[1] = bmpobj.GetPixel(x, y - 1).R;
p[2] = bmpobj.GetPixel(x + 1, y - 1).R;
p[3] = bmpobj.GetPixel(x - 1, y).R;
p[4] = bmpobj.GetPixel(x, y).R;
p[5] = bmpobj.GetPixel(x + 1, y).R;
p[6] = bmpobj.GetPixel(x - 1, y + 1).R;
p[7] = bmpobj.GetPixel(x, y + 1).R;
p[8] = bmpobj.GetPixel(x + 1, y + 1).R;
//计算中值
for (j = 0; j < 5; j++)
{
for (i = j + 1; i < 9; i++)
{
if (p[j] > p[i])
{
s = p[j];
p[j] = p[i];
p[i] = s;
}
}
}
// if (bmpobj.GetPixel(x, y).R < dgGrayValue)
bmpobj.SetPixel(x, y, Color.FromArgb(p[4], p[4], p[4])); //给有效值付中值
}
}
}
这里还有个扭曲验证码的类
public class Twist
{
/// <summary>
/// 正弦曲线Wave扭曲图片
/// </summary>
/// <param name="srcBmp">图片路径</param>
/// <param name="bXDir">沿Y轴扭曲则选择为True</param>
/// <param name="dMultValue">波形的幅度倍数,越大扭曲的程度越高,一般为3</param>
/// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>
/// <returns>扭曲后的位图</returns>
public Bitmap TwistBitmap(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
{
int w = srcBmp.Width;
int h = srcBmp.Height;
System.Drawing.Bitmap destBmp = new Bitmap(w, h);
using (System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp))
{
graph.Clear(Color.White);
}
double dBaseAxisLen = bXDir ? (double)h : (double)w;
BitmapData destData = destBmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, srcBmp.PixelFormat);
BitmapData srcData = srcBmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, srcBmp.PixelFormat);
//调式一定注意PixelFormat是多少,别处都没有解释的。如果是24下面指针和跨度就3倍,要是32就是4倍。
unsafe
{
byte* p = (byte*)srcData.Scan0;
byte* p2 = (byte*)destData.Scan0;
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
double dx = 0;
dx = bXDir ? (Math.PI * (double)j * 2) / dBaseAxisLen : (Math.PI * (double)i * 2) / dBaseAxisLen;
dx += dPhase;
double dy = Math.Sin(dx);
// 取得当前点
int nOldX = 0, nOldY = 0;
nOldX = bXDir ? i + (int)(dy * dMultValue) : i;
nOldY = bXDir ? j : j + (int)(dy * dMultValue); if ((nOldX >= 0 && nOldX < w) && (nOldY >= 0 && nOldY < h))
{
p2[(nOldY * destData.Stride) + (nOldX * 4)] = p[(j * srcData.Stride) + (i * 4)];
p2[(nOldY * destData.Stride) + (nOldX * 4) + 1] = p[(j * srcData.Stride) + (i * 4) + 1];
p2[(nOldY * destData.Stride) + (nOldX * 4) + 2] = p[(j * srcData.Stride) + (i * 4) + 2];
}
}
}
}
destBmp.UnlockBits(destData);
srcBmp.UnlockBits(srcData);
if (srcBmp != null)
srcBmp.Dispose();
return destBmp;
}
}
/// 该函数用于对图像进行腐蚀运算。结构元素为水平方向或垂直方向的三个点,
/// 中间点位于原点;或者由用户自己定义3×3的结构元素。
/// </summary>
/// <param name="dgGrayValue">前后景临界值</param>
/// <param name="nMode">腐蚀方式:0表示水平方向,1垂直方向,2自定义结构元素。</param>
/// <param name="structure"> 自定义的3×3结构元素</param>
public void ErosionPic(int dgGrayValue, int nMode, bool[,] structure)
{
int lWidth = bmpobj.Width;
int lHeight = bmpobj.Height;
Bitmap newBmp = new Bitmap(lWidth, lHeight); int i, j, n, m; if (nMode == 0)
{
for (j = 0; j < lHeight; j++)
{
for (i = 1; i < lWidth - 1; i++)
{
//目标图像中的当前点先赋成黑色
newBmp.SetPixel(i, j, Color.Black); //如果源图像中当前点自身或者左右有一个点不是黑色,
//则将目标图像中的当前点赋成白色
if (bmpobj.GetPixel(i - 1, j).R > dgGrayValue ||
bmpobj.GetPixel(i, j).R > dgGrayValue ||
bmpobj.GetPixel(i + 1, j).R > dgGrayValue)
newBmp.SetPixel(i, j, Color.White);
}
}
}
else if (nMode == 1)
{
//使用垂真方向的结构元素进行腐蚀
// 由于使用3×1的结构元素,为防止越界,所以不处理最上边和最下边的两行像素
for (j = 1; j < lHeight - 1; j++)
{
for (i = 0; i < lWidth; i++)
{
//目标图像中的当前点先赋成黑色
newBmp.SetPixel(i, j, Color.Black); //如果源图像中当前点自身或者左右有一个点不是黑色,
//则将目标图像中的当前点赋成白色
if (bmpobj.GetPixel(i, j - 1).R > dgGrayValue ||
bmpobj.GetPixel(i, j).R > dgGrayValue ||
bmpobj.GetPixel(i, j + 1).R > dgGrayValue)
newBmp.SetPixel(i, j, Color.White);
}
}
}
else
{
if (structure.Length != 9) //检查自定义结构
return;
for (j = 1; j < lHeight - 1; j++)
{
for (i = 1; i < lWidth - 1; i++)
{
//目标图像中的当前点先赋成黑色
newBmp.SetPixel(i, j, Color.Black);
//如果原图像中对应结构元素中为黑色的那些点中有一个不是黑色,
//则将目标图像中的当前点赋成白色
for (m = 0; m < 3; m++)
{
for (n = 0; n < 3; n++)
{
if (!structure[m, n])
continue;
if (bmpobj.GetPixel(i + m - 1, j + n - 1).R > dgGrayValue)
{
newBmp.SetPixel(i, j, Color.White);
break;
}
}
}
}
}
}
bmpobj = newBmp;
}
/// <summary>
/// 该函数用于对图像进行细化运算。要求目标图像为灰度图像
/// </summary>
/// <param name="dgGrayValue"></param>
public void ThiningPic(int dgGrayValue)
{
int lWidth = bmpobj.Width;
int lHeight = bmpobj.Height;
bool bModified;
int i, j, n, m; //四个条件
bool bCondition1;
bool bCondition2;
bool bCondition3;
bool bCondition4;
int nCount; //计数器
int[,] neighbour = new int[5, 5]; //5×5相邻区域像素值
bModified = true;
while (bModified)
{
bModified = false; //由于使用5×5的结构元素,为防止越界,所以不处理外围的几行和几列像素
for (j = 2; j < lHeight - 2; j++)
{
for (i = 2; i < lWidth - 2; i++)
{
bCondition1 = false;
bCondition2 = false;
bCondition3 = false;
bCondition4 = false; if (bmpobj.GetPixel(i, j).R > dgGrayValue)
{
if (bmpobj.GetPixel(i, j).R < 255)
bmpobj.SetPixel(i, j, Color.White);
continue;
} //获得当前点相邻的5×5区域内像素值,白色用0代表,黑色用1代表
for (m = 0; m < 5; m++)
{
for (n = 0; n < 5; n++)
{
neighbour[m, n] = bmpobj.GetPixel(i + m - 2, j + n - 2).R < dgGrayValue ? 1 : 0;
}
} //逐个判断条件。
//判断2<=NZ(P1)<=6
nCount = neighbour[1, 1] + neighbour[1, 2] + neighbour[1, 3]
+ neighbour[2, 1] + neighbour[2, 3] +
+neighbour[3, 1] + neighbour[3, 2] + neighbour[3, 3];
if (nCount >= 2 && nCount <= 6)
{
bCondition1 = true;
} //判断Z0(P1)=1
nCount = 0;
if (neighbour[1, 2] == 0 && neighbour[1, 1] == 1)
nCount++;
if (neighbour[1, 1] == 0 && neighbour[2, 1] == 1)
nCount++;
if (neighbour[2, 1] == 0 && neighbour[3, 1] == 1)
nCount++;
if (neighbour[3, 1] == 0 && neighbour[3, 2] == 1)
nCount++;
if (neighbour[3, 2] == 0 && neighbour[3, 3] == 1)
nCount++;
if (neighbour[3, 3] == 0 && neighbour[2, 3] == 1)
nCount++;
if (neighbour[2, 3] == 0 && neighbour[1, 3] == 1)
nCount++;
if (neighbour[1, 3] == 0 && neighbour[1, 2] == 1)
nCount++;
if (nCount == 1)
bCondition2 = true; //判断P2*P4*P8=0 or Z0(p2)!=1
if (neighbour[1, 2] * neighbour[2, 1] * neighbour[2, 3] == 0)
{
bCondition3 = true;
}
else
{
nCount = 0;
if (neighbour[0, 2] == 0 && neighbour[0, 1] == 1)
nCount++;
if (neighbour[0, 1] == 0 && neighbour[1, 1] == 1)
nCount++;
if (neighbour[1, 1] == 0 && neighbour[2, 1] == 1)
nCount++;
if (neighbour[2, 1] == 0 && neighbour[2, 2] == 1)
nCount++;
if (neighbour[2, 2] == 0 && neighbour[2, 3] == 1)
nCount++;
if (neighbour[2, 3] == 0 && neighbour[1, 3] == 1)
nCount++;
if (neighbour[1, 3] == 0 && neighbour[0, 3] == 1)
nCount++;
if (neighbour[0, 3] == 0 && neighbour[0, 2] == 1)
nCount++;
if (nCount != 1)
bCondition3 = true;
} //判断P2*P4*P6=0 or Z0(p4)!=1
if (neighbour[1, 2] * neighbour[2, 1] * neighbour[3, 2] == 0)
{
bCondition4 = true;
}
else
{
nCount = 0;
if (neighbour[1, 1] == 0 && neighbour[1, 0] == 1)
nCount++;
if (neighbour[1, 0] == 0 && neighbour[2, 0] == 1)
nCount++;
if (neighbour[2, 0] == 0 && neighbour[3, 0] == 1)
nCount++;
if (neighbour[3, 0] == 0 && neighbour[3, 1] == 1)
nCount++;
if (neighbour[3, 1] == 0 && neighbour[3, 2] == 1)
nCount++;
if (neighbour[3, 2] == 0 && neighbour[2, 2] == 1)
nCount++;
if (neighbour[2, 2] == 0 && neighbour[1, 2] == 1)
nCount++;
if (neighbour[1, 2] == 0 && neighbour[1, 1] == 1)
nCount++;
if (nCount != 1)
bCondition4 = true;
} if (bCondition1 && bCondition2 && bCondition3 && bCondition4)
{
bmpobj.SetPixel(i, j, Color.White);
bModified = true;
}
else
{
bmpobj.SetPixel(i, j, Color.Black);
}
}
}
}
// 复制细化后的图像
// bmpobj = newBmp;
}