(self)
| 55 | self.use_ransac = True |
| 56 | |
| 57 | def run(self): |
| 58 | while True: |
| 59 | ret, frame = self.cam.read() |
| 60 | frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) |
| 61 | vis = frame.copy() |
| 62 | if self.p0 is not None: |
| 63 | p2, trace_status = checkedTrace(self.gray1, frame_gray, self.p1) |
| 64 | |
| 65 | self.p1 = p2[trace_status].copy() |
| 66 | self.p0 = self.p0[trace_status].copy() |
| 67 | self.gray1 = frame_gray |
| 68 | |
| 69 | if len(self.p0) < 4: |
| 70 | self.p0 = None |
| 71 | continue |
| 72 | H, status = cv2.findHomography(self.p0, self.p1, (0, cv2.RANSAC)[self.use_ransac], 10.0) |
| 73 | h, w = frame.shape[:2] |
| 74 | overlay = cv2.warpPerspective(self.frame0, H, (w, h)) |
| 75 | vis = cv2.addWeighted(vis, 0.5, overlay, 0.5, 0.0) |
| 76 | |
| 77 | for (x0, y0), (x1, y1), good in zip(self.p0[:,0], self.p1[:,0], status[:,0]): |
| 78 | if good: |
| 79 | cv2.line(vis, (x0, y0), (x1, y1), (0, 128, 0)) |
| 80 | cv2.circle(vis, (x1, y1), 2, (red, green)[good], -1) |
| 81 | draw_str(vis, (20, 20), 'track count: %d' % len(self.p1)) |
| 82 | if self.use_ransac: |
| 83 | draw_str(vis, (20, 40), 'RANSAC') |
| 84 | else: |
| 85 | p = cv2.goodFeaturesToTrack(frame_gray, **feature_params) |
| 86 | if p is not None: |
| 87 | for x, y in p[:,0]: |
| 88 | cv2.circle(vis, (x, y), 2, green, -1) |
| 89 | draw_str(vis, (20, 20), 'feature count: %d' % len(p)) |
| 90 | |
| 91 | cv2.imshow('lk_homography', vis) |
| 92 | |
| 93 | ch = cv2.waitKey(1) |
| 94 | if ch == 27: |
| 95 | break |
| 96 | if ch == ord(' '): |
| 97 | self.frame0 = frame.copy() |
| 98 | self.p0 = cv2.goodFeaturesToTrack(frame_gray, **feature_params) |
| 99 | if self.p0 is not None: |
| 100 | self.p1 = self.p0 |
| 101 | self.gray0 = frame_gray |
| 102 | self.gray1 = frame_gray |
| 103 | if ch == ord('r'): |
| 104 | self.use_ransac = not self.use_ransac |
| 105 | |
| 106 | |
| 107 |
no test coverage detected