opencvで人を認識するプログラムを作っていましす。
そこで認識した人の軌跡を描きたいのですが
現在、前フレームの座標と現フレームの座標しか繋げられず
その軌跡をずっと残したいです。
以下のコードにcv2.lineの部分があります。
そこをfor文から外せばいいと思っているんですが
if文の条件も必要なのでどうしてどうしればいいかわかりません。
プログラミングは初心者でいろいろ練習中です。
while(cap.isOpened()): ret, frame = cap.read() fgmask = fgbg.apply(frame) fgmask = cv2.threshold(fgmask, 244, 255, cv2.THRESH_BINARY)[1] kernel = np.ones((5,5), np.uint8) fgmask = cv2.dilate(fgmask, kernel, iterations = 2) image, contours, hierarchy = cv2.findContours(fgmask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) new_center = np.empty((0,2), float) for c in contours: #cArea = cv2.contourArea(c) cArea = cv2.moments(c) area = int (cArea['m00']) if 50 < area < 300 : continue c = convHull(c) x, y, w, h = cv2.boundingRect(c) x, y, w, h = padding_position(x, y, w, h, 5) cx, cy = centroidPL(c) new_point = np.array([cx, cy], float) new_center = np.append(new_center, np.array([[cx, cy]]), axis=0) if (old_center.size > 1): #近くの座標探し old_point_t = serchNN(new_point, old_center) # old_centerが輪郭の内側にあるか確かめる if (cv2.pointPolygonTest(c, (old_point_t[0], old_point_t[1]), True) > 0): old_point = old_point_t #ここのlineをfor文から外しいたいのですがどうすればfor文から抜け出せて線を引くことができるのでしょうか? #ここのif文の条件を満たしながら線を引くにはどうすればいいかわかりません。 cv2.line(frame, (int(old_point[0]), int(old_point[1])), (cx, cy), (0,0,255), 2) #cv2.putText(frame, 'Area:' + str(area) , (x+10, y+20), font, 0.5, (255,255,255), 1) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3) cv2.circle(frame,(cx,cy),5,(0,0,255),-1) cv2.imshow('framew',fgmask) cv2.imshow('frame',frame) rec(frame) old_center = new_center k = cv2.waitKey(30) & 0xff if k == 27:
質問内容は本文に記載、プログラムの簡単な動作説明、できれば元画像+現在と希望する結果画像を追記すると回答得られやすいと思います。
回答1件
あなたの回答
tips
プレビュー