自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

用 OpenCV 實現(xiàn) FAST 算法目標跟蹤

開發(fā) 人工智能
FAST算法非常適合實時計算機視覺任務。在本文中,我將解釋FAST算法的工作原理,它的優(yōu)點和缺點,并最終創(chuàng)建一個使用FAST算法的對象跟蹤器。

主要工作

提取特征(角點)并使用FAST算法跟蹤對象:OpenCV,Python

OpenCV中有多種特征提取算法可供使用,但其中一種名為FAST算法的,對于實時計算機視覺應用來說非常有用。大多數(shù)特征提取和角點檢測方法在提取特征方面表現(xiàn)良好,但它們大多數(shù)并不適合實時應用。

FAST算法非常適合實時計算機視覺任務。在本文中,我將解釋FAST算法的工作原理,它的優(yōu)點和缺點,并最終創(chuàng)建一個使用FAST算法的對象跟蹤器。

FAST算法的工作原理是什么?

FAST算法相對簡單。

  • FAST算法選擇一個隨機像素,并在該像素周圍畫一個圓(半徑:3像素),其圓周為16像素。
  • 如果在16像素中有至少12個連續(xù)點的強度比中心像素亮或暗(加上閾值),那么這個中心像素就被視為興趣點(角點)。
  • 為了加快這個過程,算法首先檢查圓周上的4個像素。至少有3個像素必須都比中心像素暗或亮,如果它們不是,該點就不能是興趣點,因為正如我之前所說,至少有12個連續(xù)像素必須更暗或更亮。

查看下面的圖片,它準確地展示了我嘗試解釋的內容。

FAST算法的優(yōu)點和缺點

優(yōu)點:FAST算法非常快。如果你將它與其他特征提取和角點檢測算法進行比較,你會看到差異。實際上,我在另一篇博客文章中比較了ORB、FAST和SIFT算法,結果顯示FAST算法比其他算法快(博客文章鏈接)

缺點:FAST算法對噪聲敏感,因此在噪聲圖像中可能會檢測到錯誤的角點。它不是尺度不變性的,這意味著如果圖像的大小改變,它可能不會很好地工作。

結論

選擇FAST算法為你的任務完全取決于你的目的。如果你需要更高的幀率,并且不太關心提取點的準確性,你可以考慮使用FAST算法。

代碼/使用FAST算法跟蹤對象

有兩個主要步驟:

  • 首先,用戶通過使用鼠標左鍵在目標對象周圍畫矩形來定義目標對象。然后使用FAST算法從這個目標對象(而不是整幅圖像)中提取特征。
  • 接下來,對于每一幀,使用FAST算法提取特征。將目標圖像的特征與每一幀中的特征進行比較。如果有匹配,就在特征位置畫一個圓圈,通過這樣做來跟蹤對象。

(1) 導入庫:


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

(2) 使用鼠標通過在其周圍畫矩形來選擇目標對象:


# Path to video  
video_path=r"videos/fish-video.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_RBUTTONDOWN:
        
        # 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

(3) 從目標對象中提取特征(不是從整幅圖像中):


# take region of interest ( take inside of rectangle )
roi_image=frame[y_min:y_max,x_min:x_max]

# 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=20)
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_image, keypoints_1, outImage=None, color=(0, 255, 0))
# display keypoints
plt.imshow(keypoints_image,cmap="gray")

(4) 使用FAST算法跟蹤對象


# matcher object
bf = cv2.BFMatcher()

# Variables for FPS calculation
frame_count = 0
start_time = time.time()
     
while True :
    # reading video 
    ret,frame=video.read()

    if ret:
          # 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)
    
    
            for match in matches:
            
                # queryIdx gives keypoint index from target image
                query_idx = match.queryIdx
                
                # .trainIdx gives keypoint index from current frame 
                train_idx = match.trainIdx
                
                # take coordinates that matches
                pt1 = keypoints_1[query_idx].pt
                
                # current frame keypoints coordinates
                pt2 = keypoints_2[train_idx].pt
                
                # draw circle to pt2 coordinates , because pt2 gives current frame coordinates
                cv2.circle(frame,(int(pt2[0]),int(pt2[1])),5,(255,0,0),-1)
    
        # Calculate and display FPS
        frame_count += 1
        elapsed_time = time.time() - start_time
        fps = frame_count / elapsed_time
        cv2.putText(frame, f"FPS: {fps:.2f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 3)
        
    
        cv2.imshow("coordinate_screen",frame) 
    
    
        k = cv2.waitKey(5) & 0xFF # after drawing rectangle press esc   
        if k == 27:
            cv2.destroyAllWindows()
            break
    else:
        break

video.release()
cv2.destroyAllWindows()

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

2024-10-28 17:17:32

2010-08-05 08:49:19

2011-10-11 09:15:58

移動應用PhoneGapGoodDay

2025-02-17 07:00:00

ORB對象跟蹤器計算機視覺

2023-09-25 10:13:59

Java識別

2025-03-25 08:30:00

OpenCV計算機視覺圖像識別

2019-05-22 14:28:08

AI人工智能深度學習

2024-06-21 10:40:00

計算機視覺

2017-09-22 11:45:10

深度學習OpenCVPython

2023-01-11 08:59:33

Linuxtraceroute命令

2019-05-14 09:53:31

代碼開發(fā)工具

2021-10-20 11:47:26

威脅情報網絡威脅網絡安全

2020-07-25 19:40:33

Java開發(fā)代碼

2024-11-12 10:20:00

模型數(shù)據

2019-02-18 09:00:00

TextRank算法自然語言處理Python

2024-09-24 17:12:47

2024-09-12 17:19:43

YOLO目標檢測深度學習

2024-07-18 10:37:34

2024-04-25 11:45:09

在線地圖SOTA

2023-10-12 09:21:41

Java圖像
點贊
收藏

51CTO技術棧公眾號