【问题标题】:Python - Find center of object in an imagePython - 在图像中查找对象的中心
【发布时间】:2016-09-27 21:42:02
【问题描述】:

我有一个带有非白色对象的白色背景的图像文件。 我想使用 python (Pillow) 找到对象的中心。

我在 c++ 中发现了一个类似的问题,但没有可接受的答案 - How can I find center of object?

类似的问题,但答案中的链接断开 - What is the fastest way to find the center of an irregularly shaped polygon? (broken links in answer)

我也阅读了这个页面,但它没有给我一个有用的食谱 - https://en.wikipedia.org/wiki/Smallest-circle_problem

这是一个示例图片:

编辑: 我正在使用的当前解决方案是这样的:

def find_center(image_file):
    img = Image.open(image_file)
    img_mtx = img.load()
    top = bottom = 0
    first_row = True
    # First we find the top and bottom border of the object
    for row in range(img.size[0]):
        for col in range(img.size[1]):
            if img_mtx[row, col][0:3] != (255, 255, 255):
                bottom = row
                if first_row:
                    top = row
                    first_row = False
    middle_row = (top + bottom) / 2  # Calculate the middle row of the object

    left = right = 0
    first_col = True
    # Scan through the middle row and find the left and right border
    for col in range(img.size[1]):
        if img_mtx[middle_row, col][0:3] != (255, 255, 255):
            left = col
            if first_col:
                right = col
                first_col = False
    middle_col = (left + right) / 2  # Calculate the middle col of the object

    return (middle_row, middle_col)

【问题讨论】:

标签: python image pillow


【解决方案1】:

如果您将中心定义为Center of Mass,那么这并不难,尽管 CoM 可以在您的形状之外。您可以将您的图像解释为2D distribution,并且您可以使用集成(求和)找到它的expected value (CoM)。

如果你有 numpy,那就很简单了。首先创建一个包含 1 的 numpy 数组,其中您的图像是非白色的,然后使其成为概率分布,将其除以总数。

from PIL import Image
import numpy as np

im = Image.open('image.bmp')
immat = im.load()
(X, Y) = im.size
m = np.zeros((X, Y))

for x in range(X):
    for y in range(Y):
        m[x, y] = immat[(x, y)] != (255, 255, 255)
m = m / np.sum(np.sum(m))

从这一点开始,它变成了基本的概率论。您找到边际分布,然后像计算离散概率分布一样计算期望值。

# marginal distributions
dx = np.sum(m, 1)
dy = np.sum(m, 0)

# expected values
cx = np.sum(dx * np.arange(X))
cy = np.sum(dy * np.arange(Y))

(cx, cy) 是您要查找的 CoM。

备注:

  • 如果你没有 numpy,你仍然可以这样做。这只是有点乏味,因为您必须通过循环/理解来进行求和。
  • 如果您想根据颜色分配“质量”,可以轻松扩展此方法。您只需将m[x, y] = immat[(x, y)] != (255, 255, 255) 更改为m[x, y] = f(immat[(x, y)]),其中f 是一个任意(非负值)函数。
  • 如果你想避免双重循环,你可以np.asarray(im),但要小心索引

没有循环:

m = np.sum(np.asarray(im), -1) < 255*3
m = m / np.sum(np.sum(m))

dx = np.sum(m, 0) # there is a 0 here instead of the 1
dy = np.sum(m, 1) # as np.asarray switches the axes, because
                  # in matrices the vertical axis is the main
                  # one, while in images the horizontal one is
                  # the first

【讨论】:

  • 非常好的解决方案,代码简洁。我可以建议添加一个链接到“你找到边际分布,然后你计算期望值,就好像它是一个离散的概率分布。”背后的直觉?我认为这是有道理的,但对于不熟悉概率论(我认为这很漂亮)的人来说可能不太平易近人。
【解决方案2】:

我会尝试找到一种方法在它周围绘制一个三角形,三角形的一个点位于对象上最远的“点”,然后找到该三角形的中心。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    相关资源
    最近更新 更多