📜  Mahotas – 给定图像的质心

📅  最后修改于: 2022-05-13 01:55:29.213000             🧑  作者: Mango

Mahotas – 给定图像的质心

在本文中,我们将了解如何在 mahotas 中获得图像的质心。质心”(对于二进制图像)是一种有点复杂的说法,即“每个维度的平均值”。换句话说——取所有的 x 坐标并对它们进行平均——你得到了你的“质心”的 x 坐标,对于 y 也是一样的。
在本教程中,我们将使用“lena”图像,下面是加载它的命令。

mahotas.demos.load('lena')

下面是莉娜的图片

注意:输入图像应被过滤或应加载为灰色
为了过滤图像,我们将获取图像对象 numpy.ndarray 并在索引的帮助下对其进行过滤,下面是执行此操作的命令

image = image[:, :, 0]

下面是实现

Python3
# importing required libraries
# importing required libraries
import mahotas
import mahotas.demos
from pylab import gray, imshow, show
import numpy as np
  
# loading image
img = mahotas.demos.load('lena')
 
# grey image
g = img[:, :, 1]
 
# multiplying grey image values
g = g * 100
  
# filtering image
img = img.max(2)
  
# showing image
imshow(img)
show()
 
# getting center of mass
center = mahotas.center_of_mass(img)
  
# printing center of mass co-ordinate
print("Center of Mass : " + str(center))


Python3
# importing required libraries
import mahotas
import numpy as np
from pylab import gray, imshow, show
import os
  
# loading image
img = mahotas.imread('dog_image.png')
 
  
# filtering image
img = img[:, :, 0]
  
# showing image
imshow(img)
show()
 
# getting center of mass
center = mahotas.center_of_mass(img)
  
# printing center of mass co-ordinate
print("Center of Mass : " + str(center))


输出 :

Center of Mass : [246.64854256 259.45157125]

另一个例子

Python3

# importing required libraries
import mahotas
import numpy as np
from pylab import gray, imshow, show
import os
  
# loading image
img = mahotas.imread('dog_image.png')
 
  
# filtering image
img = img[:, :, 0]
  
# showing image
imshow(img)
show()
 
# getting center of mass
center = mahotas.center_of_mass(img)
  
# printing center of mass co-ordinate
print("Center of Mass : " + str(center))

输出 :

Center of Mass : [265.35619268 482.66701402]