C #. NET Verification Code Intelligent Recognition learning notes --- 05C #. Net image preprocessing
C #. NET Verification Code Intelligent Recognition learning notes --- 05C #. Net image preprocessing
Technology qq exchange group: JavaDream: 251572072
Download the tutorial and communicate online: it.yunsit.cn
After preprocessing, the image can be much clearer. In addition, image in tif format is generally used for image recognition.
The following is a detailed description of the image preprocessing code.
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 image preprocessing
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void button#click (object sender, EventArgs e)
{
// 1. Get all the images in the folder and return an array
String [] files = Directory. GetFiles (@ "G: \ NET learning \ workspace \ verification code recognition system resources \ sample images", "*. 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 learning \ workspace \ verification code recognition system resources \ newimage \" + I + ". tif", ImageFormat. Tiff );
}
}
}
}
Private static Bitmap Process (Bitmap bitmap)
{
// 1. Create a new image
Bitmap newBitmap = new Bitmap (bitmap. Width, bitmap. Height );
// 2. traverse the entire image
For (int x = 0; x <bitmap. Width; x ++)
{
For (int y = 0; y <bitmap. Height; y ++ ){
// 3. Remove the border
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 the color of the vertex is background interference, set it to white.
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. Otherwise, set it to the original color.
NewBitmap. SetPixel (x, y, color );
}
}
}
}
Return newBitmap;
}
}
}
--------------------------------------------------------------------------------