前提・実現したいこと
動画ファイルを読み取り、マウスイベントで領域を選択するというプログラムを作っています。
作成中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
Traceback (most recent call last): File "tennis_line.py", line 66, in <module> cv2.setMouseCallback(wname, onMouse, [wname, self.frame, ptlist]) NameError: name 'self' is not defined
プログラム
python
1import numpy as np 2import cv2 3 4 5VIDEO_DATE = 'tennis.AVI' 6 7 8class PointList(): 9 def __init__(self, npoints): 10 self.video = cv2.VideoCapture(VIDEO_DATE) 11 self.frame = None 12 self.npoints = npoints 13 self.ptlist = np.empty((npoints, 2), dtype=int) 14 self.pos = 0 15 16 def add(self, x, y): 17 if self.pos < self.npoints: 18 self.ptlist[self.pos, :] = [x, y] 19 self.pos += 1 20 return True 21 return False 22 23 def run(self): 24 while(self.video.isOpened()): 25 26 end_flag,selt.frame = self.video.read() 27 28def onMouse(self,event, x, y, flag, params): 29 wname, self.frame, ptlist = params 30 if event == cv2.EVENT_MOUSEMOVE: # マウスが移動したときにx線とy線を更新する 31 self.frame2 = np.copy(self.frame) 32 h, w = self.frame2.shape[0], self.frame2.shape[1] 33 cv2.line(self.frame2, (x, 0), (x, h - 1), (255, 0, 0)) 34 cv2.line(self.frame2, (0, y), (w - 1, y), (255, 0, 0)) 35 cv2.imshow(wname, self.frame2) 36 37 if event == cv2.EVENT_LBUTTONDOWN: # レフトボタンをクリックしたとき、ptlist配列にx,y座標を格納する 38 if ptlist.add(x, y): 39 print('[%d] ( %d, %d )' % (ptlist.pos - 1, x, y)) 40 cv2.circle(self.frame, (x, y), 3, (0, 0, 255), 3) 41 cv2.imshow(wname, self.frame) 42 else: 43 print('All points have selected. Press ESC-key.') 44 if(ptlist.pos == ptlist.npoints): 45 print(ptlist.ptlist) 46 cv2.line(self.frame, (ptlist.ptlist[0][0], ptlist.ptlist[0][1]), 47 (ptlist.ptlist[1][0], ptlist.ptlist[1][1]), (0, 255, 0), 3) 48 cv2.line(self.frame, (ptlist.ptlist[1][0], ptlist.ptlist[1][1]), 49 (ptlist.ptlist[2][0], ptlist.ptlist[2][1]), (0, 255, 0), 3) 50 cv2.line(self.frame, (ptlist.ptlist[2][0], ptlist.ptlist[2][1]), 51 (ptlist.ptlist[3][0], ptlist.ptlist[3][1]), (0, 255, 0), 3) 52 cv2.line(self.frame, (ptlist.ptlist[3][0], ptlist.ptlist[3][1]), 53 (ptlist.ptlist[0][0], ptlist.ptlist[0][1]), (0, 255, 0), 3) 54 55 56if __name__ == '__main__': 57 wname = "MouseEvent" 58 cv2.namedWindow(wname) 59 npoints = 4 60 ptlist = PointList(npoints) 61 cv2.setMouseCallback(wname, onMouse, [wname, self.frame, ptlist]) 62 cv2.imshow(wname, self.frame) 63 cv2.waitKey() 64 cv2.destroyAllWindows()
参考サイト
https://qiita.com/otakoma/items/04e525ac74b7191dffe6
https://qiita.com/hitomatagi/items/3d8973f855e963c9d999
補足情報(FW/ツールのバージョンなど)
Python3.5.3\Opencv3.4.1\Raspberry Pi3 Model B++

回答1件
あなたの回答
tips
プレビュー