答案 0 :(得分:3)
它们都是字母(甚至是H)吗?
有几种方法可以解决这个问题。最快(但最天真)的方法是找到最左边和最右边的黑色像素,然后在中间点居中。然后做同样的垂直。基本上为图像创建一个边界框,您可以在其中过滤非#FFFFFF
同样,取决于数据。
答案 1 :(得分:0)
您还可以使用Scikit图像查找图像的质心(或者您自己的功能),然后使用填充翻译图像?在Python中执行此操作的基本方法是:
im = numpy.zeros((20, 20))
im[2:6, 2:14] = 1
# Determine Centre of Mass
com = ndimage.measurements.center_of_mass(im)
print(com)
# Translation distances in x and y axis
x_trans = int(im.shape[0]//2-com[0])
y_trans = int(im.shape[1]//2-com[1])
# Pad and remove pixels from image to perform translation
if x_trans > 0:
im2 = numpy.pad(im, ((x_trans, 0), (0, 0)), mode='constant')
im2 = im2[:im.shape[0]-x_trans, :]
else:
im2 = numpy.pad(im, ((0, -x_trans), (0, 0)), mode='constant')
im2 = im2[-x_trans:, :]
if y_trans > 0:
im3 = numpy.pad(im2, ((0, 0), (y_trans, 0)), mode='constant')
im3 = im3[:, :im.shape[0]-y_trans]
else:
im3 = numpy.pad(im2, ((0, 0), (0, -y_trans)), mode='constant')
im3 = im3[:, -y_trans:]
print(ndimage.measurements.center_of_mass(im3))