#質問
私はボールトラッキングをしようとしています。ここのプログラム実行させると私が求めていたものに非常に近いので使いたいのですが、初めの方に書いてあるap = argparse.ArgumentParser()からの6行、pts = deque(maxlen=args["buffer"])の意味がよくわからず、while文の中でもそれが使われているところがあり理解できません。指定した色の範囲以外の色を黒くしてしまい、そこから中心を求め、円を描画しているのだろうと思います。基本的な部分かもしれませんが教えていただきたいです。
あとteratailで質問を書く際にソースコードに行の番号を書きいれることはできますか?(地道に書き入れる以外の方法です。)宜しくお願いします。
#該当のソースコード
python
1from collections import deque 2import numpy as np 3import argparse 4 5import cv2 6 7# construct the argument parse and parse the arguments 8ap = argparse.ArgumentParser() 9ap.add_argument("-v", "--video", 10 help="path to the (optional) video file") 11ap.add_argument("-b", "--buffer", type=int, default=64, 12 help="max buffer size") 13args = vars(ap.parse_args()) 14 15# define the lower and upper boundaries of the "green" 16# ball in the HSV color space, then initialize the 17# list of tracked points 18greenLower = (29, 86, 6) 19greenUpper = (64, 255, 255) 20pts = deque(maxlen=args["buffer"]) 21 22# if a video path was not supplied, grab the reference 23# to the webcam 24if not args.get("video", False): 25 camera = cv2.VideoCapture(0) 26# otherwise, grab a reference to the video file 27 28else: 29 camera = cv2.VideoCapture(args["video"]) 30 31camera.set(3,320) 32camera.set(4,240) 33# keep looping 34while True: 35 # grab the current frame 36 (grabbed, frame) = camera.read() 37 38 # if we are viewing a video and we did not grab a frame, 39 # then we have reached the end of the video 40 if args.get("video") and not grabbed: 41 break 42 43 # resize the frame, blur it, and convert it to the HSV 44 # color space 45 #frame = imutils.resize(frame, width=600) 46 # blurred = cv2.GaussianBlur(frame, (11, 11), 0) 47 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 48 49 # construct a mask for the color "green", then perform 50 # a series of dilations and erosions to remove any small 51 # blobs left in the mask 52 mask = cv2.inRange(hsv, greenLower, greenUpper) 53 mask = cv2.erode(mask, None, iterations=2) 54 mask = cv2.dilate(mask, None, iterations=2) 55 56 # find contours in the mask and initialize the current 57 # (x, y) center of the ball 58 cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, 59 cv2.CHAIN_APPROX_SIMPLE)[-2] 60 center = None 61 62 # only proceed if at least one contour was found 63 if len(cnts) > 0: 64 # find the largest contour in the mask, then use 65 # it to compute the minimum enclosing circle and 66 # centroid 67 c = max(cnts, key=cv2.contourArea) 68 ((x, y), radius) = cv2.minEnclosingCircle(c) 69 M = cv2.moments(c) 70 center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) 71 72 # only proceed if the radius meets a minimum size 73 if radius > 10: 74 # draw the circle and centroid on the frame, 75 # then update the list of tracked points 76 cv2.circle(frame, (int(x), int(y)), int(radius), 77 (0, 255, 255), 2) 78 cv2.circle(frame, center, 5, (0, 0, 255), -1) 79 80 # update the points queue 81 pts.appendleft(center) 82 83 # loop over the set of tracked points 84 for i in xrange(1, len(pts)): 85 # if either of the tracked points are None, ignore 86 # them 87 if pts[i - 1] is None or pts[i] is None: 88 continue 89 90 # otherwise, compute the thickness of the line and 91 # draw the connecting lines 92 thickness = int(np.sqrt(args["buffer"] / float(i + 1)) * 2.5) 93 cv2.line(frame, pts[i - 1], pts[i], (0, 0, 255), thickness) 94 95 # show the frame to our screen 96 cv2.imshow("Frame", frame) 97 key = cv2.waitKey(1) & 0xFF 98 99 # if the 'q' key is pressed, stop the loop 100 if key == ord("q"): 101 break 102 103# cleanup the camera and close any open windows 104camera.release() 105cv2.destroyAllWindows()
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/07/26 05:22