现在的位置: 首页 > 综合 > 正文

C#.NET验证码智能识别学习笔记—05C#.Net图片预处理

2019年09月15日 ⁄ 综合 ⁄ 共 2014字 ⁄ 字号 评论关闭

C#.NET验证码智能识别学习笔记---05C#.Net图片预处理
技术qq交流群:JavaDream:251572072
 教程下载,在线交流:it.yunsit.cn

图片经过下面的预处理以后就可以变的清晰很多了,另外在做图像识别的时候一般使用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;
        }
    }
}
--------------------------------------------------------------------------------

抱歉!评论已关闭.