Python Verification Code Recognition tutorial using projection method and connected domain method to segment pictures

Source: Internet
Author: User
This article mainly introduces the Python Verification Code recognition tutorial using projection method, connected domain method segmentation picture, has a certain reference value, now share to everyone, there is a need for friends can refer to

Objective

Today this article mainly records how to slice the verification code, the main library used is pillow and Linux image processing Tools gimp. First, consider a fixed position and width, no adhesion, no interference, and learn how to use pillow to cut pictures.

With GIMP open the picture, press the plus sign to enlarge the picture, and then click View->show Grid to display the grid lines:


Where each square edge is 10 pixels long, so the number 1 cut coordinates are left 20, 20, right 40, bottom 70. And so you know the cutting position of the remaining 3 digits.

The code is as follows:

From PIL Import IMAGEP = Image.open ("1.png") # Note the position order is left, top, right, bottom cuts = [(20,20,40,70), (60,20,90,70), (100,10,130,60), ( 140,20,170,50)]for i,n in Enumerate (cuts,1): temp = P.crop (n) # call crop function to cut Temp.save ("cut%s.png"% i)

Get 4 pictures after cutting:


So what if the character position is not fixed? Now assume a random position width, no adhesion, no interference line situation.

The first and simplest of these methods is called projection method. The principle is that the two-valued image is projected in the vertical direction, and the segmentation boundary is judged according to the extreme value after projection. Here I still use the verification code image above to demonstrate:

DEF vertical (IMG): "" "" "" "" "" "" "" "" "" "" "" "" Pixdata = Img.load () w,h = Img.size ver_list = [] # Start projection for x in range (W): Black  = 0 for y in range (h):  if pixdata[x,y] = = 0:  black + 1 Ver_list.append (black) # judgment Boundary l,r = 0,0 flag = False cuts = [] for I,count in Enumerate (ver_list): # threshold is here 0 if flag is False and Count > 0:  l = i  flag = True If flag an D count = = 0:  r = i-1  flag = False  cuts.append ((l,r)) return cutsp = Image.open (' 1.png ') b_img = binarizing (p,2 XX) v = vertical (b_img)

With the vertical function we get a position that contains all the black pixels on the x-axis after the left and right edge of the projection. My threshold is set to 0 because there is no interference with the captcha. About the Binarizing function can refer to the previous article

The output is as follows:

[(21, 37), (62, 89), (100, 122), (146, 164)]

As you can see, the projection method gives the left and right borders and we manually view them to get very close. For the upper and lower boundary, lazy can directly use 0 and the height of the picture, but also in the horizontal direction of the projection, here are interested in the small partners can try their own.

However, in the case of adhesion between characters, the projection method will appear a split error, such as in the previous article:


When the modification threshold is 5, the left and right edges of the projection method are:

[(5, 27), (33, 53), (59, 108)]

Obviously the last 6 and 9 digits are not cut.

The modification threshold is 7, and the result is:

[(5, 27), (33, 53), (60, 79), (83, 108)]

Therefore, for simple adhesion, the adjustment threshold can also be resolved.

The second method, called the CFS connected domain segmentation method. The principle is to assume that each character is composed of a separate connected field, in other words, without adhesion, to find a black pixel and begin to judge, until all the connected black pixels are iterated over the marker to determine where the character is split. The algorithm is as follows:

    • The two-valued picture is traversed from left to right, top to bottom, if black pixels are encountered and the pixel has not been accessed, the pixel is placed on the stack and marked as accessed.

    • If the stack is not empty, continue probing around 8 pixels and perform a 2nd step; if the stack is empty, a block of characters is detected.

    • The probe ends so that several characters are identified.

The code is as follows:

Import queuedef CFS (IMG): "" "The incoming binary image is connected to the domain segmentation" "pixdata = Img.load () W,h = Img.size visited = set () q = queue. Queue () offset = [( -1,-1), (0,-1), (1,-1), ( -1,0), (1,0), ( -1,1), (0,1), (max)] cuts = [] for x in range (W): For  y in range (h ):   X_axis = []   #y_axis = []   if pixdata[x,y] = = 0 and (x, y) not in visited:    Q.put ((x, y))    Visited.add ( , y)) and   Q.empty ():    x_p,y_p = Q.get () for    x_offset,y_offset in offset:     X_c,y_c = x_p+x_offset,y_p +y_offset     if (X_c,y_c) in visited:      continue     Visited.add ((x_c,y_c))     try:      if Pixdata[x_c,y_ c] = = 0:       q.put ((x_c,y_c))       x_axis.append (x_c)       #y_axis. Append (Y_c)     except:      pass   if x_ Axis:    min_x,max_x = min (x_axis), Max (X_axis)    if max_x-min_x > 3:     # Width less than 3 is considered noise, modify     as needed Cuts.append ((min_x,max_x)) return cuts

After the call, the output is the same as using the projection method. In addition, I think there is a method called "Flood Fill (Flood fill)", which seems to be the same as the connected domain.

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.