Python – 简易识别

# -*- TomYuan
import numpy as np
import os
import matplotlib.pyplot as plt
'''
0000000000000000000000000
0000000000000000000000000
0000000001111111000000000
0000000010000000100000000
0000000100000000010000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000000100000000010000000
0000000010000000100000000
0000000001111111000000000
0000000000000000000000000
0000000000000000000000000
'''
#define array (25 * 22) {m * n}
m = 25
n = 22
matrix = [0] * n
fr = open(fileName)
l = 0;
#put all number into array
matrix[l] = [0] * (len(line) - 1)
for i in range(1, len(line)):
# print (line[i - 1])
matrix[l][i - 1] = line[i - 1]
l = l + 1
return matrix

def calMatrix(matrix):
ret = 0;
for i in range(1, 22):
for j in range(1, 25):
if matrix[i][j] == '1':
ret = ret + 1
#
if matrix[i - 1][j] == '1':
ret = ret + 2
else:
ret = ret - 2
#
if matrix[i - 1][j - 1] == '1':
ret = ret + 2
else:
ret = ret - 2
#
if matrix[i][j - 1] == '1':
ret = ret + 2
else:
ret = ret - 2
##
#
if matrix[i + 1][j] == '1':
ret = ret + 2
else:
ret = ret - 2
#
if matrix[i + 1][j + 1] == '1':
ret = ret + 2
else:
ret = ret - 2
#
if matrix[i][j + 1] == '1':
ret = ret + 2
else:
ret = ret - 2
##
#
if matrix[i - 1][j + 1] == '1':
ret = ret + 2
else:
ret = ret - 2
#
if matrix[i + 1][j - 1] == '1':
ret = ret + 2
else:
ret = ret - 2
return ret