2016-05-30 6 views
2

イメージファイルに白い背景と非白色のオブジェクトがあります。 python(ピロー)を使ってオブジェクトの中心を探したいと思います。Python - イメージ内のオブジェクトの中心を見つけよう

私はC言語で同様の質問を見られない++が、何の許容答えている - 私も、このページを読んで、それは私に与えるものではありませんWhat is the fastest way to find the center of an irregularly shaped polygon? (broken links in answer)

からHow can I find center of object?

同様の質問が、答えで壊れたリンクを持ちますMoon

編集: 私は、現在のソリューション - 便利なレシピ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) 
+0

可能性のある重複した[見つけるための最速の方法は何ですか不規則な形のポリゴンの中心?](http://stackoverflow.com/questions/1203135/what-is-the-fastest-way-to-find-the-center-of-an-irularly-shaped-polygon) – user20160

+0

@ user20160申し訳ありませんが、上記の答えがリンクを壊してしまったため、使用できないコード – Nir

答えて

0

私がしようとすると、オブジェクト上の最も遠い「ポイント」で三角形の一点で、その周りに三角形を描画する方法を見つけるし、その三角形の中心を見つけるだろう。

6

CenterをMass of Centerとして定義すると、CoMが形状外になることはありますが、難しくありません。画像は2D分布として解釈でき、積分(総和)を使用して期待値(CoM)を見つけることができます。

numpyをお持ちの場合は、非常に簡単です。最初に、画像が非白色である1を含む数値配列を作成し、それを確率分布にして、それを1の総数で割ります。

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です。

result 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 
+0

クリーンなコードで本当に良い解決策です。直感にリンクを追加することを提案することができます。「あなたは限界配分を見つけた後、離散確率分布のように期待値を計算します。私はそれが理にかなっていると思うが、確率論(私は美しいと思う)に精通していないpplには近づけないかもしれない。 – PaulDong

関連する問題