Python cv2.goodFeaturesToTrack() Examples
The following are 19
code examples of cv2.goodFeaturesToTrack().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cv2
, or try the search function
.
Example #1
Source File: optical_flow_tracker.py From face_landmark_dnn with MIT License | 7 votes |
def get_new_tracks(self, frame, roi): """Get new tracks every detect_interval frames.""" # Using mask to determine where to look for feature points. mask = np.zeros_like(frame) mask[roi[0]:roi[1], roi[2]:roi[3]] = 255 # Get good feature points. feature_points = cv2.goodFeaturesToTrack( frame, mask=mask, **self.feature_params) if feature_points is not None: for x, y in np.float32(feature_points).reshape(-1, 2): self.tracks.append([(x, y)])
Example #2
Source File: lk_track.py From OpenCV-Python-Tutorial with MIT License | 6 votes |
def run(self): while True: ret, frame = self.cam.read() frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) vis = frame.copy() if len(self.tracks) > 0: img0, img1 = self.prev_gray, frame_gray p0 = np.float32([tr[-1] for tr in self.tracks]).reshape(-1, 1, 2) p1, st, err = cv2.calcOpticalFlowPyrLK(img0, img1, p0, None, **lk_params) p0r, st, err = cv2.calcOpticalFlowPyrLK(img1, img0, p1, None, **lk_params) d = abs(p0-p0r).reshape(-1, 2).max(-1) good = d < 1 new_tracks = [] for tr, (x, y), good_flag in zip(self.tracks, p1.reshape(-1, 2), good): if not good_flag: continue tr.append((x, y)) if len(tr) > self.track_len: del tr[0] new_tracks.append(tr) cv2.circle(vis, (x, y), 2, (0, 255, 0), -1) self.tracks = new_tracks cv2.polylines(vis, [np.int32(tr) for tr in self.tracks], False, (0, 255, 0)) draw_str(vis, (20, 20), 'track count: %d' % len(self.tracks)) if self.frame_idx % self.detect_interval == 0: mask = np.zeros_like(frame_gray) mask[:] = 255 for x, y in [np.int32(tr[-1]) for tr in self.tracks]: cv2.circle(mask, (x, y), 5, 0, -1) p = cv2.goodFeaturesToTrack(frame_gray, mask = mask, **feature_params) if p is not None: for x, y in np.float32(p).reshape(-1, 2): self.tracks.append([(x, y)]) self.frame_idx += 1 self.prev_gray = frame_gray cv2.imshow('lk_track', vis) ch = cv2.waitKey(1) if ch == 27: break
Example #3
Source File: __init__.py From python-smart-crop with MIT License | 6 votes |
def center_from_good_features(matrix): x, y = (0, 0) weight = 0 corners = cv2.goodFeaturesToTrack(matrix, FEATURE_DETECT_MAX_CORNERS, FEATURE_DETECT_QUALITY_LEVEL, FEATURE_DETECT_MIN_DISTANCE) for point in corners: weight += 1 x += point[0][0] y += point[0][1] return { 'x': x / weight, 'y': y / weight, 'count': weight }
Example #4
Source File: DartsRecognition.py From opencv-steel-darts with GNU General Public License v3.0 | 6 votes |
def getCorners(img_in): # number of features to track is a distinctive feature ## FeaturesToTrack important -> make accessible edges = cv2.goodFeaturesToTrack(img_in, 640, 0.0008, 1, mask=None, blockSize=3, useHarrisDetector=1, k=0.06) # k=0.08 corners = np.int0(edges) return corners
Example #5
Source File: feature_tracking.py From OpenCV-3-x-with-Python-By-Example with MIT License | 5 votes |
def add_tracking_paths(frame, tracking_paths): mask = calculate_region_of_interest(frame, tracking_paths) # Extract good features to track. You can learn more # about the parameters here: http://goo.gl/BI2Kml feature_points = cv2.goodFeaturesToTrack(frame, mask = mask, maxCorners = 500, \ qualityLevel = 0.3, minDistance = 7, blockSize = 7) if feature_points is not None: for x, y in np.float32(feature_points).reshape(-1, 2): tracking_paths.append([(x, y)])
Example #6
Source File: example_bbox_keypoint_rotate.py From albumentations_examples with MIT License | 5 votes |
def main(): image = cv2.imread("images/image_1.jpg") keypoints = cv2.goodFeaturesToTrack( cv2.cvtColor(image, cv2.COLOR_RGB2GRAY), maxCorners=100, qualityLevel=0.5, minDistance=5 ).squeeze(1) bboxes = [(kp[0] - 10, kp[1] - 10, kp[0] + 10, kp[1] + 10) for kp in keypoints] disp_image = visualize(image, keypoints, bboxes) plt.figure(figsize=(10, 10)) plt.imshow(cv2.cvtColor(disp_image, cv2.COLOR_RGB2BGR)) plt.tight_layout() plt.show() aug = A.Compose( [A.ShiftScaleRotate(scale_limit=0.1, shift_limit=0.2, rotate_limit=10, always_apply=True)], bbox_params=A.BboxParams(format="pascal_voc", label_fields=["bbox_labels"]), keypoint_params=A.KeypointParams(format="xy"), ) for _i in range(10): data = aug(image=image, keypoints=keypoints, bboxes=bboxes, bbox_labels=np.ones(len(bboxes))) aug_image = data["image"] aug_image = visualize(aug_image, data["keypoints"], data["bboxes"]) plt.figure(figsize=(10, 10)) plt.imshow(cv2.cvtColor(aug_image, cv2.COLOR_RGB2BGR)) plt.tight_layout() plt.show()
Example #7
Source File: optical_flow_tracker.py From head-pose-estimation with MIT License | 5 votes |
def get_new_tracks(self, frame, roi): """Get new tracks every detect_interval frames.""" # Using mask to determine where to look for feature points. mask = np.zeros_like(frame) mask[roi[0]:roi[1], roi[2]:roi[3]] = 255 # Get good feature points. feature_points = cv2.goodFeaturesToTrack( frame, mask=mask, **self.feature_params) if feature_points is not None: for x, y in np.float32(feature_points).reshape(-1, 2): self.tracks.append([(x, y)])
Example #8
Source File: feature_shitomasi.py From pyslam with GNU General Public License v3.0 | 5 votes |
def detect(self, frame, mask=None): pts = cv2.goodFeaturesToTrack(frame, self.num_features, self.quality_level, self.min_coner_distance, blockSize=self.blockSize, mask=mask) # convert matrix of pts into list of keypoints if pts is not None: kps = [ cv2.KeyPoint(p[0][0], p[0][1], self.blockSize) for p in pts ] else: kps = [] #if kVerbose: # print('detector: Shi-Tomasi, #features: ', len(kps), ', #ref: ', self.num_features, ', frame res: ', frame.shape[0:2]) return kps
Example #9
Source File: gftt.py From imutils with MIT License | 5 votes |
def detect(self, img): cnrs = cv2.goodFeaturesToTrack(img, self.maxCorners, self.qualityLevel, self.minDistance, mask=self.mask, blockSize=self.blockSize, useHarrisDetector=self.useHarrisDetector, k=self.k) return corners_to_keypoints(cnrs)
Example #10
Source File: frame.py From twitchslam with MIT License | 5 votes |
def extractFeatures(img): orb = cv2.ORB_create() # detection pts = cv2.goodFeaturesToTrack(np.mean(img, axis=2).astype(np.uint8), 3000, qualityLevel=0.01, minDistance=7) # extraction kps = [cv2.KeyPoint(x=f[0][0], y=f[0][1], _size=20) for f in pts] kps, des = orb.compute(img, kps) # return pts and des return np.array([(kp.pt[0], kp.pt[1]) for kp in kps]), des
Example #11
Source File: models.py From rainymotion with MIT License | 4 votes |
def _sparse_sd(data_instance, of_params={'st_pars': dict(maxCorners = 200, qualityLevel = 0.2, minDistance = 7, blockSize = 21), 'lk_pars': dict(winSize = (20, 20), maxLevel = 2, criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0))}, lead_steps=12): # define penult and last frames penult_frame = data_instance[-2] last_frame = data_instance[-1] # find features to track old_corners = cv2.goodFeaturesToTrack(data_instance[0], mask=None, **of_params['st_pars']) # track corners by optical flow algorithm new_corners, st, err = cv2.calcOpticalFlowPyrLK(prevImg=penult_frame, nextImg=last_frame, prevPts=old_corners, nextPts=None, **of_params['lk_pars']) # select only good attempts for corner tracking success = st.ravel() == 1 new_corners = new_corners[success].copy() old_corners = old_corners[success].copy() # calculate Simple Delta delta = new_corners.reshape(-1, 2) - old_corners.reshape(-1, 2) # simplificate furher transformations pts_source = new_corners.reshape(-1, 2) # propagate our corners through time pts_target_container = [] for lead_step in range(lead_steps): pts_target_container.append(pts_source + delta * (lead_step + 1)) return pts_source, pts_target_container
Example #12
Source File: feature_matching.py From dual-fisheye-video-stitching with MIT License | 4 votes |
def getMatches_goodtemplmatch(img1, img2, templ_shape, max): """Return pairs of corresponding points using combining Shi-Tomasi corner detector and template matching.""" if not np.array_equal(img1.shape, img2.shape): print "error: inconsistent array dimention", img1.shape, img2.shape sys.exit() if not (np.all(templ_shape <= img1.shape[:2]) and np.all(templ_shape <= img2.shape[:2])): print "error: template shape shall fit img1 and img2" sys.exit() feature_params = dict(maxCorners=max, qualityLevel=0.01, minDistance=5, blockSize=5) kps1 = cv2.goodFeaturesToTrack(img1, mask=None, **feature_params) kps2 = cv2.goodFeaturesToTrack(img2, mask=None, **feature_params) Hs, Ws = img1.shape[:2] Ht, Wt = templ_shape matches = [] for [[xt, yt]] in kps1: if int(yt) + Ht > Hs or int(xt) + Wt > Ws: continue result = cv2.matchTemplate( img2, img1[int(yt):int(yt) + Ht, int(xt):int(xt) + Wt], cv2.TM_CCOEFF_NORMED) minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(result) if maxVal > 0.85: matches.append((maxVal, (int(xt), int(yt)), maxLoc)) for [[xt, yt]] in kps2: if int(yt) + Ht > Hs or int(xt) + Wt > Ws: continue result = cv2.matchTemplate( img1, img2[int(yt):int(yt) + Ht, int(xt):int(xt) + Wt], cv2.TM_CCOEFF_NORMED) minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(result) if maxVal > 0.85: matches.append((maxVal, maxLoc, (int(xt), int(yt)))) matches.sort(key=lambda e: e[0], reverse=True) if len(matches) >= max: return np.int32([matches[i][1:] for i in range(max)]) else: return np.int32([c[1:] for c in matches])
Example #13
Source File: opticalFlow.py From Mask-RCNN-Pedestrian-Detection with MIT License | 4 votes |
def sparseOpticalFlow(): # use 0 for webcam capturing # cap = cv2.VideoCapture(0) cap = cv2.VideoCapture('test/Pedestrian overpass.mp4') # params for ShiTomasi corner detection feature_params = dict( maxCorners = 100, qualityLevel = 0.3, minDistance = 7, blockSize = 7 ) # Parameters for lucas kanade optical flow lk_params = dict( winSize = (15,15), maxLevel = 2, criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) # Create some random colors color = np.random.randint(0,255,(100,3)) # Take first frame and find corners in it ret, old_frame = cap.read() old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) p0 = cv2.goodFeaturesToTrack(old_gray, mask = None, **feature_params) # Create a mask image for drawing purposes mask = np.zeros_like(old_frame) while(1): ret,frame = cap.read() frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # calculate optical flow p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params) # Select good points good_new = p1[st==1] good_old = p0[st==1] # draw the tracks for i,(new,old) in enumerate(zip(good_new,good_old)): a,b = new.ravel() c,d = old.ravel() mask = cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2) frame = cv2.circle(frame,(a,b),5,color[i].tolist(),-1) img = cv2.add(frame,mask) cv2.imshow('frame',img) k = cv2.waitKey(30) & 0xff if k == 27: break # Now update the previous frame and previous points old_gray = frame_gray.copy() p0 = good_new.reshape(-1,1,2) cv2.destroyAllWindows() cap.release() # DENSE OPTICAL FLOW
Example #14
Source File: tracking.py From pi-tracking-telescope with MIT License | 4 votes |
def updateError(self, frame): self.frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) if len(self.tracks) > 0: img0, img1 = self.prev_gray, self.frame_gray p0 = np.float32([tr[-1][:2] for tr in self.tracks]).reshape(-1, 1, 2) p1, st, err = cv2.calcOpticalFlowPyrLK(img0, img1, p0, None, **self.lk_params) p0r, st, err = cv2.calcOpticalFlowPyrLK(img1, img0, p1, None, **self.lk_params) d = abs(p0-p0r).reshape(-1, 2).max(-1) good = d < 1 new_tracks = [] self.xerror = 0.0 self.yerror = 0.0 self.n = 0.0 current_time = time.time() for tr, (x, y), good_flag in zip(self.tracks, p1.reshape(-1, 2), good): if not good_flag: continue tr.append((x, y, current_time)) if len(tr) > 500: del tr[0] new_tracks.append(tr) if(len(tr)>=2): t = np.float32([v[2] for v in tr]) x = np.float32([v[0] for v in tr]) y = np.float32([v[1] for v in tr]) self.xerror = self.xerror + (x[-1] - x[0]) self.yerror = self.yerror + (y[-1] - y[0]) self.n = self.n + 1.0 if self.n>0: self.xerror = self.xerror / float(self.n) self.yerror = self.yerror / float(self.n) self.tracks = new_tracks if self.xerror==0 and self.yerror==0: current_time = time.time() mask = np.zeros_like(self.frame_gray) mask[:] = 255 p = cv2.goodFeaturesToTrack(self.frame_gray, mask = mask, **self.feature_params) if p is not None: for x, y in np.float32(p).reshape(-1, 2): self.tracks.append([(x, y, current_time)]) self.prev_gray = self.frame_gray
Example #15
Source File: lk_homography.py From OpenCV-Python-Tutorial with MIT License | 4 votes |
def run(self): while True: ret, frame = self.cam.read() frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) vis = frame.copy() if self.p0 is not None: p2, trace_status = checkedTrace(self.gray1, frame_gray, self.p1) self.p1 = p2[trace_status].copy() self.p0 = self.p0[trace_status].copy() self.gray1 = frame_gray if len(self.p0) < 4: self.p0 = None continue H, status = cv2.findHomography(self.p0, self.p1, (0, cv2.RANSAC)[self.use_ransac], 10.0) h, w = frame.shape[:2] overlay = cv2.warpPerspective(self.frame0, H, (w, h)) vis = cv2.addWeighted(vis, 0.5, overlay, 0.5, 0.0) for (x0, y0), (x1, y1), good in zip(self.p0[:,0], self.p1[:,0], status[:,0]): if good: cv2.line(vis, (x0, y0), (x1, y1), (0, 128, 0)) cv2.circle(vis, (x1, y1), 2, (red, green)[good], -1) draw_str(vis, (20, 20), 'track count: %d' % len(self.p1)) if self.use_ransac: draw_str(vis, (20, 40), 'RANSAC') else: p = cv2.goodFeaturesToTrack(frame_gray, **feature_params) if p is not None: for x, y in p[:,0]: cv2.circle(vis, (x, y), 2, green, -1) draw_str(vis, (20, 20), 'feature count: %d' % len(p)) cv2.imshow('lk_homography', vis) ch = cv2.waitKey(1) if ch == 27: break if ch == ord(' '): self.frame0 = frame.copy() self.p0 = cv2.goodFeaturesToTrack(frame_gray, **feature_params) if self.p0 is not None: self.p1 = self.p0 self.gray0 = frame_gray self.gray1 = frame_gray if ch == ord('r'): self.use_ransac = not self.use_ransac
Example #16
Source File: lk_track.py From PyCV-time with MIT License | 4 votes |
def run(self): while True: ret, frame = self.cam.read() frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) vis = frame.copy() if len(self.tracks) > 0: img0, img1 = self.prev_gray, frame_gray p0 = np.float32([tr[-1] for tr in self.tracks]).reshape(-1, 1, 2) p1, st, err = cv2.calcOpticalFlowPyrLK(img0, img1, p0, None, **lk_params) p0r, st, err = cv2.calcOpticalFlowPyrLK(img1, img0, p1, None, **lk_params) d = abs(p0-p0r).reshape(-1, 2).max(-1) good = d < 1 new_tracks = [] for tr, (x, y), good_flag in zip(self.tracks, p1.reshape(-1, 2), good): if not good_flag: continue tr.append((x, y)) if len(tr) > self.track_len: del tr[0] new_tracks.append(tr) cv2.circle(vis, (x, y), 2, (0, 255, 0), -1) self.tracks = new_tracks cv2.polylines(vis, [np.int32(tr) for tr in self.tracks], False, (0, 255, 0)) draw_str(vis, (20, 20), 'track count: %d' % len(self.tracks)) if self.frame_idx % self.detect_interval == 0: mask = np.zeros_like(frame_gray) mask[:] = 255 for x, y in [np.int32(tr[-1]) for tr in self.tracks]: cv2.circle(mask, (x, y), 5, 0, -1) p = cv2.goodFeaturesToTrack(frame_gray, mask = mask, **feature_params) if p is not None: for x, y in np.float32(p).reshape(-1, 2): self.tracks.append([(x, y)]) self.frame_idx += 1 self.prev_gray = frame_gray cv2.imshow('lk_track', vis) ch = 0xFF & cv2.waitKey(1) if ch == 27: break
Example #17
Source File: lk_homography.py From PyCV-time with MIT License | 4 votes |
def run(self): while True: ret, frame = self.cam.read() frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) vis = frame.copy() if self.p0 is not None: p2, trace_status = checkedTrace(self.gray1, frame_gray, self.p1) self.p1 = p2[trace_status].copy() self.p0 = self.p0[trace_status].copy() self.gray1 = frame_gray if len(self.p0) < 4: self.p0 = None continue H, status = cv2.findHomography(self.p0, self.p1, (0, cv2.RANSAC)[self.use_ransac], 10.0) h, w = frame.shape[:2] overlay = cv2.warpPerspective(self.frame0, H, (w, h)) vis = cv2.addWeighted(vis, 0.5, overlay, 0.5, 0.0) for (x0, y0), (x1, y1), good in zip(self.p0[:,0], self.p1[:,0], status[:,0]): if good: cv2.line(vis, (x0, y0), (x1, y1), (0, 128, 0)) cv2.circle(vis, (x1, y1), 2, (red, green)[good], -1) draw_str(vis, (20, 20), 'track count: %d' % len(self.p1)) if self.use_ransac: draw_str(vis, (20, 40), 'RANSAC') else: p = cv2.goodFeaturesToTrack(frame_gray, **feature_params) if p is not None: for x, y in p[:,0]: cv2.circle(vis, (x, y), 2, green, -1) draw_str(vis, (20, 20), 'feature count: %d' % len(p)) cv2.imshow('lk_homography', vis) ch = 0xFF & cv2.waitKey(1) if ch == 27: break if ch == ord(' '): self.frame0 = frame.copy() self.p0 = cv2.goodFeaturesToTrack(frame_gray, **feature_params) if self.p0 is not None: self.p1 = self.p0 self.gray0 = frame_gray self.gray1 = frame_gray if ch == ord('r'): self.use_ransac = not self.use_ransac
Example #18
Source File: lk_track.py From PyCV-time with MIT License | 4 votes |
def run(self): while True: ret, frame = self.cam.read() frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) vis = frame.copy() if len(self.tracks) > 0: img0, img1 = self.prev_gray, frame_gray p0 = np.float32([tr[-1] for tr in self.tracks]).reshape(-1, 1, 2) p1, st, err = cv2.calcOpticalFlowPyrLK(img0, img1, p0, None, **lk_params) p0r, st, err = cv2.calcOpticalFlowPyrLK(img1, img0, p1, None, **lk_params) d = abs(p0-p0r).reshape(-1, 2).max(-1) good = d < 1 new_tracks = [] for tr, (x, y), good_flag in zip(self.tracks, p1.reshape(-1, 2), good): if not good_flag: continue tr.append((x, y)) if len(tr) > self.track_len: del tr[0] new_tracks.append(tr) cv2.circle(vis, (x, y), 2, (0, 255, 0), -1) self.tracks = new_tracks cv2.polylines(vis, [np.int32(tr) for tr in self.tracks], False, (0, 255, 0)) draw_str(vis, (20, 20), 'track count: %d' % len(self.tracks)) if self.frame_idx % self.detect_interval == 0: mask = np.zeros_like(frame_gray) mask[:] = 255 for x, y in [np.int32(tr[-1]) for tr in self.tracks]: cv2.circle(mask, (x, y), 5, 0, -1) p = cv2.goodFeaturesToTrack(frame_gray, mask = mask, **feature_params) if p is not None: for x, y in np.float32(p).reshape(-1, 2): self.tracks.append([(x, y)]) self.frame_idx += 1 self.prev_gray = frame_gray cv2.imshow('lk_track', vis) ch = 0xFF & cv2.waitKey(1) if ch == 27: break
Example #19
Source File: lk_homography.py From PyCV-time with MIT License | 4 votes |
def run(self): while True: ret, frame = self.cam.read() frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) vis = frame.copy() if self.p0 is not None: p2, trace_status = checkedTrace(self.gray1, frame_gray, self.p1) self.p1 = p2[trace_status].copy() self.p0 = self.p0[trace_status].copy() self.gray1 = frame_gray if len(self.p0) < 4: self.p0 = None continue H, status = cv2.findHomography(self.p0, self.p1, (0, cv2.RANSAC)[self.use_ransac], 10.0) h, w = frame.shape[:2] overlay = cv2.warpPerspective(self.frame0, H, (w, h)) vis = cv2.addWeighted(vis, 0.5, overlay, 0.5, 0.0) for (x0, y0), (x1, y1), good in zip(self.p0[:,0], self.p1[:,0], status[:,0]): if good: cv2.line(vis, (x0, y0), (x1, y1), (0, 128, 0)) cv2.circle(vis, (x1, y1), 2, (red, green)[good], -1) draw_str(vis, (20, 20), 'track count: %d' % len(self.p1)) if self.use_ransac: draw_str(vis, (20, 40), 'RANSAC') else: p = cv2.goodFeaturesToTrack(frame_gray, **feature_params) if p is not None: for x, y in p[:,0]: cv2.circle(vis, (x, y), 2, green, -1) draw_str(vis, (20, 20), 'feature count: %d' % len(p)) cv2.imshow('lk_homography', vis) ch = 0xFF & cv2.waitKey(1) if ch == 27: break if ch == ord(' '): self.frame0 = frame.copy() self.p0 = cv2.goodFeaturesToTrack(frame_gray, **feature_params) if self.p0 is not None: self.p1 = self.p0 self.gray0 = frame_gray self.gray1 = frame_gray if ch == ord('r'): self.use_ransac = not self.use_ransac