在图像中找到质心

Leo*_*Leo 1 python image-processing scikit-image

我的形象是: 在此处输入图片说明

我想在此图像中找到质心。我可以通过绘制两条垂直线来找到质心的大致位置,如下图所示: 在此处输入图片说明

我想使用python中的图像处理工具找到它。我对python(scikit-image)的图像处理库有一点经验,但是我不确定该库是否可以帮助找到图像的重心。我想知道是否有人可以帮助我做到这一点。如果可以使用python中的任何其他库找到图像中的重心,我将很高兴。在此先感谢您的帮助!

Jua*_*uan 6

skimage.measure.regionprops会做你想要的。这是一个例子:

import imageio as iio
from skimage import filters
from skimage.color import rgb2gray  # only needed for incorrectly saved images
from skimage.measure import regionprops

image = rgb2gray(iio.imread('eyeball.png'))
threshold_value = filters.threshold_otsu(image)
labeled_foreground = (image > threshold_value).astype(int)
properties = regionprops(labeled_foreground, image)
center_of_mass = properties[0].centroid
weighted_center_of_mass = properties[0].weighted_centroid

print(center_of_mass)
Run Code Online (Sandbox Code Playgroud)

在我的机器上以及您的示例图片中,我得到了(228.48663375508113, 200.85290046969845)

我们可以做出一张漂亮的图片:

import matplotlib.pyplot as plt
from skimage.color import label2rgb

colorized = label2rgb(labeled_foreground, image, colors=['black', 'red'], alpha=0.1)
fig, ax = plt.subplots()
ax.imshow(colorized)
# Note the inverted coordinates because plt uses (x, y) while NumPy uses (row, column)
ax.scatter(center_of_mass[1], center_of_mass[0], s=160, c='C0', marker='+')
plt.show()
Run Code Online (Sandbox Code Playgroud)

这给了我这个输出:

眼重心

您会注意到,那里可能有些不需要的前景,例如图片的右下角。这是一个整体诺特尔答案,但你可以看看scipy.ndimage.labelskimage.morphology.remove_small_objects和更普遍的skimage.segmentation