圖像處理下--S1Harris檢測器

角點介紹
Harris檢測器
代碼實現

# 導入庫
import cv2 as cv
import matplotlib.pyplot as plt # 或from matplotlib import pyplot as plt 
import numpy as np

# %% 參數設置
# detector parameters
blockSize=3 # 用於角點檢測的領域大小即窗口尺寸
Ksize=3 # 用於計算梯度圖的sobel算子的尺寸
k=0.06

image=cv.imread('Scenery.jpg')

# %% 圖像參數
print(image.shape)
height=image.shape[0]
width=image.shape[1]
channels=image.shape[2]
print('width: %s  height: %s channels: %s',width,height,channels)

gray_img=cv.cvtColor(image,cv.COLOR_BGR2GRAY) # 彩色圖像灰度值化

# %% 
# modify the data type setting to 32-bit floating point 
gray_img=np.float32(gray_img)

# detect the corners with appropriate values as input parameters
corners_img=cv.cornerHarris(gray_img,blockSize,Ksize,k)

# result is dilated for marking the corners, not necessary
kernel = cv.getStructuringElement(cv.MORPH_RECT,(3, 3))
dst = cv.dilate(corners_img, kernel)

# Threshold for an optimal value, marking the corners in Green
#image[corners_img>0.01*corners_img.max()] = [0,0,255]

for r in range(height):
        for c in range(width):
            pix=dst[r,c]
            if pix>0.05*dst.max():
               cv.circle(image,(c,r),5,(0,0,255),0)

image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
plt.imshow(image)
plt.show()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章