博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 验证识别基类
阅读量:4428 次
发布时间:2019-06-07

本文共 32397 字,大约阅读时间需要 107 分钟。

原文

网上找了些代码  自己又改了下先放出来了  处理简单的验证码足够了001using System;002using System.Collections.Generic;003using System.Linq;004using System.Text;005using System.Drawing;006using System.Drawing.Imaging;007using System.Runtime.InteropServices;008 009namespace 验证码处理010{011    class VerifyCode012    {013        public Bitmap bmpobj;014 015        public VerifyCode(Bitmap pic)016        {017            bmpobj = new Bitmap(pic);    //转换为Format32bppRgb018        }019 020        /// 021        /// 根据RGB,计算灰度值022        /// 023        /// Color值024        /// 
灰度值,整型
025 private int GetGrayNumColor(System.Drawing.Color posClr)026 {027 return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;028 }029 030 /// 031 /// 灰度转换,逐点方式032 /// 033 public void GrayByPixels()034 {035 for (int i = 0; i < bmpobj.Height; i++)036 {037 for (int j = 0; j < bmpobj.Width; j++)038 {039 int tmpValue = GetGrayNumColor(bmpobj.GetPixel(j, i));040 bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue, tmpValue, tmpValue));041 }042 }043 }044 045 /// 046 /// 去图形边框047 /// 048 /// 049 public void ClearPicBorder(int borderWidth)050 {051 for (int i = 0; i < bmpobj.Height; i++)052 {053 for (int j = 0; j < bmpobj.Width; j++)054 {055 if (i < borderWidth || j < borderWidth || j > bmpobj.Width - 1 - borderWidth || i > bmpobj.Height - 1 - borderWidth)056 bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255));057 }058 }059 }060 061 /// 062 /// 灰度转换,逐行方式063 /// 064 public void GrayByLine()065 {066 Rectangle rec = new Rectangle(0, 0, bmpobj.Width, bmpobj.Height);067 BitmapData bmpData = bmpobj.LockBits(rec, ImageLockMode.ReadWrite, bmpobj.PixelFormat);// PixelFormat.Format32bppPArgb);068 // bmpData.PixelFormat = PixelFormat.Format24bppRgb;069 IntPtr scan0 = bmpData.Scan0;070 int len = bmpobj.Width * bmpobj.Height;071 int[] pixels = new int[len];072 Marshal.Copy(scan0, pixels, 0, len);073 074 //对图片进行处理075 int GrayValue = 0;076 for (int i = 0; i < len; i++)077 {078 GrayValue = GetGrayNumColor(Color.FromArgb(pixels[i]));079 pixels[i] = (byte)(Color.FromArgb(GrayValue, GrayValue, GrayValue)).ToArgb(); //Color转byte080 }081 082 bmpobj.UnlockBits(bmpData);083 084 输出085 //GCHandle gch = GCHandle.Alloc(pixels, GCHandleType.Pinned);086 //bmpOutput = new Bitmap(bmpobj.Width, bmpobj.Height, bmpData.Stride, bmpData.PixelFormat, gch.AddrOfPinnedObject());087 //gch.Free();088 }089 090 /// 091 /// 得到有效图形并调整为可平均分割的大小092 /// 093 /// 灰度背景分界值094 /// 有效字符数095 ///
096 public void GetPicValidByValue(int dgGrayValue, int CharsCount)097 {098 int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;099 int posx2 = 0; int posy2 = 0;100 for (int i = 0; i < bmpobj.Height; i++) //找有效区101 {102 for (int j = 0; j < bmpobj.Width; j++)103 {104 int pixelValue = bmpobj.GetPixel(j, i).R;105 if (pixelValue < dgGrayValue) //根据灰度值106 {107 if (posx1 > j) posx1 = j;108 if (posy1 > i) posy1 = i;109 110 if (posx2 < j) posx2 = j;111 if (posy2 < i) posy2 = i;112 };113 };114 };115 // 确保能整除116 int Span = CharsCount - (posx2 - posx1 + 1) % CharsCount; //可整除的差额数117 if (Span < CharsCount)118 {119 int leftSpan = Span / 2; //分配到左边的空列 ,如span为单数,则右边比左边大1120 if (posx1 > leftSpan)121 posx1 = posx1 - leftSpan;122 if (posx2 + Span - leftSpan < bmpobj.Width)123 posx2 = posx2 + Span - leftSpan;124 }125 //复制新图126 Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);127 bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);128 }129 130 /// 131 /// 得到有效图形,图形为类变量132 /// 133 /// 灰度背景分界值134 /// 有效字符数135 ///
136 public void GetPicValidByValue(int dgGrayValue)137 {138 int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;139 int posx2 = 0; int posy2 = 0;140 for (int i = 0; i < bmpobj.Height; i++) //找有效区141 {142 for (int j = 0; j < bmpobj.Width; j++)143 {144 int pixelValue = bmpobj.GetPixel(j, i).R;145 if (pixelValue < dgGrayValue) //根据灰度值146 {147 if (posx1 > j) posx1 = j;148 if (posy1 > i) posy1 = i;149 150 if (posx2 < j) posx2 = j;151 if (posy2 < i) posy2 = i;152 };153 };154 };155 //复制新图156 Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);157 bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);158 }159 160 /// 161 /// 得到有效图形,图形由外面传入162 /// 163 /// 灰度背景分界值164 /// 有效字符数165 ///
166 public Bitmap GetPicValidByValue(Bitmap singlepic, int dgGrayValue)167 {168 int posx1 = singlepic.Width; int posy1 = singlepic.Height;169 int posx2 = 0; int posy2 = 0;170 for (int i = 0; i < singlepic.Height; i++) //找有效区171 {172 for (int j = 0; j < singlepic.Width; j++)173 {174 int pixelValue = singlepic.GetPixel(j, i).R;175 if (pixelValue < dgGrayValue) //根据灰度值176 {177 if (posx1 > j) posx1 = j;178 if (posy1 > i) posy1 = i;179 180 if (posx2 < j) posx2 = j;181 if (posy2 < i) posy2 = i;182 };183 };184 };185 //复制新图186 Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);187 return singlepic.Clone(cloneRect, singlepic.PixelFormat);188 }189 190 /// 191 /// 平均分割图片192 /// 193 /// 水平上分割数194 /// 垂直上分割数195 ///
分割好的图片数组
196 public Bitmap [] GetSplitPics(int RowNum,int ColNum)197 {198 if (RowNum == 0 || ColNum == 0)199 return null;200 int singW = bmpobj.Width / RowNum;201 int singH = bmpobj.Height / ColNum;202 Bitmap [] PicArray=new Bitmap[RowNum*ColNum];203 204 Rectangle cloneRect;205 for (int i = 0; i < ColNum; i++) //找有效区206 {207 for (int j = 0; j < RowNum; j++)208 {209 cloneRect = new Rectangle(j*singW, i*singH, singW , singH);210 PicArray[i*RowNum+j]=bmpobj.Clone(cloneRect, bmpobj.PixelFormat);//复制小块图211 }212 }213 return PicArray;214 }215 216 /// 217 /// 返回灰度图片的点阵描述字串,1表示灰点,0表示背景218 /// 219 /// 灰度图220 /// 背前景灰色界限221 ///
222 public string GetSingleBmpCode(Bitmap singlepic, int dgGrayValue)223 {224 Color piexl;225 string code = "";226 for (int posy = 0; posy < singlepic.Height; posy++)227 for (int posx = 0; posx < singlepic.Width; posx++)228 {229 piexl = singlepic.GetPixel(posx, posy);230 if (piexl.R < dgGrayValue) // Color.Black )231 code = code + "1";232 else233 code = code + "0";234 }235 return code;236 }237 238 /// 239 /// 得到灰度图像前景背景的临界值 最大类间方差法240 /// 241 ///
前景背景的临界值
242 public int GetDgGrayValue()243 {244 int[] pixelNum = new int[256]; //图象直方图,共256个点245 int n, n1, n2;246 int total; //total为总和,累计值247 double m1, m2, sum, csum, fmax, sb; //sb为类间方差,fmax存储最大方差值248 int k, t, q;249 int threshValue = 1; // 阈值250 //生成直方图251 for (int i = 0; i < bmpobj.Width; i++)252 {253 for (int j = 0; j < bmpobj.Height; j++)254 {255 //返回各个点的颜色,以RGB表示256 pixelNum[bmpobj.GetPixel(i, j).R]++; //相应的直方图加1257 }258 }259 //直方图平滑化260 for (k = 0; k <= 255; k++)261 {262 total = 0;263 for (t = -2; t <= 2; t++) //与附近2个灰度做平滑化,t值应取较小的值264 {265 q = k + t;266 if (q < 0) //越界处理267 q = 0;268 if (q > 255)269 q = 255;270 total = total + pixelNum[q]; //total为总和,累计值271 }272 pixelNum[k] = (int)((float)total / 5.0 + 0.5); //平滑化,左边2个+中间1个+右边2个灰度,共5个,所以总和除以5,后面加0.5是用修正值273 }274 //求阈值275 sum = csum = 0.0;276 n = 0;277 //计算总的图象的点数和质量矩,为后面的计算做准备278 for (k = 0; k <= 255; k++)279 {280 sum += (double)k * (double)pixelNum[k]; //x*f(x)质量矩,也就是每个灰度的值乘以其点数(归一化后为概率),sum为其总和281 n += pixelNum[k]; //n为图象总的点数,归一化后就是累积概率282 }283 284 fmax = -1.0; //类间方差sb不可能为负,所以fmax初始值为-1不影响计算的进行285 n1 = 0;286 for (k = 0; k < 256; k++) //对每个灰度(从0到255)计算一次分割后的类间方差sb287 {288 n1 += pixelNum[k]; //n1为在当前阈值遍前景图象的点数289 if (n1 == 0) { continue; } //没有分出前景后景290 n2 = n - n1; //n2为背景图象的点数291 if (n2 == 0) { break; } //n2为0表示全部都是后景图象,与n1=0情况类似,之后的遍历不可能使前景点数增加,所以此时可以退出循环292 csum += (double)k * pixelNum[k]; //前景的“灰度的值*其点数”的总和293 m1 = csum / n1; //m1为前景的平均灰度294 m2 = (sum - csum) / n2; //m2为背景的平均灰度295 sb = (double)n1 * (double)n2 * (m1 - m2) * (m1 - m2); //sb为类间方差296 if (sb > fmax) //如果算出的类间方差大于前一次算出的类间方差297 {298 fmax = sb; //fmax始终为最大类间方差(otsu)299 threshValue = k; //取最大类间方差时对应的灰度的k就是最佳阈值300 }301 }302 return threshValue;303 }304 305 /// 306 /// 去掉杂点(适合杂点/杂线粗为1)307 /// 308 /// 背前景灰色界限309 ///
310 public void ClearNoise(int dgGrayValue, int MaxNearPoints)311 {312 Color piexl;313 int nearDots = 0;314 //逐点判断315 for (int i = 0; i < bmpobj.Width; i++)316 for (int j = 0; j < bmpobj.Height; j++)317 {318 piexl = bmpobj.GetPixel(i, j);319 if (piexl.R < dgGrayValue)320 {321 nearDots = 0;322 //判断周围8个点是否全为空323 if (i == 0 || i == bmpobj.Width - 1 || j == 0 || j == bmpobj.Height - 1) //边框全去掉324 {325 bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255));326 }327 else328 {329 if (bmpobj.GetPixel(i - 1, j - 1).R < dgGrayValue) nearDots++;330 if (bmpobj.GetPixel(i, j - 1).R < dgGrayValue) nearDots++;331 if (bmpobj.GetPixel(i + 1, j - 1).R < dgGrayValue) nearDots++;332 if (bmpobj.GetPixel(i - 1, j).R < dgGrayValue) nearDots++;333 if (bmpobj.GetPixel(i + 1, j).R < dgGrayValue) nearDots++;334 if (bmpobj.GetPixel(i - 1, j + 1).R < dgGrayValue) nearDots++;335 if (bmpobj.GetPixel(i, j + 1).R < dgGrayValue) nearDots++;336 if (bmpobj.GetPixel(i + 1, j + 1).R < dgGrayValue) nearDots++;337 }338 339 if (nearDots < MaxNearPoints)340 bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255)); //去掉单点 && 粗细小3邻边点341 }342 else //背景343 bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255));344 }345 }346 347 /// 348 /// 3×3中值滤波除杂349 /// 350 /// 351 public void ClearNoise(int dgGrayValue)352 {353 int x, y;354 byte[] p = new byte[9]; //最小处理窗口3*3355 byte s;356 //byte[] lpTemp=new BYTE[nByteWidth*nHeight];357 int i, j;358 //--!!!!!!!!!!!!!!下面开始窗口为3×3中值滤波!!!!!!!!!!!!!!!!359 for (y = 1; y < bmpobj.Height - 1; y++) //--第一行和最后一行无法取窗口360 {361 for (x = 1; x < bmpobj.Width - 1; x++)362 {363 //取9个点的值364 p[0] = bmpobj.GetPixel(x - 1, y - 1).R;365 p[1] = bmpobj.GetPixel(x, y - 1).R;366 p[2] = bmpobj.GetPixel(x + 1, y - 1).R;367 p[3] = bmpobj.GetPixel(x - 1, y).R;368 p[4] = bmpobj.GetPixel(x, y).R;369 p[5] = bmpobj.GetPixel(x + 1, y).R;370 p[6] = bmpobj.GetPixel(x - 1, y + 1).R;371 p[7] = bmpobj.GetPixel(x, y + 1).R;372 p[8] = bmpobj.GetPixel(x + 1, y + 1).R;373 //计算中值374 for (j = 0; j < 5; j++)375 {376 for (i = j + 1; i < 9; i++)377 {378 if (p[j] > p[i])379 {380 s = p[j];381 p[j] = p[i];382 p[i] = s;383 }384 }385 }386 // if (bmpobj.GetPixel(x, y).R < dgGrayValue)387 bmpobj.SetPixel(x, y, Color.FromArgb(p[4], p[4], p[4])); //给有效值付中值388 }389 }390 }391 392 /// 393 /// 该函数用于对图像进行腐蚀运算。结构元素为水平方向或垂直方向的三个点,394 /// 中间点位于原点;或者由用户自己定义3×3的结构元素。395 /// 396 /// 前后景临界值397 /// 腐蚀方式:0表示水平方向,1垂直方向,2自定义结构元素。398 /// 自定义的3×3结构元素399 public void ErosionPic(int dgGrayValue, int nMode, bool[,] structure)400 {401 int lWidth = bmpobj.Width;402 int lHeight = bmpobj.Height;403 Bitmap newBmp = new Bitmap(lWidth, lHeight);404 405 int i, j, n, m; //循环变量406 407 if (nMode == 0)408 {409 //使用水平方向的结构元素进行腐蚀410 // 由于使用1×3的结构元素,为防止越界,所以不处理最左边和最右边411 // 的两列像素412 for (j = 0; j < lHeight; j++)413 {414 for (i = 1; i < lWidth - 1; i++)415 {416 //目标图像中的当前点先赋成黑色417 newBmp.SetPixel(i, j, Color.Black);418 419 //如果源图像中当前点自身或者左右有一个点不是黑色,420 //则将目标图像中的当前点赋成白色421 if (bmpobj.GetPixel(i - 1, j).R > dgGrayValue ||422 bmpobj.GetPixel(i, j).R > dgGrayValue ||423 bmpobj.GetPixel(i + 1, j).R > dgGrayValue)424 newBmp.SetPixel(i, j, Color.White);425 }426 }427 }428 else if (nMode == 1)429 {430 //使用垂真方向的结构元素进行腐蚀431 // 由于使用3×1的结构元素,为防止越界,所以不处理最上边和最下边432 // 的两行像素433 for (j = 1; j < lHeight - 1; j++)434 {435 for (i = 0; i < lWidth; i++)436 {437 //目标图像中的当前点先赋成黑色438 newBmp.SetPixel(i, j, Color.Black);439 440 //如果源图像中当前点自身或者左右有一个点不是黑色,441 //则将目标图像中的当前点赋成白色442 if (bmpobj.GetPixel(i, j - 1).R > dgGrayValue ||443 bmpobj.GetPixel(i, j).R > dgGrayValue ||444 bmpobj.GetPixel(i, j + 1).R > dgGrayValue)445 newBmp.SetPixel(i, j, Color.White);446 }447 }448 }449 else450 {451 if (structure.Length != 9) //检查自定义结构452 return;453 //使用自定义的结构元素进行腐蚀454 // 由于使用3×3的结构元素,为防止越界,所以不处理最左边和最右边455 // 的两列像素和最上边和最下边的两列像素456 for (j = 1; j < lHeight - 1; j++)457 {458 for (i = 1; i < lWidth - 1; i++)459 {460 //目标图像中的当前点先赋成黑色461 newBmp.SetPixel(i, j, Color.Black);462 //如果原图像中对应结构元素中为黑色的那些点中有一个不是黑色,463 //则将目标图像中的当前点赋成白色464 for (m = 0; m < 3; m++)465 {466 for (n = 0; n < 3; n++)467 {468 if (!structure[m, n])469 continue;470 if (bmpobj.GetPixel(i + m - 1, j + n - 1).R > dgGrayValue)471 {472 newBmp.SetPixel(i, j, Color.White);473 break;474 }475 }476 }477 }478 }479 }480 bmpobj = newBmp;481 }482 483 /// 484 /// 该函数用于对图像进行细化运算。要求目标图像为灰度图像485 /// 486 /// 487 public void ThiningPic(int dgGrayValue)488 {489 int lWidth = bmpobj.Width;490 int lHeight = bmpobj.Height;491 // Bitmap newBmp = new Bitmap(lWidth, lHeight);492 493 bool bModified; //脏标记 494 int i, j, n, m; //循环变量495 496 //四个条件497 bool bCondition1;498 bool bCondition2;499 bool bCondition3;500 bool bCondition4;501 502 int nCount; //计数器 503 int[,] neighbour = new int[5, 5]; //5×5相邻区域像素值504 505 506 507 bModified = true;508 while (bModified)509 {510 bModified = false;511 512 //由于使用5×5的结构元素,为防止越界,所以不处理外围的几行和几列像素513 for (j = 2; j < lHeight - 2; j++)514 {515 for (i = 2; i < lWidth - 2; i++)516 {517 bCondition1 = false;518 bCondition2 = false;519 bCondition3 = false;520 bCondition4 = false;521 522 if (bmpobj.GetPixel(i, j).R > dgGrayValue)523 {524 if (bmpobj.GetPixel(i, j).R < 255)525 bmpobj.SetPixel(i, j, Color.White);526 continue;527 }528 529 //获得当前点相邻的5×5区域内像素值,白色用0代表,黑色用1代表530 for (m = 0; m < 5; m++)531 {532 for (n = 0; n < 5; n++)533 {534 neighbour[m, n] = bmpobj.GetPixel(i + m - 2, j + n - 2).R < dgGrayValue ? 1 : 0;535 }536 }537 538 //逐个判断条件。539 //判断2<=NZ(P1)<=6540 nCount = neighbour[1, 1] + neighbour[1, 2] + neighbour[1, 3]541 + neighbour[2, 1] + neighbour[2, 3] +542 +neighbour[3, 1] + neighbour[3, 2] + neighbour[3, 3];543 if (nCount >= 2 && nCount <= 6)544 {545 bCondition1 = true;546 }547 548 //判断Z0(P1)=1549 nCount = 0;550 if (neighbour[1, 2] == 0 && neighbour[1, 1] == 1)551 nCount++;552 if (neighbour[1, 1] == 0 && neighbour[2, 1] == 1)553 nCount++;554 if (neighbour[2, 1] == 0 && neighbour[3, 1] == 1)555 nCount++;556 if (neighbour[3, 1] == 0 && neighbour[3, 2] == 1)557 nCount++;558 if (neighbour[3, 2] == 0 && neighbour[3, 3] == 1)559 nCount++;560 if (neighbour[3, 3] == 0 && neighbour[2, 3] == 1)561 nCount++;562 if (neighbour[2, 3] == 0 && neighbour[1, 3] == 1)563 nCount++;564 if (neighbour[1, 3] == 0 && neighbour[1, 2] == 1)565 nCount++;566 if (nCount == 1)567 bCondition2 = true;568 569 //判断P2*P4*P8=0 or Z0(p2)!=1570 if (neighbour[1, 2] * neighbour[2, 1] * neighbour[2, 3] == 0)571 {572 bCondition3 = true;573 }574 else575 {576 nCount = 0;577 if (neighbour[0, 2] == 0 && neighbour[0, 1] == 1)578 nCount++;579 if (neighbour[0, 1] == 0 && neighbour[1, 1] == 1)580 nCount++;581 if (neighbour[1, 1] == 0 && neighbour[2, 1] == 1)582 nCount++;583 if (neighbour[2, 1] == 0 && neighbour[2, 2] == 1)584 nCount++;585 if (neighbour[2, 2] == 0 && neighbour[2, 3] == 1)586 nCount++;587 if (neighbour[2, 3] == 0 && neighbour[1, 3] == 1)588 nCount++;589 if (neighbour[1, 3] == 0 && neighbour[0, 3] == 1)590 nCount++;591 if (neighbour[0, 3] == 0 && neighbour[0, 2] == 1)592 nCount++;593 if (nCount != 1)594 bCondition3 = true;595 }596 597 //判断P2*P4*P6=0 or Z0(p4)!=1598 if (neighbour[1, 2] * neighbour[2, 1] * neighbour[3, 2] == 0)599 {600 bCondition4 = true;601 }602 else603 {604 nCount = 0;605 if (neighbour[1, 1] == 0 && neighbour[1, 0] == 1)606 nCount++;607 if (neighbour[1, 0] == 0 && neighbour[2, 0] == 1)608 nCount++;609 if (neighbour[2, 0] == 0 && neighbour[3, 0] == 1)610 nCount++;611 if (neighbour[3, 0] == 0 && neighbour[3, 1] == 1)612 nCount++;613 if (neighbour[3, 1] == 0 && neighbour[3, 2] == 1)614 nCount++;615 if (neighbour[3, 2] == 0 && neighbour[2, 2] == 1)616 nCount++;617 if (neighbour[2, 2] == 0 && neighbour[1, 2] == 1)618 nCount++;619 if (neighbour[1, 2] == 0 && neighbour[1, 1] == 1)620 nCount++;621 if (nCount != 1)622 bCondition4 = true;623 }624 625 if (bCondition1 && bCondition2 && bCondition3 && bCondition4)626 {627 bmpobj.SetPixel(i, j, Color.White);628 bModified = true;629 }630 else631 {632 bmpobj.SetPixel(i, j, Color.Black);633 }634 }635 }636 }637 // 复制细化后的图像638 // bmpobj = newBmp;639 }640 641 /// 642 /// 锐化要启用不安全代码编译643 /// 644 /// 锐化程度。取值[0,1]。值越大锐化程度越高645 ///
锐化后的图像
646 public void Sharpen(float val)647 {648 int w = bmpobj.Width;649 int h = bmpobj.Height;650 Bitmap bmpRtn = new Bitmap(w, h, PixelFormat.Format24bppRgb);651 BitmapData srcData = bmpobj.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);652 BitmapData dstData = bmpRtn.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);653 unsafe654 {655 byte* pIn = (byte*)srcData.Scan0.ToPointer();656 byte* pOut = (byte*)dstData.Scan0.ToPointer();657 int stride = srcData.Stride;658 byte* p;659 660 for (int y = 0; y < h; y++)661 {662 for (int x = 0; x < w; x++)663 {664 //取周围9点的值。位于边缘上的点不做改变。665 if (x == 0 || x == w - 1 || y == 0 || y == h - 1)666 {667 //不做668 pOut[0] = pIn[0];669 pOut[1] = pIn[1];670 pOut[2] = pIn[2];671 }672 else673 {674 int r1, r2, r3, r4, r5, r6, r7, r8, r0;675 int g1, g2, g3, g4, g5, g6, g7, g8, g0;676 int b1, b2, b3, b4, b5, b6, b7, b8, b0;677 678 float vR, vG, vB;679 680 //左上681 p = pIn - stride - 3;682 r1 = p[2];683 g1 = p[1];684 b1 = p[0];685 686 //正上687 p = pIn - stride;688 r2 = p[2];689 g2 = p[1];690 b2 = p[0];691 692 //右上693 p = pIn - stride + 3;694 r3 = p[2];695 g3 = p[1];696 b3 = p[0];697 698 //左侧699 p = pIn - 3;700 r4 = p[2];701 g4 = p[1];702 b4 = p[0];703 704 //右侧705 p = pIn + 3;706 r5 = p[2];707 g5 = p[1];708 b5 = p[0];709 710 //右下711 p = pIn + stride - 3;712 r6 = p[2];713 g6 = p[1];714 b6 = p[0];715 716 //正下717 p = pIn + stride;718 r7 = p[2];719 g7 = p[1];720 b7 = p[0];721 722 //右下723 p = pIn + stride + 3;724 r8 = p[2];725 g8 = p[1];726 b8 = p[0];727 728 //自己729 p = pIn;730 r0 = p[2];731 g0 = p[1];732 b0 = p[0];733 734 vR = (float)r0 - (float)(r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8) / 8;735 vG = (float)g0 - (float)(g1 + g2 + g3 + g4 + g5 + g6 + g7 + g8) / 8;736 vB = (float)b0 - (float)(b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8) / 8;737 738 vR = r0 + vR * val;739 vG = g0 + vG * val;740 vB = b0 + vB * val;741 742 if (vR > 0)743 {744 vR = Math.Min(255, vR);745 }746 else747 {748 vR = Math.Max(0, vR);749 }750 751 if (vG > 0)752 {753 vG = Math.Min(255, vG);754 }755 else756 {757 vG = Math.Max(0, vG);758 }759 760 if (vB > 0)761 {762 vB = Math.Min(255, vB);763 }764 else765 {766 vB = Math.Max(0, vB);767 }768 769 pOut[0] = (byte)vB;770 pOut[1] = (byte)vG;771 pOut[2] = (byte)vR;772 }773 pIn += 3;774 pOut += 3;775 }// end of x776 pIn += srcData.Stride - w * 3;777 pOut += srcData.Stride - w * 3;778 } // end of y779 }780 bmpobj.UnlockBits(srcData);781 bmpRtn.UnlockBits(dstData);782 bmpobj = bmpRtn;783 }784 785 /// 786 /// 图片二值化787 /// 788 /// 789 public void BitmapTo1Bpp(Double hsb)790 {791 int w = bmpobj.Width;792 int h = bmpobj.Height;793 Bitmap bmp = new Bitmap(w, h, PixelFormat.Format1bppIndexed);794 BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);795 for (int y = 0; y < h; y++)796 {797 byte[] scan = new byte[(w + 7) / 8];798 for (int x = 0; x < w; x++)799 {800 Color c = bmpobj.GetPixel(x, y);801 if (c.GetBrightness() >= hsb) scan[x / 8] |= (byte)(0x80 >> (x % 8));802 }803 Marshal.Copy(scan, 0, (IntPtr)((int)data.Scan0 + data.Stride * y), scan.Length);804 }805 bmp.UnlockBits(data);806 bmpobj = bmp;807 }808 }809}

  

posted on
2014-01-13 17:42 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/lonelyxmas/p/3517812.html

你可能感兴趣的文章
C#面试题
查看>>
work of 1/4/2016
查看>>
javascript作用域
查看>>
your local changes would be overwritten by merge. commit stash or revert them to proceed. view them
查看>>
Git 详细命令集
查看>>
布局inline-block问题
查看>>
MVC6 TagHelpers整合到了MVC版本
查看>>
C#学习笔记-抽象工厂模式
查看>>
在哪里可以使用pdf在线翻译工具
查看>>
通过xshell远程连接ubuntu
查看>>
九鼎实际控制人投资观!
查看>>
加速Web前端开发知名的HTML5框架
查看>>
OpenCL和CUDA的使用比较
查看>>
如何使用 WinInet 时提供下载上载进度信息
查看>>
alert(1) to win 4
查看>>
Kindeditor编译器
查看>>
[ZJOI2007]捉迷藏 (点分树+堆*3)
查看>>
leetcode 412. Fizz Buzz
查看>>
对Netflix Ribbon的Loadbalancer类源码设计合理性的一点质疑
查看>>
关于日历的算法
查看>>