Python Verification Code (ii) connected domain segmentation

Source: Internet
Author: User

Before cutting: After cutting:

Code:

#-*-coding:utf-8-*- fromPILImportImageImportQueuedefCFS (IMG):"""incoming binary images for connected 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), (1, 1))] Cuts= []     forXinchRange (W): forYinchRange (h): X_axis= []            #Y_axis = []            ifPixdata[x, y] = = 0 and(x, Y) not inchVisited:q.put ((x, y)) Visited.add ((x, y)) while  notq.empty (): x_p, Y_p=Q.get () forX_offset, Y_offsetinchOffset:x_c, Y_c= X_p + X_offset, y_p +Y_offsetif(X_c, Y_c)inchvisited:ContinueVisited.add ((X_c, Y_c))Try:                        ifPixdata[x_c, y_c] = =0:q.put ((X_c, Y_c)) X_axis.append (X_c) #y_axis.append (Y_c)                    except:                        Pass            ifx_axis:min_x, max_x=min (X_axis), Max (X_axis)ifmax_x-min_x > 3:                    #width less than 3 is considered noise, modify as needed .Cuts.append ((min_x, max_x + 1))    returncutsdefSavesmall (IMG, OutDir, cuts): W, H=img.size Pixdata=img.load () forI, iteminchenumerate (Cuts): Box= (Item[0], 0, item[1], h) img.crop (box). Save (OutDir+ STR (i) +". bmp") img= Image.open ('Cfs/2.png') Savesmall (IMG,'cfs/', CFS (IMG))

The idea is to use depth traversal, the image binary processing, first find a black pixel, and then the pixel around 8 pixels to judge, if not visited, save it, and then the last array of the smallest x and Max X is the x-axis cutting position. This method of partitioning can only be applied to non-adhesion verification code, than the advantage of vertical segmentation, can handle the location of the more strange verification code.


Reprinted: 80557242

Python Verification Code (ii) connected domain segmentation

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.