成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

目標跟蹤器 | Kalman + FAST 預測物體運動 | 附代碼

開發
在本文中,我將展示如何使用卡爾曼濾波器和FAST算法來跟蹤物體并預測物體的運動。

對于目標跟蹤,有諸如FAST、SURF、SIFT和ORB等特征提取算法。在從目標物體提取特征后,可以嘗試對每一幀的這些特征進行跟蹤,通過這種方式,可以創建一個簡單的目標跟蹤器。但是,如何預測物體的運動呢?可能想知道1秒后目標物體將位于何處。僅使用特征提取算法是無法做到的,但不用擔心,卡爾曼濾波器非常適合運動預測任務。在本文中,我將展示如何使用卡爾曼濾波器和FAST算法來跟蹤物體并預測物體的運動。

紅色圓圈 → 運動預測

卡爾曼濾波器和FAST算法

卡爾曼濾波器使用過去的數據來預測物體的運動。使用卡爾曼濾波器時,必須跟蹤一個物體,因為卡爾曼濾波器需要位置數據,基于這些位置數據,它預測物體的位置。

使用FAST算法,我將跟蹤物體,提取中心坐標,并使用這些數據與卡爾曼濾波器一起預測物體的位置。

NOTE: 關于FAST算法的文章,后續我們有機會將進行詳細解讀。

代碼/跟蹤和預測物體的運動 → FAST + 卡爾曼濾波器

主要有5個步驟,我將逐一解釋它們。

# Import Necessary Libraries

import cv2
import numpy as np 
import matplotlib.pyplot as plt
import time

1.使用FAST算法提取跟蹤特征:用鼠標左鍵在目標物體周圍畫一個矩形框,將從這個矩形框中提取特征。

# Path to video  
video_path = r"videos/helicopter3.mp4"
video = cv2.VideoCapture(video_path)

# read only the first frame for drawing a rectangle for the desired object
ret,frame = video.read()

# I am giving  big random numbers for x_min and y_min because if you initialize them as zeros whatever coordinate you go minimum will be zero 
x_min,y_min,x_max,y_max=36000,36000,0,0


def coordinat_chooser(event,x,y,flags,param):
    global go , x_min , y_min, x_max , y_max

    # when you click the right button, it will provide coordinates for variables
    if event==cv2.EVENT_LBUTTONDOWN:
        
        # if current coordinate of x lower than the x_min it will be new x_min , same rules apply for y_min 
        x_min=min(x,x_min) 
        y_min=min(y,y_min)

         # if current coordinate of x higher than the x_max it will be new x_max , same rules apply for y_max
        x_max=max(x,x_max)
        y_max=max(y,y_max)

        # draw rectangle
        cv2.rectangle(frame,(x_min,y_min),(x_max,y_max),(0,255,0),1)


    """
        if you didn't like your rectangle (maybe if you made some misscliks),  reset the coordinates with the middle button of your mouse
        if you press the middle button of your mouse coordinates will reset and you can give a new 2-point pair for your rectangle
    """
    if event==cv2.EVENT_MBUTTONDOWN:
        print("reset coordinate  data")
        x_min,y_min,x_max,y_max=36000,36000,0,0

cv2.namedWindow('coordinate_screen')
# Set mouse handler for the specified window, in this case, "coordinate_screen" window
cv2.setMouseCallback('coordinate_screen',coordinat_chooser)


while True:
    cv2.imshow("coordinate_screen",frame) # show only first frame 
    
    k = cv2.waitKey(5) & 0xFF # after drawing rectangle press ESC   
    if k == 27:
        cv2.destroyAllWindows()
        break

繪制矩形框以定位目標

2. 顯示提取的特征:使用FAST算法從矩形框中提取特征。

# take region of interest ( take inside of rectangle )
roi_image=frame[y_min+2:y_max-2,x_min+2:x_max-2]
roi_rgb=cv2.cvtColor(roi_image,cv2.COLOR_BGR2RGB)

# convert roi to grayscale, SIFT Algorithm works with grayscale images
roi_gray=cv2.cvtColor(roi_image,cv2.COLOR_BGR2GRAY) 

# Initialize the FAST detector and BRIEF descriptor extractor
fast = cv2.FastFeatureDetector_create(threshold=1)
brief = cv2.xfeatures2d.BriefDescriptorExtractor_create()


# detect keypoints
keypoints_1 = fast.detect(roi_gray, None)
# descriptors
keypoints_1, descriptors_1 = brief.compute(roi_gray, keypoints_1)

# draw keypoints for visualizing
keypoints_image = cv2.drawKeypoints(roi_rgb, keypoints_1, outImage=None, color=(23, 255, 10))
# display keypoints
plt.imshow(keypoints_image,cmap="gray")

提取的特征

3.創建一個提取目標物體中心位置的函數

# matcher object
bf = cv2.BFMatcher()

def detect_target_fast(frame):
    # convert frame to gray scale 
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Detect keypoints using FAST
    keypoints_2 = fast.detect(frame_gray, None)

    # Compute descriptors using BRIEF
    keypoints_2, descriptors_2 = brief.compute(frame_gray, keypoints_2)

    """
    Compare the keypoints/descriptors extracted from the 
    first frame (from target object) with those extracted from the current frame.
    """
    if descriptors_2 is not None:
        matches = bf.match(descriptors_1, descriptors_2)
        
        if matches:
            # Initialize sums for x and y coordinates
            sum_x = 0
            sum_y = 0
            match_count = 0
            
            for match in matches:
                # .trainIdx gives keypoint index from current frame 
                train_idx = match.trainIdx
                
                # current frame keypoints coordinates
                pt2 = keypoints_2[train_idx].pt
                
                # Sum the x and y coordinates
                sum_x += pt2[0]
                sum_y += pt2[1]
                match_count += 1
            
            # Calculate average of the x and y coordinates
            avg_x = sum_x / match_count
            avg_y = sum_y / match_count
            
    return int(avg_x),int(avg_y)

4.初始化卡爾曼濾波器

# Initialize Kalman filter parameters
kalman = cv2.KalmanFilter(4, 2)   
 
kalman.measurementMatrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0]], np.float32)
kalman.transitionMatrix = np.array([[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]], np.float32)
kalman.processNoiseCov = np.eye(4, dtype=np.float32) * 0.03  # Process noise
kalman.measurementNoiseCov = np.eye(2, dtype=np.float32) * 0.5  # Measurement noise

5.讀取視頻并使用卡爾曼濾波器和FAST算法

# Startcapturing the video from file
cap = cv2.VideoCapture(video_path)

while True:
    ret, frame = cap.read()
    if not ret:
        break
    
    # Predict the new position of the ball
    predicted = kalman.predict()
    predicted_x, predicted_y = int(predicted[0]), int(predicted[1])
    predicted_dx, predicted_dy = predicted[2], predicted[3]  # Predicted velocity

    print(predicted_x, predicted_y )
    print(f"Predicted velocity: (dx: {predicted_dx}, dy: {predicted_dy})")

    
    # Detect the ball in the current frame
    ball_position = detect_target_fast(frame)
    
    if ball_position:
        measured_x, measured_y = ball_position
        # Correct the Kalman Filter with the actual measurement
        kalman.correct(np.array([[np.float32(measured_x)], [np.float32(measured_y)]]))
        # Draw the detected ball
        cv2.circle(frame, (measured_x, measured_y), 6, (0, 255, 0), 2) # green --> correct position
    
    # Draw the predicted position (Kalman Filter result)
    cv2.circle(frame, (predicted_x, predicted_y), 8, (0, 0, 255), 2) # red --> predicted position

    # Show the frame
    cv2.imshow("Kalman Ball Tracking", frame)
    
    # Break on 'q' key press
    if cv2.waitKey(30) & 0xFF == ord('q'):  # 30 ms delay for smooth playback
        break

cap.release()
cv2.destroyAllWindows()

終端輸出

跟蹤和預測飛機

責任編輯:趙寧寧 來源: 小白玩轉Python
相關推薦

2024-12-19 08:00:00

FAST算法OpenCV目標跟蹤

2024-11-20 16:51:00

目標檢測模型

2019-06-06 15:00:10

2020-08-29 18:38:11

物聯網 LPWAN資產跟蹤器

2022-05-27 10:06:17

DuckDuckGo用戶隱私微軟

2022-06-10 10:24:02

JavaScriptCOVID-19前端

2011-01-18 13:50:20

路由跟蹤tcptracerou

2021-03-02 09:42:25

跟蹤器密碼管理器密碼

2018-03-13 11:38:14

2015-01-09 09:41:16

HTTPSHTTPS安全COOKIE

2022-01-05 09:00:00

加密貨幣數據技術

2023-03-02 08:32:27

2011-09-15 15:38:48

Android應用Endomondo運動

2019-05-14 09:53:31

代碼開發工具

2025-01-23 08:47:50

2025-02-17 07:00:00

ORB對象跟蹤器計算機視覺

2023-06-06 06:26:26

2024-10-31 11:03:06

C#橢圓運動緩沖

2010-03-18 11:26:46

無線傳感器網絡多目標跟

2024-09-24 17:12:47

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: av日韩高清 | 欧美激情精品久久久久久 | 国产色婷婷精品综合在线手机播放 | 亚洲国产成人精品女人久久久野战 | 国产高清视频在线观看播放 | 人人艹人人爽 | 亚洲中字在线 | 在线一区视频 | 久热精品免费 | 成人深夜小视频 | 精品欧美一区二区三区精品久久 | 爱草视频 | 99免费在线观看 | 91xxx在线观看 | 成人国产一区二区三区精品麻豆 | 久久精品国产清自在天天线 | 99热国产免费 | 国产分类视频 | 日韩av一区二区在线观看 | 激情 亚洲| 日韩精品一二三 | 99久久精品免费看国产免费软件 | 国产福利资源在线 | 日韩1区| 亚洲欧美激情精品一区二区 | 成人1区2区 | 操久久 | 国产精品久久久久久婷婷天堂 | 亚洲免费婷婷 | 日韩小视频 | 午夜欧美一区二区三区在线播放 | 精品在线一区 | 亚洲欧美国产精品久久 | 一区二区三区四区在线 | 一级毛片色一级 | 午夜影院 | 国产三区精品 | 国产精品日本一区二区不卡视频 | 日韩不卡三区 | 国产精品免费在线 | 操亚洲 |