Python-查找图像中的对象中心 [英] Python - Find center of object in an image

查看:99
本文介绍了Python-查找图像中的对象中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有白色背景和非白色物体的图像文件. 我想使用python(枕头)找到对象的中心.

I have an image file that's has a white background with a non-white object. I want to find the center of the object using python (Pillow).

我在c ++中发现了类似的问题,但没有可接受的答案-如何我找到对象的中心了吗?

I have found a similar question in c++ but no acceptable answer - How can I find center of object?

类似的问题,但答案中的链接断开-

Similar question, but with broken links in answer - 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

I also read this page but it doesn't give me a useful recipe - https://en.wikipedia.org/wiki/Smallest-circle_problem

以下是示例图片:

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

The current solution I'm using is this:

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)

推荐答案

如果将中心定义为中心质量,这并不困难,尽管CoM可能不在您的形状范围内.您可以将图像解释为 2D分布,然后可以找到其期望值(CoM).

If you define center as Center of Mass, then it is not difficult, although the CoM can be outside of your shape. You can interpret your image as a 2D distribution, and you can find its expected value (CoM) using integration (summation).

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

If you have numpy it is quite simple. First create a numpy array containing 1 where your image is non-white, then to make it a probability distribution divide it by the total number of ones.

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))

从那时起,它变成了基本概率论.找到边际分布,然后计算期望值,就好像它是离散的概率分布一样.

From this point on it turns into basic probability theory. You find the marginal distributions, then you calculate the expected values as if it was a discrete probability distribution.

# 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.

(cx, cy) is the CoM you are looking for.

备注:

  • 如果您没有numpy,您仍然可以这样做.麻烦一点,因为您必须通过循环/理解来求和.
  • 如果要基于颜色分配质量",则可以轻松扩展此方法.您只需将m[x, y] = immat[(x, y)] != (255, 255, 255)更改为m[x, y] = f(immat[(x, y)]),其中f是一种任意(非负值)函数.
  • 如果要避免出现双重循环,可以使用np.asarray(im),但请谨慎使用索引
  • If you do not have numpy, you can still do it. It is just a bit more tedious as you have to do the summations by loops / comprehensions.
  • This method can easily be extended if you want to assign a 'mass' based on color. You just have to change m[x, y] = immat[(x, y)] != (255, 255, 255) to m[x, y] = f(immat[(x, y)]) where f is an arbitary (non-negative valued) function.
  • If you want to avoid the double loop, you can us np.asarray(im), but then be careful with the indices

无循环:

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

这篇关于Python-查找图像中的对象中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆