驗證碼識別技術研究(1)

來源:互聯網
上載者:User
 1             string imgpName = @"005"; 2             Image image = Image.FromFile(@"G:\" + imgpName + @".jpg"); 3             Image img = (Image)image.Clone(); 4             Bitmap bmp = new Bitmap((Image)image.Clone()); 5             int gray = 0; 6             Graphics g = Graphics.FromImage(image); 7             int sum = 0; 8             int[] zf = new int[256];//灰階數組 9 10             #region 灰階平均值11             for (int x = 0; x < bmp.Width; x++)12             {13                 for (int y = 0; y < bmp.Height; y++)14                 {15                     //灰階演算法16                     gray = (bmp.GetPixel(x, y).R * 299 + bmp.GetPixel(x, y).G * 587 + bmp.GetPixel(x, y).B * 114 + 500) / 1000;17                     zf[gray]++;18                     sum += gray;19                 }20             }21             int avg = sum / (bmp.Width * bmp.Height);22             #endregion23 24             #region 以獲得的灰階平均值為閥值,對映像進行二值化處理25             for (int x = 0; x < bmp.Width; x++)26             {27                 for (int y = 0; y < bmp.Height; y++)28                 {29                     gray = (bmp.GetPixel(x, y).R * 299 + bmp.GetPixel(x, y).G * 587 + bmp.GetPixel(x, y).B * 114 + 500) / 1000;30                     zf[gray]++;31                     sum += gray;32                     Color color = new Color();33                     if (gray > avg)34                     {35                         color = Color.FromArgb(255, 255, 255);36                     }37                     else38                     {39                         color = Color.FromArgb(0, 0, 0);40                     }41                     g.DrawLine(new Pen(color, 1), x, y, x + 1, y + 1);42                 }43             }44             #endregion45 46             #region 長條圖繪製47             Graphics gg = Graphics.FromImage(img);48             //string k = ((int)(bmp.Height * 0.5) / zf.Max()).ToString();49             for (int i = 0; i < zf.Length; i++)50             {51                 Pen p = new Pen(Color.Red, 1);52                 gg.DrawLine(p, i, 0, i, zf[i]);53             }54             #endregion55 56             img.Save(@"G:\" + imgpName + @"長條圖.jpg");57             gg.Dispose();58             image.Save(@"G:\" + imgpName + @"二值化圖.jpg");59             image.Dispose();60             g.Dispose();61             MessageBox.Show("OK!\nGray_AVG:" + avg);//灰階平均值

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.