def insert_into_center(resized_digits):
results = []
for img in resized_digits:
i = np.zeros((28, 28))
# calculate center of mass of the pixels
M = cv2.moments(img)
try:
xc = M['m10'] / M['m00']
yc = M['m01'] / M['m00']
except ZeroDivisionError:
xc = 10
yc = 10
# translating the image so as to position
# this point at the center of the 28x28 field.
start_a = max(min(4 + (10 - int(yc)), 8), 0)
start_b = max(min(4 + (10 - int(xc)), 8), 0)
i[start_a:start_a+20, start_b:start_b+20] = img
results.append(i)
return results
评论列表
文章目录