Harris 角點檢測和Shi-Tomasi 角點檢測

Harris 角點檢測和Shi-Tomasi 角點檢測

這篇文章是關於一些流行的(Harris & Shi-Tomasi)角點檢測算法和如何在Python和OpenCV實現它們。但是,在此之前,我們需要知道什麼是圖像特徵:

圖像的特徵是提供豐富圖像內容信息的興趣點。它們基本上由兩部分組成:

  • 興趣點:對旋轉、平移、強度和尺度變化不變的圖像中的點。有不同的興趣點,如角,邊,斑點等。
  • 特徵描述符:這些描述了圍繞向量中的興趣點的圖像patch。它們可以簡單到原始像素值,也可以複雜到像梯度直方圖(HoG)等。

因此角點檢測基本上就是檢測圖像中的(一種)興趣點。

角點檢測:為圖像位置,其中位置的輕微偏移將導致水平(X)和垂直(Y)軸的強度發生大的變化。

Harris 角點檢測

Harris Corner Detector算法簡單如下:

第1步、當在X和Y方向(即梯度)上移動時,它確定哪些窗口(小圖像塊)產生非常大的強度變化。

第2步、找到每個這樣的窗口,計算得分R.

第3步、對此分數應用閾值後,將選擇並標記重要角點。

數學概述

第1步 :我們如何確定產生大變化的窗口?

讓窗口(中心)位於位置(x,y)。設這個位置的像素強度為I(x,y)。如果此窗口稍微移動到具有位移(u,v)的新位置,則該位置處的像素的強度將為I(x + u,y + v)。因此[I(x + u,y + v)-I(x,y)]將是窗口移位強度的差異。對於一個角點,這種差異將非常高。因此,我們通過相對於X和Y軸來區分它來最大化該項。設w(x,y)為窗口上的像素權重(矩形或高斯)然後,E(u,v)定義為:

Harris 角點檢測和Shi-Tomasi 角點檢測

加權和乘以窗口中所有像素的強度差

現在,通過上面的公式計算E(u,v)將非常非常慢。因此,我們使用泰勒級數展開(僅第一階)

Harris 角點檢測和Shi-Tomasi 角點檢測

第2步:現在我們知道如何找到具有大變化的窗口,我們如何選擇具有合適角落的窗口?矩陣的特徵值可用於實現此目的。因此,我們計算與每個這樣的窗口相關聯的分數。

Harris 角點檢測和Shi-Tomasi 角點檢測

用於分類為平坦區域/邊緣/角點的分數

Harris 角點檢測和Shi-Tomasi 角點檢測

步驟3:根據R的值,窗口被分類為由平面,邊緣或角點組成。R的較大值表示角,負值表示邊。此外,為了獲得最佳角點,我們可以使用非最大抑制。

(注意:Harris Detector不是尺度不變的)

代碼概述

Harris Corner檢測在OpenCV中實現。我們來看下面的Python代碼:

'''

Function : cv2.cornerHarris(image,blocksize,ksize,k)

Parameters are as follows :

1. image : the source image in which we wish to find the corners (grayscale)

2. blocksize : size of the neighborhood in which we compare the gradient

3. ksize : aperture parameter for the Sobel() Operator (used for finding Ix and Iy)

4. k : Harris detector free parameter (used in the calculation of R)

'''

def harris_corners(image):

#Converting the image to grayscale

gray_img = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

#Conversion to float is a prerequisite for the algorithm

gray_img = np.float32(gray_img)

# 3 is the size of the neighborhood considered, aperture parameter = 3

# k = 0.04 used to calculate the window score (R)

corners_img = cv2.cornerHarris(gray_img,3,3,0.04)

#Marking the corners in Green

image[corners_img>0.001*corners_img.max()] = [0,255,0]

return image

Shi-Tomasi角點探測器

除了計算得分(R)的方式之外,Shi-Tomasi幾乎與Harris 角點檢測類似。這樣可以獲得更好的結果。此外,在這種方法中,我們可以找到前N個角,這在我們不想檢測每個角的情況下可能是有用的。

數學概述

在Shi-Tomasi,R按以下方式計算:

Harris 角點檢測和Shi-Tomasi 角點檢測

如果R大於閾值,則將其歸類為拐角。

代碼概述

'''

Function: cv2.goodFeaturesToTrack(image,maxCorners, qualityLevel, minDistance[, corners[, mask[, blockSize[, useHarrisDetector[, k]]]]])

image – Input 8-bit or floating-point 32-bit, single-channel image.

maxCorners – You can specify the maximum no. of corners to be detected. (Strongest ones are returned if detected more than max.)

qualityLevel – Minimum accepted quality of image corners.

minDistance – Minimum possible Euclidean distance between the returned corners.

corners – Output vector of detected corners.

mask – Optional region of interest.

blockSize – Size of an average block for computing a derivative covariation matrix over each pixel neighborhood.

useHarrisDetector – Set this to True if you want to use Harris Detector with this function.

k – Free parameter of the Harris detector.

'''

def shi_tomasi(image):

#Converting to grayscale

gray_img = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

#Specifying maximum number of corners as 1000

# 0.01 is the minimum quality level below which the corners are rejected

# 10 is the minimum euclidean distance between two corners

corners_img = cv2.goodFeaturesToTrack(gray_img,1000,0.01,10)

corners_img = np.int0(corners_img)

for corners in corners_img:

x,y = corners.ravel()

#Circling the corners in green

cv2.circle(image,(x,y),3,[0,255,0],-1)

return image

該代碼可用於在圖像,圖像文件夾或現場網絡攝像頭中使用Harris和Shi-Tomasi檢測方法檢測角點。您還可以使用其他一些參數來獲得不同的輸出。

讓我們看一下這些代碼的實際應用。我們可以看到,Shi-Tomasi比Harris更好地檢測角點:

Harris 角點檢測和Shi-Tomasi 角點檢測

左上:Harris,右上:Shi-Tomasi,左下:原文

結論

綜上所述,Harris和Shi-Tomasi的角檢測方法是一些非常酷和容易的算法,可以使用intensity gradients的簡單概念來檢測這些角。shii - tomasi是一個稍微好一點的版本,只是改變了分數公式。我們檢測了幾個應用的角點:圖像對齊、圖像拼接(還記得你的手機攝像頭的全景圖嗎?)、對象識別、3D重建、運動跟蹤等等。


分享到:


相關文章: