def binary ( img ) :
bImg = [ ]
for i in range ( img. size [ 0 ] ) :
tmp = [ ]
for j in range ( img. size [ 1 ] ) :
t = img. getpixel ( ( i, j ) )
p = t [ 0 ] * 0.3 + t [ 1 ] * 0.59 + t [ 2 ] * 0.11
if p > 128 :
p = 1
else :
p = 0
tmp. append ( p )
bImg. append ( tmp )
return bImg
def tmpDelete ( img ) : # call skeletonization procedure, lists of lists as input (after binarization)
w = len ( img )
h = len ( img [ 0 ] )
count = 1
while count ! = 0 : # repeat at least one pixel was deleted
count = delete ( img, w, h )
if count:
delete2 ( img, w, h )
def delete ( img, w, h ) : # delete a pixel on the main set, return count of deleted
count = 0
for i in range ( 1 , h- 1 ) :
for j in range ( 1 , w- 1 ) :
if img [ j ] [ i ] == 0 :
if deletable ( img, j, i ) :
img [ j ] [ i ] = 1
count + = 1
return count
def delete2 ( img, w, h ) : # deleting a pixel by noise set
for i in range ( 1 , h- 1 ) :
for j in range ( 1 , w- 1 ) :
if img [ j ] [ i ] == 0 :
if deletable2 ( img, j, i ) :
img [ j ] [ i ] = 1
def fringe ( a ) : # definition of accessories 3 * 3 to noise
t = [ [ 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 , 1 ] ,
[ 1 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 0 ] ,
[ 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 ] ,
[ 0 , 0 , 1 , 1 , 0 , 1 , 1 , 1 , 1 ] ,
[ 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 ] ,
[ 1 , 1 , 1 , 1 , 0 , 1 , 0 , 0 , 1 ] ,
[ 0 , 1 , 1 , 0 , 0 , 1 , 1 , 1 , 1 ] ,
[ 1 , 0 , 0 , 1 , 0 , 1 , 1 , 1 , 1 ] ,
[ 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 ] ,
[ 1 , 1 , 1 , 1 , 0 , 1 , 0 , 0 , 0 ] ,
[ 0 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 ] ,
[ 0 , 0 , 0 , 1 , 0 , 1 , 1 , 1 , 1 ] ,
[ 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 0 ] ]
for i in t:
if a == i:
return true
def check ( a ) : # determination of the belonging of 3 * 3 to the main templates
t123457 = [ 1 , 1 , 0 , 0 , 1 , 0 ]
t013457 = [ 1 , 1 , 1 , 0 , 0 , 0 ]
t134567 = [ 0 , 1 , 0 , 0 , 1 , 1 ]
t134578 = [ 0 , 0 , 0 , 1 , 1 , 1 ]
t0123457 = [ 1 , 1 , 1 , 0 , 0 , 0 , 0 ]
t0134567 = [ 1 , 0 , 1 , 0 , 0 , 1 , 0 ]
t1345678 = [ 0 , 0 , 0 , 0 , 1 , 1 , 1 ]
t1234578 = [ 0 , 1 , 0 , 0 , 1 , 0 , 1 ]
t = [ a [ 1 ] , a [ 2 ] , a [ 3 ] , a [ 4 ] , a [ 5 ] , a [ 7 ] ]
if t == t123457:
return true
t = [ a [ 0 ] , a [ 1 ] , a [ 3 ] , a [ 4 ] , a [ 5 ] , a [ 7 ] ]
if t == t013457:
return true
t = [ a [ 1 ] , a [ 3 ] , a [ 4 ] , a [ 5 ] , a [ 6 ] , a [ 7 ] ]
if t == t134567:
return true
t = [ a [ 1 ] , a [ 3 ] , a [ 4 ] , a [ 5 ] , a [ 7 ] , a [ 8 ] ]
if t == t134578:
return true
t = [ a [ 0 ] , a [ 1 ] , a [ 2 ] , a [ 3 ] , a [ 4 ] , a [ 5 ] , a [ 7 ] ]
if t == t0123457:
return true
t = [ a [ 1 ] , a [ 3 ] , a [ 4 ] , a [ 5 ] , a [ 6 ] , a [ 7 ] , a [ 8 ] ]
if t == t1345678:
return true
t = [ a [ 0 ] , a [ 1 ] , a [ 3 ] , a [ 4 ] , a [ 5 ] , a [ 6 ] , a [ 7 ] ]
if t == t0134567:
return true
t = [ a [ 1 ] , a [ 2 ] , a [ 3 ] , a [ 4 ] , a [ 5 ] , a [ 7 ] , a [ 8 ] ]
if t == t1234578:
return true
def deletable ( img, x, y ) : # receiving 3 * 3, pass for verification for DOS.
a = [ ]
for i in range ( y- 1 , y + 2 ) :
for j in range ( x- 1 , x + 2 ) :
a. append ( img [ j ] [ i ] )
return check ( a )
def deletable2 ( img, x, y ) : # receiving 3 * 3, pass to test for noise
a = [ ]
for i in range ( y- 1 , y + 2 ) :
for j in range ( x- 1 , x + 2 ) :
a. append ( img [ j ] [ i ] )
return fringe ( a )
def checkThisPoint ( img, x, y ) : # count the number of blacks in the neighborhood
c = 0
for i in range ( x- 1 , x + 2 ) :
for j in range ( y- 1 , y + 2 ) :
if img [ i ] [ j ] == 0 :
c + = 1
return c- 1
def findCheckPoint ( img ) : # forming lists of branch points and end points
x = len ( img )
y = len ( img [ 0 ] )
branchPoint = [ ]
endPoint = [ ]
for i in range ( x ) :
for j in range ( y ) :
if img [ i ] [ j ] == 0 :
t = checkThisPoint ( img, i, j )
if t == 1 :
endPoint. append ( ( i, j ) )
if t == 3 :
branchPoint. append ( ( i, j ) )
return ( branchPoint, endPoint )
def __removeDouble ( x, y ) : # returns a list of elements that do not have the same in another list
z = [ ]
for i in x:
c = true
for j in y:
if i == j:
c = False
if c:
z. append ( i )
for i in y:
c = true
for j in x:
if i == j:
c = False
if c:
z. append ( i )
return z
def delNoisePoint ( r ) : # inlet tuple (branch, finite)
tmp = [ ]
tmp2 = [ ]
for i in r [ 1 ] :
x = range ( i [ 0 ] - 5 , i [ 0 ] + 5 )
y = range ( i [ 1 ] - 5 , i [ 1 ] + 5 )
for j in r [ 0 ] :
if j [ 0 ] in x and j [ 1 ] in y:
tmp. append ( i )
tmp2. append ( j )
return ( __removeDouble ( r [ 0 ] , tmp2 ) , __ removeDouble ( r [ 1 ] , tmp ) )
def matchingPoint ( r, v ) : # input: a tuple of reference points and a tuple of the test; output (by coincidence, in total)
all = 0
match = 0
for i in v [ 0 ] :
x = range ( i [ 0 ] - 15 , i [ 0 ] + 15 )
y = range ( i [ 1 ] - 15 , i [ 1 ] + 15 )
all + = 1
for j in r [ 0 ] :
if j [ 0 ] in x and j [ 1 ] in y:
match + = 1
break
for i in v [ 1 ] :
x = range ( i [ 0 ] - 15 , i [ 0 ] + 15 )
y = range ( i [ 1 ] - 15 , i [ 1 ] + 15 )
all + = 1
for j in r [ 1 ] :
if j [ 0 ] in x and j [ 1 ] in y:
match + = 1
break
return ( match, all )
Source: https://habr.com/ru/post/116603/
All Articles