瞄准质量中心 - scipy / numpy

时间:2017-10-20 03:53:38

标签: python numpy image-processing scipy linear-algebra

鉴于以下内容:

from scipy.ndimage import center_of_mass
from numpy import array

A = array([
  [ 255, 255, 0, 0 ]
  [ 255, 255, 0, 0 ],
  [ 0   , 0,  0, 0 ],
  [ 0   , 0,  0, 0 ]
])

cm = center_of_mass(A)
# cm = (0.5, 0.5)

centered = ???
cmc = center_of_mass(centered)
# cmc ~= (1.5,1.5)

我们如何移动这个ndarray / image,使其在中心位置,基于它的质心?

我们的目标结果如下:

centered = array([
  [ 0,   0,   0, 0 ],
  [ 0, 255, 255, 0 ],
  [ 0, 255, 255, 0 ],
  [ 0,   0,   0, 0 ]
])

1 个答案:

答案 0 :(得分:1)

很简单:

  1. 找到几何中心

    import numpy as np
    
    c1 = center_of_mass(np.ones_like(A))
    #or : c1 = [A.shape[0]/2.,A.shape[1]/2.]
    
  2. 通过差异移动矩阵

    S = np.roll(A, c1[0]-cm[0] , axis=0)
    S = np.roll(S, c1[0]-cm[0] , axis=1)
    
  3. 答案是:

        Out[18]: 
        array([[  0,   0,   0,   0],
               [  0, 255, 255,   0],
               [  0, 255, 255,   0],
               [  0,   0,   0,   0]])