Simple verification code recognition: Use of Bitmap Class

Source: Internet
Author: User

Intelligent Identification of verification codes is a complicated task, and even requires the knowledge of image learning.

Of course, it is not necessary to write the program so deeply. You only need to master several common steps.

 

Verification code Image recognition steps: 1. Image acquisition 2. Border removal 3. Grayscale processing 4. Binarization processing 5. Noise processing 6. Image segmentation 7. Single digit recognition 8. Spliced verification code

 

1. Obtain images

The image is generally remote, so WebRequest is required:

Public Bitmap GetImg (string imgUrl) {WebRequest wreq = WebRequest. create (imgUrl); wreq. timeout = 10000; HttpWebResponse wresp = (HttpWebResponse) wreq. getResponse (); Stream s = wresp. getResponseStream (); return new Bitmap (s );}

 

 

II. Clear borders

Many verification codes are surrounded by a black border. Therefore, you need to perform the following operations:

Public Bitmap ClearBorder (Bitmap bm) {// remove the border width for (int I = 0; I <bm. width; I ++) {bm. setPixel (I, 0, Color. white); bm. setPixel (I, bm. height-1, Color. white);} // trim the border height for (int j = 0; j <bm. height; j ++) {bm. setPixel (0, j, Color. white); bm. setPixel (bm. width-1, j, Color. white);} return bm ;}

 

III. Grayscale processing

The so-called grayscale processing enables colorful images to become gray images with different shades. The reason for this is the subsequent binarization processing. First look at grayscale processing:

Public Bitmap MakeGray (Bitmap bm) {for (int I = 0; I <bm. width; I ++) {for (int j = 0; j <bm. height; j ++) {Color c = bm. getPixel (I, j); // original background color int gray = (int) (c. R * 0.11 + c. G * 0.59 + c. B * 0.3); // calculate the gray bm. setPixel (I, j, Color. fromArgb (gray, gray, gray);} return bm ;}

The principle is to traverse the pixels on the image, obtain the gray level based on the RGB color of the current pixel, and then re-assign the gray level to the current pixel.

Grayscale processing usually has the following three methods (the formula 2.2 used in this example ):

(2.1)

(2.2)

(2.3)

The formula (2.1) takes the average value of the RGB channel, and the obtained image is relatively soft. At the same time, the average brightness difference between the target and the background is reduced, which is not conducive to subsequent threshold processing.

The formula (2.2) considers that the human eye has the strongest adaptability to green, followed by blue, and the red is the worst. When processing the green and blue verification code images, the formula (2.2) is satisfactory, but when processing the red image, because the Red weight in the formula is very small, after grayscale, the brightness difference between the target pixel and the background pixel is greatly reduced, and the effect is not as good as the formula (2.1 ).

The formula (2.3) is based on a premise that the brightness information of the target pixel is limited to be retained, facilitating subsequent threshold value segmentation.

Http://www.cnblogs.com/chaosimple/archive/2013/07/18/3197720.html (thanks to the author for providing valuable information)

 

IV. Binarization processing

Binarization enables gray pixels with different shades to be converted into black and white pixels, that is, binarization.

A critical value needs to be found here. If the value is greater than the value, the value is white (background) and the value is smaller than the value is white (verification code ).

Public Bitmap MakeBlackWhite (Bitmap bm) {for (int I = 0; I <bm. width; I ++) {for (int j = 0; j <bm. height; j ++) {Color c = bm. getPixel (I, j); // background color if (c. b> 37) // determine the current pixel and critical value {bm. setPixel (I, j, Color. white);} else {bm. setPixel (I, j, Color. black) ;}} return bm ;}

 

5. Noise processing

The so-called noise processing means to remove some fragmented points and make them clean and tidy.

Public Bitmap ClearPieces (Bitmap bm) {for (int I = 1; I <bm. width-1; I ++) {for (int j = 1; j <bm. height-1; j ++) {Color c = bm. getPixel (I, j); // original background Color cUp = bm. getPixel (I, j-1); Color cDown = bm. getPixel (I, j + 1); Color cLeft = bm. getPixel (I-1, j); Color cRight = bm. getPixel (I + 1, j); // Response. write (c. R + "" + c. G + "" + c. B + "<br/>"); if (c. R = 0 & cUp. r! = 0 & cDown. R! = 0 & cLeft. R! = 0 & cRight. R! = 0) {bm. SetPixel (I, j, Color. White) ;}} return bm ;}

 

VI. Image cutting

Public Bitmap SplitImg (Bitmap bm, int pointX, int pointY) {Bitmap first = new Bitmap (cutWidth, cutHeight, PixelFormat. format32bppRgb); for (int I = 0; I <first. width; I ++) {for (int j = 0; j <first. height; j ++) {Color c = bm. getPixel (pointX + I, pointY + j); first. setPixel (I, j, c );}}
Return first ;}

In this way, the image containing only one digit is obtained.

 

7. Identify a single number

 

Public string GetOneNumber (Bitmap first) {StringBuilder strFir = new StringBuilder (""); for (int I = 0; I <first. width; I ++) {for (int j = 0; j <first. height; j ++) {Color c = bm. getPixel (I, j); if (c. R = 0) {strFir. append ("0");} else {strFir. append ("1") ;}} int result = 0; string num = ""; List <string> numbers = verifyHelper. getList (); for (int j = 0; j <numbers. count (); j ++) {result = 0; for (int I = 0; I <strFir. length; I ++) {if (strFir [I] = numbers [j] [I]) {result ++;} if (result> 110) {num = j. toString (); return num ;}} return "-1 ";

 

8. Final stitching

Result + = GetOneNumber (bm, 8, 4); result + = GetOneNumber (bm, 20, 2); result + = GetOneNumber (bm, 35, 5 ); result + = GetOneNumber (bm, 45, 2 );

 

The operations to be done are as follows:

Public string GetNumbers (string imgUrl) {Bitmap bm = new Bitmap (GetImgStream (imgUrl); bm = ClearBorder (bm); bm = MakeGray (bm ); bm = MakeBlackWhite (bm); bm = ClearPieces (bm); string result = ""; result + = GetOneNumber (bm, 8, 4); result + = GetOneNumber (bm, 20, 2); result + = GetOneNumber (bm, 35, 5); result + = GetOneNumber (bm, 45, 2); if (result. contains ("-1") {return "-1" ;}else {return result ;}}

 

Simple verification code recognition: Use of Bitmap Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.