一、在atiri游戏中
IMAGE_SIZE = (72, 128)
def preprocess(frame):
"""Do preprocessing: resize and binarize.
Downsampling to 128x72 size and convert to grayscale
frame -- input frame, rgb image with 512x288 size
"""
im = Image.fromarray(frame).resize(IMAGE_SIZE).convert(mode='L')
out = np.asarray(im).astype(np.float32)
out[out <= 1.] = 0.0
out[out > 1.] = 1.0
return out
由函数可知,游戏中返回的是数组,因此首先变为图像,然后再下采样,这里的72x128是因为PIL.Image,然后变为灰度。
第二步将灰度图变为数组,并指定格式为float。
这里选择以1为分界相当于对图像进行了二值化
tips:
[python]out[out<=1.] = 0.0可以直接对数组中每一个元素进行判断并赋值
二、在pong游戏中
只通过数组处理
def prepro(I):
""" prepro 210x160x3 into 6400 """
I = I[35:195]
I = I[::2, ::2, 0]
I[I == 144] = 0
I[I == 109] = 0
I[I != 0 ] = 1
return I.astype(np.float).ravel()
运行程序发现输入的大小为210x160x3
tips:
[python]
1、对比第一行处理完的图片格式为160x160x3,可以发现,I[35:195]是取图片第一维度的35-195之间。
2、::2表示步进,在这里意思就是下采样。
python inputs[:,::2,::2,:]
3、0表示取第0层,即红色层
I[I == 144] = 0
I[I == 109] = 0
为什么这么处理???
最后得到80x80
然后经过 return I.astype(np.float).ravel()变为一维
6400