角點檢測顧名思義,就是對類似頂點的檢測,與邊緣有所區別
邊緣可能在某一方向上變化不是特別明顯,但角點在任何方向上變換都很明顯
cv2.cornerHarris(img,blockSize,ksize,k)
cv2.cornerHarris(gray,2,3,0.04)
參數一:img,類型為float32的圖像
參數二:blockSize 角點檢測中指定區域的大小
參數三:ksize Sobel算子求導中使用的窗口大小,一般情況設置為3
參數四:k 取值參數為[0.04,0.06],opencv推薦使用0.04
import cv2
import numpy as np
from matplotlib import pyplot as pltdef show_photo(name,picture):#圖像顯示函數cv2.imshow(name,picture)cv2.waitKey(0)cv2.destroyAllWindows()img = cv2.imread('E:\Jupyter_workspace\study\data/cfx.png')
print('img.shape:',img.shape)#結果為:img.shape: (151, 269, 3)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#將彩色圖轉換為灰度圖
#gray = np.float32(gray)dst = cv2.cornerHarris(gray,2,3,0.04)
print("dst.shape:",dst.shape)#結果為:dst.shape: (151, 269)img[dst>0.01*dst.max()] = [0,0,255]#(B,G,R)以紅色點描繪顯示
show_photo('dst',img)
原圖:
角點檢測結果: