欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

OpenCV角點(diǎn)檢測(cè)的實(shí)現(xiàn)示例

 更新時(shí)間:2022年03月28日 10:18:38   作者:山居秋暝LS  
角點(diǎn)通常被定義為兩條邊的交點(diǎn),本文主要介紹了OpenCV角點(diǎn)檢測(cè)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Harris 角點(diǎn)檢測(cè)算法

1. 角點(diǎn)

角點(diǎn)是水平方向、垂直方向變化都很大的像素。

角點(diǎn)檢測(cè)算法的基本思想: 

        使用一個(gè)固定窗口在圖像上進(jìn)行任意方向上的滑動(dòng),比較滑動(dòng)前與滑動(dòng)后兩種情況,窗口中的像素灰度變化程度,如果存在任意方向上的滑動(dòng),都有著較大灰度變化,那么我們可以認(rèn)為該窗口中存在角點(diǎn)。

        目前,角點(diǎn)檢測(cè)算法還不是十分完善,許多算法需要依賴大量的訓(xùn)練集和冗余數(shù)據(jù)來(lái)防止和減少錯(cuò)誤的特征的出現(xiàn)。對(duì)于角點(diǎn)檢測(cè)算法的重要評(píng)價(jià)標(biāo)準(zhǔn)是:其對(duì)多幅圖像中相同或者相似特征的檢測(cè)能力,并且能夠應(yīng)對(duì)光照變化、或者圖像旋轉(zhuǎn)等影響。

關(guān)于角點(diǎn)的具體描述可以有幾種:

  • 一階導(dǎo)數(shù)(即灰度的梯度)的局部最大所對(duì)應(yīng)的像素點(diǎn);
  • 兩條及兩條以上邊緣的交點(diǎn);
  • 圖像中梯度值和梯度方向的變化速率都很高的點(diǎn); 
  • 角點(diǎn)處的一階導(dǎo)數(shù)最大,二階導(dǎo)數(shù)為零,指示物體邊緣變化不連續(xù)的方向。                                                                                     

三類角點(diǎn)檢測(cè)算法:

  • 基于二值圖像的角點(diǎn)檢測(cè);
  • 基于輪廓曲線的角點(diǎn)檢測(cè);
  • 基于灰度圖像的角點(diǎn)檢測(cè):基于梯度、基于模板和基于模板和梯度組合三類方法;常見(jiàn)的基于模板的角點(diǎn)檢測(cè)算法有:Kitchen-Rosenfeld角點(diǎn)檢測(cè)算法,Harris角點(diǎn)檢測(cè)算法,KLT角點(diǎn)檢測(cè)算法及SUSAN角點(diǎn)檢測(cè)算法?;谀0宓姆椒ㄖ饕强紤]像素領(lǐng)域點(diǎn)灰度的變化,即亮度的變化。

2. 流程

(1)找出角點(diǎn)
用高斯算子求出像素水平方向和垂直方向的梯度dx, dy,–> 對(duì)梯度的平方dxdx ,dydy, dxdy濾波得到Wxx ,Wxy,Wyy --> 在求的(WxxWyy - Wxy**2)/(Wxx + Wyy)作為候選角點(diǎn)。

(2)篩選角點(diǎn)
根據(jù)閾值篩選角點(diǎn)–> 取得角點(diǎn)的坐標(biāo) -->根據(jù)角點(diǎn)坐標(biāo)得到角點(diǎn)所在的行 --> 在角點(diǎn)周圍,刪除掉其他角點(diǎn)。–> result

(3)標(biāo)記角點(diǎn)

3. 實(shí)現(xiàn)

from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import filters

## 1.找出角點(diǎn)
### 1.1 步驟:img求導(dǎo) --> imx ,imy 濾波-->Wxx ,Wxy,Wyy --> (Wxx*Wyy - Wxy**2)/(Wxx + Wyy)
def compute_harris_response(img,sigma=3):
? ? # 求梯度
? ? imgx,imgy ?= np.zeros(img.shape),np.zeros(img.shape)
? ? filters.gaussian_filter(img,(sigma,sigma),(0,1),imgx)
? ? filters.gaussian_filter(img,(sigma,sigma),(1,0),imgy) # [260,263]
? ? # 對(duì)梯度進(jìn)行高斯濾波
? ? wxx = filters.gaussian_filter(imgx**2,sigma)
? ? wyy = filters.gaussian_filter(imgy**2,sigma) # [260,263]
? ? wxy = filters.gaussian_filter(imgx*imgy,sigma)# [260,263]
? ? ## 求行列式和跡
? ? wdet = wxx*wyy -wxy**2
? ? wtr = wxx + wyy
? ? return ?wdet/wtr

## 2 篩選角點(diǎn)
### 2.1 步驟:根據(jù)閾值篩選角點(diǎn)--> 取得角點(diǎn)的坐標(biāo) -->根據(jù)角點(diǎn)坐標(biāo)得到角點(diǎn)所在的行 ?-->
# --> 在角點(diǎn)周圍,刪除掉其他角點(diǎn)
def get_harris_points(harri,min_dist=4,threshold=0.1):
? ? corner_thre = harri.max()*threshold ?# 角點(diǎn)閾值
? ? mask = (harri > corner_thre)*1 ?# 取出大于閾值的點(diǎn)為候選角點(diǎn)
? ? cords = np.array(mask.nonzero()).T ?# 取候選角點(diǎn)的坐標(biāo)

? ? values = [harri[i[0],i[1]] for i in cords] ?# 候選角點(diǎn)的值
? ? cls = np.argsort(values) ? ? # 對(duì)角點(diǎn)排序得到排序后的序列號(hào),序列號(hào)也是候選角點(diǎn)所在的行

? ? loc = np.zeros(harri.shape) ? # 劃出可行性區(qū)域
? ? loc[min_dist:-min_dist,min_dist:-min_dist] = 1

? ? re_cords = []
? ? for i in cls: ?# 篩選角點(diǎn)。先取出角點(diǎn),角點(diǎn)周圍的點(diǎn)不再取出
? ? ? ? if loc[cords[i,0],cords[i,1]] == 1 :
? ? ? ? ? ? re_cords.append(cords[i])
? ? ? ? ? ? loc[cords[i,0]-min_dist:cords[i,0]+min_dist,cords[i,1]-min_dist:cords[i,1]+min_dist]=0
? ? return re_cords

def plot_harri(img,cords):
? ? plt.figure()
? ? plt.gray()
? ? plt.imshow(img)
? ? plt.plot([i[1] for i in cords],[i[0] for i in cords],'.')
? ? plt.axis('off')
? ? plt.show()

## 3 測(cè)試
if __name__ == '__main__':
? ? img = np.array(Image.open('luna.png').convert('L'))
? ? harri = compute_harris_response(img)
? ? re_cords = get_harris_points(harri)
? ? plot_harri(img,re_cords)

到此這篇關(guān)于OpenCV角點(diǎn)檢測(cè)的文章就介紹到這了,更多相關(guān)OpenCV角點(diǎn)檢測(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論