C # verification code recognition, image binarization, segmentation, classification, and Recognition

Source: Internet
Author: User

C # verification code recognition consists of three steps: preprocessing, segmentation, and Recognition

First, I download the verification code from the website.

The processing result is as follows:



1. Image preprocessing, that is, binarization Image

* Sets the gray value of the pixel on the image to 0 or 255.

The principle is as follows:



The Code is as follows:

# Region binarization image ////// Binarization image // sets the gray value of the pixel on the image to 0 or 255 //////
 
  
Verification Code after processing
 Public Bitmap BinaryZaTion () {for (int x = 0; x  t) {img. setPixel (x, y, Color. fromArgb (_ c. a, B); // the number of black points plus _ blackNum ++;} // white else {img. setPixel (x, y, Color. fromArgb (_ c. a, w) ;}} return img ;}# endregion

After binarization, You need to determine the black/white ratio of the image. If the result is more black than white, you need to reverse color the image.

The Code is as follows:

# Does region need reversed colors ////// Whether reversed colors are required //////
 
  
Reversed?
 Public bool IsNeedInverseColor () {if (_ blackNum * 1.0/(img. width * img. height)> 0.5) {return true;} else {return false ;}# endregion # region reversed color ////// Reversed //////
 
  
Verification Code after processing
 Public Bitmap InverseColor () {for (int x = 0; x 

The processing result is as follows:


2. Image Segmentation <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Release + ztK1xNf2t6jKx8/Iw7/release + release/Su7j2yv3X1rXEx/jT8qOsyLu6872rx/jT8ruts/release + release "brush: java; "> # region image segmentation ///

/// Split the image /// /// Verification Code after processing Public Bitmap CutImg () {// split CutY () by Y axis; // number of regions _ count = 0; if (XList. count> 1) {// x start value int _ start = XList [0]; // x end value int _ end = XList [XList. count-1]; // x index int _ idx = 0; while (_ start! = _ End) {// area width int _ w = _ start; // The number of areas plus _ count ++; while (XList. contains (_ w) & _ idx <XList. count) {// The area width plus _ w ++; // The x Index Plus _ idx ++;} // The area x axis coordinate int x = _ start; // area y axis coordinate int y = 0; // area width int width = _ w-_ start; // area height int height = img. height;/** split the current region using the X axis */CutX (img. clone (new Rectangle (x, y, width, height), img. pixelFormat); if (YList. count> 1 & YList. count! = Img. Height) {int y1 = YList [0]; int y2 = YList [YList. Count-1]; if (y1! = 1) {y = y1-1;} height = y2-y1 + 1;} // GDI + Drawing Object Graphics g = Graphics. fromImage (img); g. smoothingMode = SmoothingMode. highQuality; g. compositingMode = CompositingMode. sourceOver; g. pixelOffsetMode = PixelOffsetMode. highSpeed; g. interpolationMode = InterpolationMode. highQualityBicubic; // draw the verification code area g. drawRectangle (new Pen (Brushes. green), new Rectangle (x, y, width, height); g. dispose (); // The start value points to the next group of if (_ idx <XList. count) {_ start = XList [_ idx];} else {_ start = _ end ;}} return img ;} # endregion # region Y-axis character segmentation image ////// Obtain the y-axis split point. /// determine whether each vertical line has a black color. // Add a black color when yes //////Image to be verifiedPrivate void CutY () {XList. clear (); for (int x = 0; x /// Obtain the x-axis split point. /// determine whether each horizontal row has a black color. // If yes, add it //////Temporary RegionPrivate void CutX (Bitmap tempImg) {YList. clear (); for (int x = 0; x <tempImg. height; x ++) {isWhilteLine = false; for (int y = 0; y <tempImg. width; y ++) {_ c = tempImg. getPixel (y, x); if (_ c. R = w) {isWhilteLine = true;} else {isWhilteLine = false; break ;}} if (! IsWhilteLine) {YList. Add (x) ;}} tempImg. Dispose () ;}# endregion


The effect is as follows:


3. Recognition

Recognition is to extract image features.

My approach is to divide the digital area of the image into 4*4 areas one by one, and calculate the percentage of black points in each area, then we compare the calculated results with the previously calculated features and find the smallest Euclidean distance d = sqrt (x1-x2) ^ 2 + (y1-y2) ^ 2) as the result.

Some code is as follows:

# Region black pixel ratio column ////// Calculate the black pixel ratio column /////////
  Private double PixlPercent (Bitmap tempimg) {int temp = 0; int w_h = tempimg. width * tempimg. height; for (int x = 0; x <tempimg. width; x ++) {for (int y = 0; y <tempimg. height; y ++) {_ c = tempimg. getPixel (x, y); if (_ c. R = B) {temp ++ ;}} tempimg. dispose (); double result = temp * 1.0/w_h; result = result. toString (). length> 3? Convert. ToDouble (result. ToString (). Substring (0, 3): result; return result;} # endregion


The effect is as follows:



This code is only used for research and learning.

Newbie on the road, have any suggestions, advice contact pigkeli@qq.com.


Related Article

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.