1

みなさん、プログラミングとpython-opencvは一般的に非常に新しいので、iveはすでにこれに対する答えを検索しましたが、見つかりませんでした。

私のウェブカメラを使用してモーショントラッキングを実行しようとしています:

  • 現在のフレームと前のフレームの絶対差を取る
  • これはグレースケールに変換され、しきい値フィルターを通過して、変更されたピクセル(つまり、動きがある場所)のみが白になるようにします。他のすべてのピクセルは黒になります。

しかし、フレームの違いにしきい値を設定して拡張を適用しようとすると、エラーが発生します。

t_minus_dilate = cv2.dilate(t_minus_thresh, es)
TypeError: <unknown> is not a numpy array

これは、使用されるフレームがnumpy配列ではないことを意味しますか?

これが私のコードの一部です:

cv2.namedWindow("window_b", cv2.CV_WINDOW_AUTOSIZE)
# Structuring element
es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,4))

## Webcam Settings
capture = cv2.VideoCapture(0)

def diffImg(t0, t1, t2): #calculates the difference between frames
    d1 = cv2.absdiff(t2, t1)
    d2 = cv2.absdiff(t1, t0)
    return cv2.bitwise_and(d1, d2)

t_minus = cv2.cvtColor(capture.read()[1], cv2.COLOR_RGB2GRAY)
t_minus_thresh = cv2.threshold(t_minus, 0, 255, cv2.THRESH_OTSU)
t_minus_dilate = cv2.dilate(t_minus_thresh, es)

t = cv2.cvtColor(capture.read()[1], cv2.COLOR_RGB2GRAY)
t_thresh = cv2.threshold(t, 0, 255, cv2.THRESH_OTSU)
t_dilate = cv2.dilate(t_minus_thresh, es)

t_plus = cv2.cvtColor(capture.read()[1], cv2.COLOR_RGB2GRAY)
t_plus_thresh = cv2.threshold(t_plus, 0, 255, cv2.THRESH_OTSU)
t_plus_dilate = cv2.dilate(t_plus_thresh, es)


while True:

    diff = diffImg(t_minus_dilate, t_dilate, t_plus_dilate) #difference between the frames
    cv2.imshow('window_b',diff)

    t_minus_dilate = t_dilate
    t = diff
    t_plus_dilate = cv2.dilate(diff, es)

    key = cv2.waitKey(10) #20
    if key == 27: #exit on ESC
        cv2.destroyAllWindows()
        break

これを使用するのに最適な方法はわかりませんが、このコードを使用して、バブルが配置されている場所に動きがある場合(白いピクセルがある場合)、画面上に落下するバブルをポップすることを目的としたゲームを作成します。バブルが飛び出します。

前もって感謝します

4

1 に答える 1

3

これを試して:

retvel, t_minus_thresh = cv2.threshold(t_minus, 0, 255, cv2.THRESH_OTSU)
t_minus_dilate = cv2.dilate(t_minus_thresh, es)

cv2.threshold は2 つの値を返し、2 番目の値は画像です

于 2013-01-25T14:30:01.843 に答える