C#.NET驗證碼智能識別學習筆記---05C#.Net圖片預先處理

來源:互聯網
上載者:User

標籤:color   io   使用   java   ar   for   檔案   art   cti   

C#.NET驗證碼智能識別學習筆記---05C#.Net圖片預先處理
技術qq交流群:JavaDream:251572072
 教程下載,線上交流:it.yunsit.cn

圖片經過下面的預先處理以後就可以變的清晰很多了,另外在做Image Recognition的時候一般使用tif格式的圖片

下面是圖片預先處理的代碼有詳細說明,如果不明白留言把


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing.Imaging;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace 圖片預先處理
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            //1.獲得檔案夾下所有圖片返回一個數組
           string [] files= Directory.GetFiles(@"G:\NET學習\workspace\驗證碼識別系統用到的資源\樣本圖片","*.gif");
           for (int i = 0; i < files.Length; i++) {
               string file = files[i];
              using ( Bitmap bitmap = (Bitmap)Image.FromFile(file)){
                  using (Bitmap newBitmap = Process(bitmap)) {
                      newBitmap.Save(@"G:\NET學習\workspace\驗證碼識別系統用到的資源\newimage\"+i+".tif",ImageFormat.Tiff);
                  
                  }
              }
              
           }
        }


        private static Bitmap Process(Bitmap bitmap)
        { 
            //1.建立一個新的圖片
            Bitmap newBitmap = new Bitmap(bitmap.Width, bitmap.Height);
           //2.遍曆整個圖片
            for (int x = 0; x < bitmap.Width;x++ )
            {
                for (int y = 0; y < bitmap.Height; y++) { 
                //3.去掉邊框操作
                    if (x == 0 || y == 0 || x == bitmap.Width - 1 || y == bitmap.Height - 1)
                    {
                        newBitmap.SetPixel(x, y, Color.White);
                    }
                    else {
                       
                        Color color = bitmap.GetPixel(x, y);
                        //4.如果點的顏色是背景幹擾色就設定為白色
                        if (color.Equals(Color.FromArgb(204, 204, 51)) ||
                        color.Equals(Color.FromArgb(153, 204, 51)) ||
                        color.Equals(Color.FromArgb(204, 204, 204)) ||
                          color.Equals(Color.FromArgb(204, 255, 51)) ||
                            color.Equals(Color.FromArgb(204, 255, 102)))
                        {
                            newBitmap.SetPixel(x, y, Color.White);
                        }
                        else { 
                        //5.否則就設成原來的顏色
                            newBitmap.SetPixel(x, y, color);
                        }
                    }
                }
            }
            return newBitmap;
        }
    }
}
--------------------------------------------------------------------------------

C#.NET驗證碼智能識別學習筆記---05C#.Net圖片預先處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.