当前位置: 首页>>代码示例>>Python>>正文


Python cv2.calcOpticalFlowPyrLK方法代码示例

本文整理汇总了Python中cv2.calcOpticalFlowPyrLK方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.calcOpticalFlowPyrLK方法的具体用法?Python cv2.calcOpticalFlowPyrLK怎么用?Python cv2.calcOpticalFlowPyrLK使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cv2的用法示例。


在下文中一共展示了cv2.calcOpticalFlowPyrLK方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_none_optical_flow

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import calcOpticalFlowPyrLK [as 别名]
def test_none_optical_flow():
    prev_gray = np.ones((1208, 1920, 3)) * 250
    current_frame_gray = np.ones((1208, 1920, 3)) * 250
    prev_kps = np.array([], dtype='float32')
    # noinspection PyArgumentList
    prev_kps = prev_kps.reshape(0, 1, 2)

    none_optical_flow = cv2.calcOpticalFlowPyrLK(prev_gray,
                                                 current_frame_gray,
                                                 prev_kps,
                                                 None)

    matched_keypoints = utils.match_keypoints(none_optical_flow, prev_kps)
    transform_i = utils.estimate_partial_transform(matched_keypoints)

    assert transform_i == [0, 0, 0] 
开发者ID:AdamSpannbauer,项目名称:python_video_stab,代码行数:18,代码来源:test_vidstab_utils.py

示例2: match_keypoints

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import calcOpticalFlowPyrLK [as 别名]
def match_keypoints(optical_flow, prev_kps):
    """Match optical flow keypoints

    :param optical_flow: output of cv2.calcOpticalFlowPyrLK
    :param prev_kps: keypoints that were passed to cv2.calcOpticalFlowPyrLK to create optical_flow
    :return: tuple of (cur_matched_kp, prev_matched_kp)
    """
    cur_kps, status, err = optical_flow

    # storage for keypoints with status 1
    prev_matched_kp = []
    cur_matched_kp = []

    if status is None:
        return cur_matched_kp, prev_matched_kp

    for i, matched in enumerate(status):
        # store coords of keypoints that appear in both
        if matched:
            prev_matched_kp.append(prev_kps[i])
            cur_matched_kp.append(cur_kps[i])

    return cur_matched_kp, prev_matched_kp 
开发者ID:AdamSpannbauer,项目名称:python_video_stab,代码行数:25,代码来源:vidstab_utils.py

示例3: _gen_next_raw_transform

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import calcOpticalFlowPyrLK [as 别名]
def _gen_next_raw_transform(self):
        current_frame = self.frame_queue.frames[-1]
        current_frame_gray = current_frame.gray_image
        current_frame_gray = self._resize_frame(current_frame_gray)

        # calc flow of movement
        optical_flow = cv2.calcOpticalFlowPyrLK(self.prev_gray,
                                                current_frame_gray,
                                                self.prev_kps, None)

        matched_keypoints = vidstab_utils.match_keypoints(optical_flow, self.prev_kps)
        transform_i = vidstab_utils.estimate_partial_transform(matched_keypoints)

        # update previous frame info for next iteration
        self._update_prev_frame(current_frame_gray)
        self._raw_transforms.append(transform_i[:])
        self._update_trajectory(transform_i) 
开发者ID:AdamSpannbauer,项目名称:python_video_stab,代码行数:19,代码来源:VidStab.py

示例4: compute_feature_points

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import calcOpticalFlowPyrLK [as 别名]
def compute_feature_points(tracking_paths, prev_img, current_img):
    feature_points = [tp[-1] for tp in tracking_paths]
    # Vector of 2D points for which the flow needs to be found
    feature_points_0 = np.float32(feature_points).reshape(-1, 1, 2) 

    feature_points_1, status_1, err_1 = cv2.calcOpticalFlowPyrLK(prev_img, current_img, \
        feature_points_0, None, **tracking_params) 
    feature_points_0_rev, status_2, err_2 = cv2.calcOpticalFlowPyrLK(current_img, prev_img, \
        feature_points_1, None, **tracking_params)

    # Compute the difference of the feature points 
    diff_feature_points = abs(feature_points_0-feature_points_0_rev).reshape(-1, 2).max(-1) 

    # threshold and keep only the good points 
    good_points = diff_feature_points < 1
    return feature_points_1.reshape(-1, 2), good_points 
开发者ID:PacktPublishing,项目名称:OpenCV-3-x-with-Python-By-Example,代码行数:18,代码来源:feature_tracking.py

示例5: _calculate_optical_flow

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import calcOpticalFlowPyrLK [as 别名]
def _calculate_optical_flow(self, old_gray, frame_gray, p0):
        """This function tracks the edge of the Middle finger.

       points for tracking:
            expected_ext_left
            expected_ext_right
            expected_ext_top
            expected_ext_bot
            palm_center_point

        :param old_gray: old frame, gray scale
        :param frame_gray: current frame
        :return: p0- updated tracking point,

        """
        # Calculate optical flow
        p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **self.lk_params)
        if p1 is None:
            good_new = p0[st == 1]
        else:
            good_new = p1[st == 1]

        # Now update the previous frame and previous points.
        self._old_gray = frame_gray.copy()
        self._p0 = good_new.reshape(-1, 1, 2) 
开发者ID:GalBrandwine,项目名称:HalloPy,代码行数:27,代码来源:controller.py

示例6: track

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import calcOpticalFlowPyrLK [as 别名]
def track(self, image_ref, image_cur, kps_ref, des_ref = None):
        kps_cur, st, err = cv2.calcOpticalFlowPyrLK(image_ref, image_cur, kps_ref, None, **self.lk_params)  #shape: [k,2] [k,1] [k,1]
        st = st.reshape(st.shape[0])
        res = FeatureTrackingResult()    
        #res.idxs_ref = (st == 1)
        res.idxs_ref = [i for i,v in enumerate(st) if v== 1]
        res.idxs_cur = res.idxs_ref.copy()       
        res.kps_ref_matched = kps_ref[res.idxs_ref] 
        res.kps_cur_matched = kps_cur[res.idxs_cur]  
        res.kps_ref = res.kps_ref_matched  # with LK we follow feature trails hence we can forget unmatched features 
        res.kps_cur = res.kps_cur_matched
        res.des_cur = None                      
        return res         
        

# Extract features by using desired detector and descriptor, match keypoints by using desired matcher on computed descriptors 
开发者ID:luigifreda,项目名称:pyslam,代码行数:18,代码来源:feature_tracker.py

示例7: feature_tracking

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import calcOpticalFlowPyrLK [as 别名]
def feature_tracking(img1,img2, points1,points2,status): #track matching features
        err = np.array([])
        winSize = (15,15)
        maxLevel = 3
        termcriteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 30, 0.01))
        cv2.calcOpticalFlowPyrLK(img1, img2, points1, points2, status, err, winSize, maxLevel, termcriteria, 0, 0.001)
        indexcorrection = 0
        #remove bad points 
        for i in range(len(status)):
                pt = points2[i - indexcorrection]
                if (status[i]==0 or pt[0,0]<0 or pt[0,1]<0):
                        if pt[0,0]<0 or pt[0,1]<0:
                                status[i]=0
                        np.delete(points1, i-indexcorrection)
                        np.delete(points2, i-indexcorrection)
                        indexcorrection+=1 
开发者ID:karanchawla,项目名称:Monocular-Visual-Inertial-Odometry,代码行数:18,代码来源:vo.py

示例8: checkedTrace

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import calcOpticalFlowPyrLK [as 别名]
def checkedTrace(img0, img1, p0, back_threshold = 1.0):
    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)
    status = d < back_threshold
    return p1, status 
开发者ID:makelove,项目名称:OpenCV-Python-Tutorial,代码行数:8,代码来源:lk_homography.py

示例9: update_tracks

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import calcOpticalFlowPyrLK [as 别名]
def update_tracks(self, img_old, img_new):
        """Update tracks."""
        # Get old points, using the latest one.
        points_old = np.float32([track[-1]
                                 for track in self.tracks]).reshape(-1, 1, 2)

        # Get new points from old points.
        points_new, _st, _err = cv2.calcOpticalFlowPyrLK(
            img_old, img_new, points_old, None, **self.lk_params)

        # Get inferred old points from new points.
        points_old_inferred, _st, _err = cv2.calcOpticalFlowPyrLK(
            img_new, img_old, points_new, None, **self.lk_params)

        # Compare between old points and inferred old points
        error_term = abs(
            points_old - points_old_inferred).reshape(-1, 2).max(-1)
        point_valid = error_term < 1

        new_tracks = []
        for track, (x, y), good_flag in zip(self.tracks, points_new.reshape(-1, 2), point_valid):
            # Track is good?
            if not good_flag:
                continue

            # New point is good, add to track.
            track.append((x, y))

            # Need to drop first old point?
            if len(track) > self.track_len:
                del track[0]

            # Track updated, add to track groups.
            new_tracks.append(track)

        # New track groups got, do update.
        self.tracks = new_tracks 
开发者ID:junhwanjang,项目名称:face_landmark_dnn,代码行数:39,代码来源:optical_flow_tracker.py

示例10: track

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import calcOpticalFlowPyrLK [as 别名]
def track(img_0, ips_rc_0, img_1, border_margin=15):
    """ Returns keypoint tracking status and the set
        of tracked keypoints. Keypoints given colwise and in (row, col).
        Status is TRACK_SUCCESS, BORDER_LOST or POOR_TRACK
        """
    status = np.ones(ips_rc_0.shape[1], dtype=int) * TRACK_SUCCESS
    if ips_rc_0.size == 0:
        return status, np.zeros_like(ips_rc_0)

    ips_xy_0 = np.reshape(np.fliplr(ips_rc_0.T), (-1, 1, 2)).astype(np.float32)
    ips_xy_1, _, _ = cv2.calcOpticalFlowPyrLK(
        img_0, img_1, ips_xy_0, None)

    # Point lost at border.
    ips_rc_1_for_bordlost = np.fliplr(np.reshape(ips_xy_1, (-1, 2))).T
    not_bordlost = points.haveMarginToBorder(
        ips_rc_1_for_bordlost, img_1.shape, border_margin)

    # Symmetry: Reject all that don't come back to within 1 px:
    re_ips_xy_0, _, _ = cv2.calcOpticalFlowPyrLK(
        img_1, img_0, ips_xy_1, None)
    err = np.linalg.norm(re_ips_xy_0 - ips_xy_0, axis=2)
    tracked = (err < 1).ravel()
    status[np.logical_not(tracked)] = POOR_TRACK
    status[np.logical_not(not_bordlost)] = BORDER_LOST

    successful = np.logical_and(not_bordlost, tracked)

    ips_xy_1 = ips_xy_1[successful, :, :]
    ips_rc_1 = np.fliplr(np.reshape(ips_xy_1, (-1, 2))).T

    assert (np.count_nonzero(status == TRACK_SUCCESS) == ips_rc_1.shape[1])

    return status, ips_rc_1 
开发者ID:uzh-rpg,项目名称:imips_open,代码行数:36,代码来源:klt.py

示例11: estimate_loop

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import calcOpticalFlowPyrLK [as 别名]
def estimate_loop(self):
        opt_flow_params = dict(winSize=(15,15), maxLevel=2, criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
        while not self.stopped:
            frame = self.video_feed.read()
            frame_grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            # Pull data from each human and bodypart -> put into np array w/shape (num_humans, 18, 2) and reshape to (num_humans 18, 1, 2) for use by optical flow
            with self.lock:
                all_human_points = np.asarray([np.asarray([[[body_part.x * self.frame_shape[1], body_part.y * self.frame_shape[0]]] for key, body_part in human.body_parts.iteritems()], dtype=np.float32) for human in self.humans])
                for idx, human_points in enumerate(all_human_points):
                        p1, st, err = cv2.calcOpticalFlowPyrLK(self.old_frame_grey, frame_grey, human_points, None, **opt_flow_params)
                        self.repack_humans(p1, idx)

                        # Grab the points that have gone out of frame
                        oof_points = p1[st!=1]
                        if oof_points.shape != 0:
                            # Get all the matches
                            tmp = np.isin(human_points, oof_points)
                            # Get the indexes of those matches
                            msng_idxz = [msng for msng in range(len(human_points)) if tmp[msng].all()]
                            #print "msng_idxz %s" % str(msng_idxz)
                            cur_part_exist = self.humans[idx].body_parts.keys()
                            for foo_idx in range(len(msng_idxz)):
                                del self.humans[idx].body_parts[cur_part_exist[msng_idxz[foo_idx]]]
                        if len(self.humans[idx].body_parts.keys()) == 0:
                            del self.humans[idx]

            self.old_frame = frame
            self.old_frame_grey = frame_grey.copy() 
开发者ID:NVIDIA-AI-IOT,项目名称:Gesture-Recognition,代码行数:30,代码来源:OptFlowEst.py

示例12: featureTracking

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import calcOpticalFlowPyrLK [as 别名]
def featureTracking(image_ref, image_cur, px_ref):
	kp2, st, err = cv2.calcOpticalFlowPyrLK(image_ref, image_cur, px_ref, None, **lk_params)  #shape: [k,2] [k,1] [k,1]

	st = st.reshape(st.shape[0])
	kp1 = px_ref[st == 1]
	kp2 = kp2[st == 1]

	return kp1, kp2 
开发者ID:karanchawla,项目名称:Monocular-Visual-Inertial-Odometry,代码行数:10,代码来源:visual_odometry.py

示例13: track_features_on_klt

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import calcOpticalFlowPyrLK [as 别名]
def track_features_on_klt(self, tracks_obj, tracker_params):
        """
        tracks features in feature_init using the dataset
        tracks must be dict with keys as ids and values as 1 x 3 array with x,y,t
        returns a dict with keys as track ids, and values as N x 3 arrays, with x,y,t.
        If collate is true, returns N x 4 array with id,x,y,t .
        """
        assert "reference_track" in tracker_params
        assert "frame_dataset" in tracker_params

        window_size = self.config["window_size"]
        num_pyramidal_layers = self.config["num_pyramidal_layers"]

        dataset = tracker_params["frame_dataset"]
        dataset.set_to_first_after(tracks_obj.t)

        print("Tracking with KLT parameters: [window_size=%s num_pyramidal_layers=%s]" % (window_size, num_pyramidal_layers))
        for i, (t, img) in enumerate(tqdm.tqdm(dataset)):
            if i == 0:
                first_img = img
                continue

            second_img = img
            
            if len(tracks_obj.active_features) == 0:
                break
                
            new_features, status, err = \
                cv2.calcOpticalFlowPyrLK(first_img, second_img, tracks_obj.active_features,
                                         None, winSize=(window_size, window_size), maxLevel=num_pyramidal_layers)

            tracks_obj.update(status[:,0]==1, new_features, t)

            first_img = second_img

        tracks = tracks_obj.collate()
        return tracks 
开发者ID:uzh-rpg,项目名称:rpg_feature_tracking_analysis,代码行数:39,代码来源:tracker.py

示例14: feed

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import calcOpticalFlowPyrLK [as 别名]
def feed(self,frame):
        if self.old_gray is None:
            self.old_frame=frame.copy()
            #self.old_gray=cv2.cvtColor(self.old_frame, cv2.COLOR_BGR2GRAY)
            self.old_gray=self.old_frame[:,:,2]
            margx=120
            margy=30
            self.p0 = np.array([(i,j) for i in range(margx,frame.shape[1]-margx,30) for j in range(margy,frame.shape[0]-margy,30)],dtype='float32').reshape(-1,1,2)
            self.color=self.color[:len(self.p0)]
            self.initial_state=[self.p0,self.color]
        #import ipdb;ipdb.set_trace()
        frame_gray = frame[:,:,2].copy()
        #frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        tic=time.time()
        p1, st, err = cv2.calcOpticalFlowPyrLK(self.old_gray, frame_gray, self.p0, None, **self.lk_params)
        #print('-------------------->',time.time()-tic)
        # Select good points
        good_new = p1[st==1]
        good_old = self.p0[st==1]
        self.color=self.color[(st==1).flatten()]

        # draw the tracks
        self.old_frame = frame.copy()
        for i,new in enumerate(good_new):
             a,b = new.ravel()
             frame = cv2.circle(frame,(a,b),2,self.color[i].tolist(),-1)

        # Now update the previous frame and previous points
        self.old_gray = frame_gray
        self.p0 = good_new.reshape(-1,1,2)
        return frame 
开发者ID:orig74,项目名称:UE4PyServer,代码行数:33,代码来源:optical_flow.py

示例15: run

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import calcOpticalFlowPyrLK [as 别名]
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 
开发者ID:makelove,项目名称:OpenCV-Python-Tutorial,代码行数:46,代码来源:lk_track.py


注:本文中的cv2.calcOpticalFlowPyrLK方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。