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