我想做的是拍摄一张250 x 250 px的图像,然后将所有文本变成1s,其余的变成0s,然后通过神经网络传递。要将图像变为1和0,我编写了此函数:
def imageMatrix(image):
#Read the image
img = cv2.imread(image, 0)
#Get the size of the image
width, height = img.shape[:2]
#This will be fed pixel by pixel to the input of the neural network
matrix = [0]
for x in range(width):
for y in range(height):
color = img[x,y]
if ( color != 255):
matrix.append(1)
else:
matrix.append(0)
return matrix
它返回类似[1、1、1、1、1、0、0、1、1、1、1、0、0、0、0、0、0、0、0、0, 0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0, 1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1, 1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1, 1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0
这确实缩短了,但是我很好奇为什么它不显示结尾的']'。但是,我的主要问题是我试图将几个图像放入训练输入数组中,但这样做很难。我收到“发生异常:ValueError所有输入数组必须具有相同数量的维数”错误。
training_inputs = np.array([imageMatrix("Blank.png")])
print(training_inputs)
training_outputs = np.array([imageMatrix("Blank.png")])
#train inputs as matrix and output as User1
#this sets up three different images
for i in range(3):
image = "Test" + str(i+1) + ".png"
np.append(training_inputs, imageMatrix(image), axis=i)
image = "Train" + str(i+1) + ".png"
np.append(training_outputs, imageMatrix(image), axis=i)
print(training_inputs)
我如何使它们具有相同的尺寸? 注意,我只做三遍,因为我正在尝试进行小规模的测试。附带说明一下,我希望它们成为什么尺寸矩阵?