偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

用 OpenCV 實(shí)現(xiàn) FAST 算法目標(biāo)跟蹤

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

主要工作

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

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

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

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

FAST算法相對簡單。

  • FAST算法選擇一個隨機(jī)像素,并在該像素周圍畫一個圓(半徑:3像素),其圓周為16像素。
  • 如果在16像素中有至少12個連續(xù)點(diǎn)的強(qiáng)度比中心像素亮或暗(加上閾值),那么這個中心像素就被視為興趣點(diǎn)(角點(diǎn))。
  • 為了加快這個過程,算法首先檢查圓周上的4個像素。至少有3個像素必須都比中心像素暗或亮,如果它們不是,該點(diǎn)就不能是興趣點(diǎn),因?yàn)檎缥抑八f,至少有12個連續(xù)像素必須更暗或更亮。

查看下面的圖片,它準(zhǔn)確地展示了我嘗試解釋的內(nèi)容。

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

優(yōu)點(diǎn):FAST算法非??臁H绻銓⑺c其他特征提取和角點(diǎn)檢測算法進(jìn)行比較,你會看到差異。實(shí)際上,我在另一篇博客文章中比較了ORB、FAST和SIFT算法,結(jié)果顯示FAST算法比其他算法快(博客文章鏈接)

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

結(jié)論

選擇FAST算法為你的任務(wù)完全取決于你的目的。如果你需要更高的幀率,并且不太關(guān)心提取點(diǎn)的準(zhǔn)確性,你可以考慮使用FAST算法。

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

有兩個主要步驟:

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

(1) 導(dǎo)入庫:


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

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


# 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) 從目標(biāo)對象中提取特征(不是從整幅圖像中):


# 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()

責(zé)任編輯:趙寧寧 來源: 小白玩轉(zhuǎn)Python
相關(guān)推薦

2024-10-28 17:17:32

2010-08-05 08:49:19

2011-10-11 09:15:58

移動應(yīng)用PhoneGapGoodDay

2025-02-17 07:00:00

ORB對象跟蹤器計(jì)算機(jī)視覺

2023-09-25 10:13:59

Java識別

2019-05-22 14:28:08

AI人工智能深度學(xué)習(xí)

2025-03-25 08:30:00

OpenCV計(jì)算機(jī)視覺圖像識別

2024-06-21 10:40:00

計(jì)算機(jī)視覺

2017-09-22 11:45:10

深度學(xué)習(xí)OpenCVPython

2023-01-11 08:59:33

Linuxtraceroute命令

2019-05-14 09:53:31

代碼開發(fā)工具

2019-02-18 09:00:00

TextRank算法自然語言處理Python

2021-10-20 11:47:26

威脅情報網(wǎng)絡(luò)威脅網(wǎng)絡(luò)安全

2020-07-25 19:40:33

Java開發(fā)代碼

2024-11-12 10:20:00

模型數(shù)據(jù)

2024-09-12 17:19:43

YOLO目標(biāo)檢測深度學(xué)習(xí)

2023-10-12 09:21:41

Java圖像

2024-09-24 17:12:47

2024-07-18 10:37:34

2024-04-25 11:45:09

在線地圖SOTA
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號