3

だから、スタックオーバーフローメンバーの助けを借りて、私は次のコードを持っています:

data = "needle's (which is a png image) base64 code goes here"
decoded = data.decode('base64')
f = cStringIO.StringIO(decoded)
image = Image.open(f)
needle = image.load()

while True:
    screenshot = ImageGrab.grab()
    haystack = screenshot.load()
    if detectImage(haystack, needle):
        break
    else:
        time.sleep(5)

針が干し草の山にあるかどうかを確認するために、次のコードを作成しました。

def detectImage(haystack, needle):
    counter = 0
    for hayrow in haystack:
        for haypix in hayrow:
            for needlerow in needle:
                for needlepix in needlerow:
                    if haypix == needlepix:
                        counter += 1

    if counter == 980: #the needle has 980 pixels
        return True
    else:
        return False

問題は、3 行目で次のエラーが発生することです: 'PixelAccess' object is not iterable

needle と haystack の両方を numpy/scipy 配列にコピーする方が簡単であると提案されました。次に、2D 配列の針が 2D 配列の干し草の山内にあるかどうかを確認する関数を使用できます。

次のことについて助けが必要です:

1) これらの配列を numpy 配列に変換します。

2) 2D アレイの針が 2D アレイの干し草の山内にあるかどうかを確認する関数。私の機能は動作しません。

これらはイメージ です
:
針

干し草の山 干し草の山

4

3 に答える 3

3

画像を numpy 配列に変換するには、次のようにするだけです。

import numpy as np
from PIL import Image

needle = Image.open('needle.png')
haystack = Image.open('haystack.jpg')

needle = np.asarray(needle)
haystack = np.asarray(haystack)

針の検索を開始するには、角が一致するすべての場所のリストが表示されることに注意してください。

haystack = np.array([[1,2,3],[3,2,1],[2,1,3]])
needle = np.array([[2,1],[1,3]])

np.where(haystack == needle[0,0])
#(array([0, 1, 2]),   row-values
# array([1, 1, 0]))   col-values

次に、すべてのコーナーの一致を見て、そこにあるサブヘイスタックが一致するかどうかを確認できます。

h,w = needle.shape
rows, cols = np.where(haystack == needle[0,0])
for row, col in zip(rows, cols):
    if np.all(haystack[row:row+h, col:col+w] == needle):
        print "found it at row = %i, col = %i"%(row,col)
        break
else:
    print "no needle in haystack"

以下は、最良の一致を見つけるより堅牢なバージョンであり、それが特定のパーセンテージよりも一致する場合は、針が見つかったと見なされます。見つからない場合はコーナー座標を返しますNone

def find_needle(needle, haystack, tolerance=.80):
    """ input:  PIL.Image objects
        output: coordinat of found needle, else None """

    # convert to grayscale ("L"uminosity) for simplicity.
    needle = np.asarray(needle.convert('L'))   
    haystack = np.asarray(haystack.convert('L'))

    h,w = needle.shape
    H,W = haystack.shape
    L = haystack.max()

    best = (None, None, 1)
    rows, cols = np.where((haystack - needle[0,0])/L < tolerance)
    for row, col in zip(rows, cols):
        if row+h > H or col+w > W: continue # out of range
        diff = np.mean(haystack[row:row+h, col:col+w] - needle)/L
        if diff < best[-1]:
            best = (diff, row, col)

    return best if best[-1] < tolerance else None
于 2013-03-29T20:48:02.113 に答える
1

matchTemplate位置を検出するために opencv で使用できます。

import cv2
import numpy as np
import pylab as pl

needle = cv2.imread("needle.png")
haystack = cv2.imread("haystack.jpg")

diff = cv2.matchTemplate(haystack, needle, cv2.TM_CCORR_NORMED)
x, y = np.unravel_index(np.argmax(diff), diff.shape)

pl.figure(figsize=(12, 8))
im = pl.imshow(haystack[:,:, ::-1])
ax = pl.gca()
ax.add_artist(pl.Rectangle((y, x), needle.shape[1], needle.shape[0],  transform=ax.transData, alpha=0.6))

出力は次のとおりです。

ここに画像の説明を入力

于 2013-03-29T22:35:06.373 に答える