OpenCV特征提取與檢測之Harris角點(diǎn)檢測
這篇博客將了解什么是特征,角點(diǎn),哈里斯角點(diǎn)檢測(Harris Corner Detection)的概念。并使用cv2.cornerHarris(),cv2.cornerSubPix()實(shí)現(xiàn)哈里斯角點(diǎn)檢測;
1. 效果圖
原圖 VS Harris角點(diǎn)檢測效果圖如下:
原圖 VS Harris角點(diǎn)檢測效果圖如下:
驚細(xì)角點(diǎn)效果圖如下:Harris角點(diǎn)用紅色像素標(biāo)記,精細(xì)角點(diǎn)用綠色像素標(biāo)記
驚細(xì)角點(diǎn)效果圖如下:Harris角點(diǎn)用紅色像素標(biāo)記,精細(xì)角點(diǎn)用綠色像素標(biāo)記
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)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。