PHP verification code recognition (intermediate)

Source: Internet
Author: User
PHP verification code recognition (intermediate) (transfer)

 

In the previous article
<PHP verification code recognition

(Elementary)

)
>
It explains how to identify simple verification. Here, the simple verification code is composed of numbers and letters, and the format is unified.
Fixed position. This article will continue to study the verification code in depth. The purpose of this recognition is that the verification code consists of characters and numbers. The verification code is rotated (either left or right), its position is not fixed, and there are characters.
And characters, and the verification code has stronger interferon. The method described in this article is not a omnipotent solution, and the Code provided cannot directly solve your problem. Here is just a method. Read the specific requirements
It must be noted that the verification code is irrelevant to the specific programming language.
PHP
Language implementation. The method described here can be implemented in any language.

This article describes the steps involved in the verification code recognition process.



For example, the subsequent explanations are all centered on this figure.
I. When we get a verification code, the first thing we need to do at first glance is binarization. Use part of the Verification Code
1
Indicates that the background part is used
0
The recognition method is very simple. We printed the verification code on the front image.
RGB
And then analyze the rule.
RGB
Code, we can easily tell the image above
R
Value greater
120
,
G
And
B
The value is less
80
According to this rule, we can easily binarization the image above. Let's look at the two images recognized in the first article.


 
It seems complicated. The background color of each verification code image is different, and the color of each verification code number is different. It seems difficult to binarization. In fact, we can print it out.
RGB
It is easy to find the value. Regardless of how the color of the verified number changes
RGB
There is always a value smaller
125
Therefore, the following judgment is adopted:

$ Rgbarray ['red'] <125 |
$ Rgbarray ['green'] <125 | $ rgbarray ['blue'] <125

We can easily tell where the numbers are and where the background is.

 
The factor that we can find out is that when making a verification code for interferon, in order to make it not affect the Display Effect of numbers, we must use interferon
RGB
And numbers
RGB
Independent from each other. As long as we understand this rule, we can easily achieve binarization.

 
We found
120
,
80
,
125
And the actual
RGB
There are discrepancies, so sometimes after binarization, some places will appear
1
This interference does not make much sense for displaying numbers at a fixed position on the verification code. However, for images with uncertain verification code positions, when we cut characters, it is likely to cause interference. Therefore, noise is removed after binarization.

 
Ii. Next we will proceed to the second step to obtain noise. The principle of dryness is very simple, that is, to remove the effective values of isolation. If the noise is high and the required efficiency is high, there is a lot of work to be done here. Fortunately, we do not need to be so advanced here, we can use the simplest method, if a point is
1
It determines the top, bottom, top, top, right, bottom, and right of the point.
8
Whether the number is
1
, If not
1
Is considered to be a dry point, directly set
1
You can.

As shown in, we use this method to easily find
1
Set to dry point directly
1
You can.

We used a technique in judgment. Sometimes the noise may be two consecutive
1
, So we

View plain
Copy to clipboard
Print
?
  1. $ Num
    = 0;
  2. If
    (
    $ Data
    [
    $ I
    ] [
    $ J
    ] = 1)
  3. {
  4. // Upload

  5. If
    (Isset (
    $ Data
    [
    $ I
    -1] [
    $ J
    ]) {
  6. $ Num
    =
    $ Num
    +
    $ Data
    [
    $ I
    -1] [
    $ J
    ];
  7. }
  8. // Lower

  9. If
    (Isset (
    $ Data
    [
    $ I
    + 1] [
    $ J
    ]) {
  10. $ Num
    =
    $ Num
    +
    $ Data
    [
    $ I
    + 1] [
    $ J
    ];
  11. }
  12. // Left

  13. If
    (Isset (
    $ Data
    [
    $ I
    ] [
    $ J
    -1]) {
  14. $ Num
    =
    $ Num
    +
    $ Data
    [
    $ I
    ] [
    $ J
    -1];
  15. }
  16. // Right

  17. If
    (Isset (
    $ Data
    [
    $ I
    ] [
    $ J
    + 1]) {
  18. $ Num
    =
    $ Num
    +
    $ Data
    [
    $ I
    ] [
    $ J
    + 1];
  19. }
  20. // Upper left

  21. If
    (Isset (
    $ Data
    [
    $ I
    -1] [
    $ J
    -1]) {
  22. $ Num
    =
    $ Num
    +
    $ Data
    [
    $ I
    -1] [
    $ J
    -1];
  23. }
  24. // Upper right

  25. If
    (Isset (
    $ Data
    [
    $ I
    -1] [
    $ J
    + 1]) {
  26. $ Num
    =
    $ Num
    +
    $ Data
    [
    $ I
    -1] [
    $ J
    + 1];
  27. }
  28. // Bottom left

  29. If
    (Isset (
    $ Data
    [
    $ I
    + 1] [
    $ J
    -1]) {
  30. $ Num
    =
    $ Num
    +
    $ Data
    [
    $ I
    + 1] [
    $ J
    -1];
  31. }
  32. // Bottom right

  33. If
    (Isset (
    $ Data
    [
    $ I
    + 1] [
    $ J
    + 1]) {
  34. $ Num
    =
    $ Num
    +
    $ Data
    [
    $ I
    + 1] [
    $ J
    + 1];
  35. }
  36. }
  37. If
    (
    $ Num
    = 0 ){
  38. $ Data
    [
    $ I
    ] [
    $ J
    ] = 0;
  39. }

$ Num = 0; <br/> if ($ data [$ I] [$ J] = 1) <br/> {<br/> // on <br/> If (isset ($ data [$ I-1] [$ J]) {<br/> $ num = $ num + $ data [$ I-1] [$ J]; <br/>}< br/> // <br/> If (isset ($ data [$ I + 1] [$ J]) {<br/> $ num = $ num + $ data [$ I + 1] [$ J]; <br/>}< br/> // left <br/> If (isset ($ data [$ I] [$ J-1]) {<br/> $ num = $ num + $ data [$ I] [$ J-1]; <br/>}< br/> // right <br/> If (isset ($ data [$ I] [$ J + 1]) {<br/> $ num = $ num + $ data [$ I] [$ J + 1]; <br/>}< br/> // upper left <br/> If (isset ($ data [$ I-1] [$ J-1]) {<br/> $ num = $ num + $ data [$ I-1] [$ J-1]; <br/>}< br/> // upper right <br/> If (isset ($ data [$ I-1] [$ J + 1]) {<br/> $ num = $ num + $ data [$ I-1] [$ J + 1]; <br/>}< br/> // lower left <br/> If (isset ($ data [$ I + 1] [$ J-1]) {<br/> $ num = $ num + $ data [$ I + 1] [$ J-1]; <br/>}< br/> // bottom right <br/> If (isset ($ data [$ I + 1] [$ J + 1]) {<br/> $ num = $ num + $ data [$ I + 1] [$ J + 1]; <br/>}< br/> if ($ num = 0) {<br/> $ data [$ I] [$ J] = 0; <br/>}


We calculate
8
At last, we can determine whether their sum is smaller than the specified threshold.
3: After Noise Removal, we will get clean binarization data. Next we will cut the characters. There are many ways to cut characters. Here I use the simplest one, first vertically cut into characters, and then remove more characters in the horizontal direction
0000
, Such

Step 1 cut the red line and Step 2 cut the blue line to get independent characters. But in the following case:



According to the above method
DW
The character is cut into one character, which is the wrong cut, so here we involve the cutting of the adhesion character.
4. Adhesive character cutting: when the verification code is created, the adhesion of the Rule characters is easy to split. If the character itself scales, deformation is difficult to handle. After analysis, we can find that, the above character adhesion is very simple
The single method is only the adhesion of Rule characters, so we also use a very simple processing method to deal with this situation. After the split operation is completed, we cannot immediately determine that the split part is a character.
The key factor for verification is whether the width of the cut character is greater than the threshold. The trade-off criterion for this threshold is that no matter how a character is rotated or deformed, it will not exceed this threshold, if we cut the block
If the value is greater than the threshold, it can be considered as a adhesion character. If the value is greater than the sum of the two thresholds, it is considered as a three-character adhesion, and so on. After knowing this rule, it is easy to cut the adhesion characters. If I
We found that it is a sticking character block. We can directly divide this block into two or more new blocks. Of course, in order to better restore characters, I usually use equal share
+ 1
,
-1
Supplement the character block.
5. After the above four steps, we can extract pure character blocks. The next step is to match the characters. There are many ways to create a pattern for rotating characters, so we will not study it in depth here. The simplest method I use here is to create a matching library for all characters, so I added
Study
The purpose is to manually identify the image verification code and then
Study
Method. In this way, the more image data is written, the higher the validation and recognition accuracy.
Well, after the above steps, we can basically identify most of the verification codes on the Internet. Here we use the simplest method, without using any
OCR
Knowledge. These methods should belong to non-
OCR
The peak of the field. To identify more complex verification codes, more
OCR
Knowledge. If you have the opportunity, I will introduce it one by one in the advanced article.
The following are some easy-to-recognize verification codes that may attract the attention of website administrators.


 

Suggestions for creating verification Codes
For verification code recognition programs, the most rare part is the cutting of verification characters and the establishment of signatures. Many programmers in China always like to add a lot of interferon and interference lines to the Verification code when they only do the verification code, not to mention the effect, but not to achieve good results; therefore, to make your own verification code difficult to identify, only the following two points are enough.

1
: Character adhesion. It is recommended that all characters have adhesion parts;

2
: Do not use specification characters. Each part of the verification code is scaled or rotated in different proportions.
As long as these two points are achieved, or the deformation of these two points, it is difficult for the recognition program to recognize them. Let's see,
Yahoo
And
Google
The verification code is known, but it is hard to be identified.

Goole:

Yahoo:


Source File: Click to download

Author
:
Yi Emy (
Ugg
)
Post Please note: http://blog.csdn.net/ugg/archive/2009/03/09/3972368.aspx

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.