目錄
- 前言
- 1. 效果圖
- 2. 原理
- 3. 源碼
- 3.1 Harris角點(diǎn)檢測
- 3.2 精細(xì)角點(diǎn)檢測
- 總結(jié)
前言
這篇博客將了解什么是特征,角點(diǎn),哈里斯角點(diǎn)檢測(Harris Corner Detection)的概念。并使用cv2.cornerHarris(),cv2.cornerSubPix()實(shí)現(xiàn)哈里斯角點(diǎn)檢測;
1. 效果圖
原圖 VS Harris角點(diǎn)檢測效果圖如下:
![](/d/20211017/d66d18c8534cc951555015990ed616ab.gif)
原圖 VS Harris角點(diǎn)檢測效果圖如下:
![](/d/20211017/7e7e06b41a15ce1d59df941f96f04613.gif)
驚細(xì)角點(diǎn)效果圖如下:Harris角點(diǎn)用紅色像素標(biāo)記,精細(xì)角點(diǎn)用綠色像素標(biāo)記
![](/d/20211017/5bbefb324dbb8bc3c176b9da26ac9033.gif)
驚細(xì)角點(diǎn)效果圖如下:Harris角點(diǎn)用紅色像素標(biāo)記,精細(xì)角點(diǎn)用綠色像素標(biāo)記
![](/d/20211017/e9318d338c4db8c815227feb848f6f5f.gif)
2. 原理
圖像最重要的一個(gè)要素是特征,一旦有了特征及其描述,就可以在所有圖像中找到相同的特征,并將它們對齊、縫合或執(zhí)行任何您想要的操作。
特征可分為角、邊、平面,OpenCV提供了許多不同的算法來查找特征、描述特征、匹配特征等。
角點(diǎn)是圖像中各個(gè)方向上強(qiáng)度變化較大的區(qū)域。
Harris角點(diǎn)檢測的結(jié)果是一個(gè)灰度圖像與這些分?jǐn)?shù)。對一個(gè)合適的圖像進(jìn)行閾值化可以得到圖像中的角點(diǎn)。
dst = cv2.cornerHarris(gray, 2, 3, 0.04)
img: 輸入圖像,灰度圖像,float32
blockSize: 用于角點(diǎn)檢測的鄰域的大小
ksize: Sobel導(dǎo)數(shù)的孔徑參數(shù)
k: 方程中的k-Harris檢測器自由參數(shù)
dst:返回值,灰度圖像
corners = cv2.cornerSubPix(gray, np.float32(centroids), (5, 5), (-1, -1), criteria)
具有亞像素精度的角點(diǎn):有時(shí)可能需要以最大的精度找到角點(diǎn)。OpenCV附帶了一個(gè)函數(shù)cv2.cornerSubPix(),它可以進(jìn)一步細(xì)化以亞像素精度檢測到的角點(diǎn)。
使用 Harris 角點(diǎn)檢測器檢查逆矩陣的相似性。它表示角點(diǎn)是更好的跟蹤點(diǎn)。
3. 源碼
3.1 Harris角點(diǎn)檢測
# Harris角點(diǎn)檢測
import cv2
import numpy as np
img = cv2.imread('images/polygon.jpg')
img = cv2.imread('images/opencv_logo.jpg')
print(img.shape)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("origin", img)
cv2.waitKey(0)
gray = np.float32(gray)
# res = cv2.cornerHarris(gray, 2, 3, 0.04)
# - img: 輸入圖像,灰度圖像,float32
# - blockSize: 用于角點(diǎn)檢測的鄰域的大小
# - ksize: Sobel導(dǎo)數(shù)的孔徑參數(shù)
# - k: 方程中的k-Harris檢測器自由參數(shù)
# - res:返回值,灰度圖像
res = cv2.cornerHarris(gray, 2, 3, 0.04)
# 擴(kuò)大標(biāo)記的內(nèi)容
res = cv2.dilate(res, None)
# 最佳閾值因圖而異
img[res > 0.01 * res.max()] = [0, 0, 255]
cv2.imshow('Harris res', img)
if cv2.waitKey(0) 0xff == 27:
cv2.destroyAllWindows()
3.2 精細(xì)角點(diǎn)檢測
# 具有亞像素精度的角點(diǎn)
# 有時(shí)可能需要以最大的精度找到角點(diǎn)。OpenCV附帶了一個(gè)函數(shù)cv2.cornerSubPix(),它可以進(jìn)一步細(xì)化以亞像素精度檢測到的角點(diǎn)。
import cv2
import imutils
import numpy as np
filename = 'images/polygon.jpg'
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 尋找Harris角點(diǎn)
gray = np.float32(gray)
dst = cv2.cornerHarris(gray, 2, 3, 0.04)
dst = cv2.dilate(dst, None)
ret, dst = cv2.threshold(dst, 0.01 * dst.max(), 255, 0)
dst = np.uint8(dst)
# 尋找中心點(diǎn)
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
# 定義停止和細(xì)化角點(diǎn)的條件
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
corners = cv2.cornerSubPix(gray, np.float32(centroids), (5, 5), (-1, -1), criteria)
# 繪制角點(diǎn)和細(xì)化的亞像素點(diǎn)
res = np.hstack((centroids, corners))
res = np.int0(res)
# Harris角點(diǎn)用紅色像素標(biāo)記,精細(xì)角點(diǎn)用綠色像素標(biāo)記
img[res[:, 1], res[:, 0]] = [0, 0, 255]
img[res[:, 3], res[:, 2]] = [0, 255, 0]
gray = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
cv2.imshow("gray", img)
gray[res[:, 1], res[:, 0]] = [0, 0, 255]
gray[res[:, 3], res[:, 2]] = [0, 255, 0]
cv2.imshow('cornerSubPix res', imutils.resize(img, width=600))
cv2.waitKey(0)
參考 https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_features_harris/py_features_harris.html#harris-corners
總結(jié)
到此這篇關(guān)于OpenCV特征提取與檢測之Harris角點(diǎn)檢測的文章就介紹到這了,更多相關(guān)OpenCV Harris角點(diǎn)檢測內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Python使用Opencv實(shí)現(xiàn)圖像特征檢測與匹配的方法
- opencv2基于SURF特征提取實(shí)現(xiàn)兩張圖像拼接融合
- Android基于OpenCV實(shí)現(xiàn)Harris角點(diǎn)檢測
- OpenCV哈里斯(Harris)角點(diǎn)檢測的實(shí)現(xiàn)
- Python中OpenCV圖像特征和harris角點(diǎn)檢測