特征向量(Feature Vectors)

角點(diǎn)與目標(biāo)檢測(cè)(Corners and Object Detection)

已經(jīng)可以從圖像中提取基于形狀的特征薯蝎,如何使用這一組特征來(lái)檢測(cè)整個(gè)對(duì)象,以山峰圖像角點(diǎn)檢測(cè)舉例:



假設(shè)想要在其他圖像中檢測(cè)這座山的方法,單個(gè)角點(diǎn)不足以在任何其他圖像中識(shí)別這座山峰楷拳,但是狂芋,我們可以定義一組這座山峰的形狀特征,將它們組合后蹭一個(gè)數(shù)組或者向量森篷,然后使用這組特征來(lái)查UN構(gòu)造一個(gè)山峰檢測(cè)器。


以這個(gè)梯形及其相應(yīng)的梯度為例 這個(gè)圖像已經(jīng)檢測(cè)好了邊緣豺型,以梯形中心為基準(zhǔn) 觀察每個(gè)單元的梯度方向仲智,我們就可以將這個(gè)數(shù)據(jù)平面化 創(chuàng)建出一個(gè)一維數(shù)組,這就是特征向量姻氨;同樣的操作可應(yīng)用于其它形狀 如這個(gè)圓圈钓辆,取梯度計(jì)算網(wǎng)格每個(gè)單元的梯度方向。我們就得到了這兩種形狀的特征向量。

實(shí)時(shí)特征檢測(cè)-ORB(Oriented Fast and Rotated Brief)

  1. 首先在圖像中找到稱為關(guān)鍵點(diǎn)的特殊區(qū)域岩馍,可以將關(guān)鍵點(diǎn)視為圖像中的一個(gè)容易辨識(shí)小區(qū)域碉咆,例如角點(diǎn)。
    以貓的圖像舉例:


  2. orb為每個(gè)關(guān)鍵點(diǎn)計(jì)算相應(yīng)的特征向量蛀恩,orb算法創(chuàng)建僅包含1和0的特征向量疫铜,因此也被稱為二元特征向量

ORB,不僅速度快双谆,不受噪聲照明壳咕、和圖像變換,如旋轉(zhuǎn)的影響

FAST 特征提取

ORB特征檢測(cè)第一步第一步是找到圖像中的關(guān)鍵點(diǎn)顽馋,這是由FAST算法完成的谓厘。

  1. 粗提取


從圖像選取一個(gè)像素P,判斷P是不是特征點(diǎn)的方法:以P為圓心,畫(huà)一個(gè)半徑為3pixel的圓寸谜,如果圓周上有連續(xù)n個(gè)像素點(diǎn)灰度值比p點(diǎn)大或者小竟稳,則認(rèn)為p為特征點(diǎn).一般n設(shè)置12或者8。FAST之所以可以這么快熊痴,因?yàn)樗紫葯z測(cè)的1他爸、5、9果善、13位置上的灰度值诊笤,如果p是特征點(diǎn),那么這四個(gè)位置至少有3個(gè)或者3個(gè)以上的像素都大于或者小于P點(diǎn)的灰度值巾陕。如果不滿足讨跟,則直接排除該點(diǎn)。

FAST發(fā)現(xiàn)的關(guān)鍵點(diǎn)提供有關(guān)圖像中定義邊緣對(duì)象的信息鄙煤,但是這些關(guān)鍵點(diǎn)只給出了一個(gè)邊緣位置晾匠,并不包括任何有關(guān)強(qiáng)度變化方向的信息

Brief 特征描述

Brief將關(guān)鍵點(diǎn)轉(zhuǎn)化為二進(jìn)制特征向量。BRIEF stands for binary robust independent elementary features馆类。
簡(jiǎn)而言之混聊,每個(gè)關(guān)鍵點(diǎn)由具有的二進(jìn)制特征向量描述弹谁,一個(gè)128-512位的字符串乾巧,只包含1和0。每一位只能容納一位二進(jìn)制预愤。
Brief怎么為關(guān)鍵點(diǎn)生成二進(jìn)制描述:

  1. 首先BRIEF使用高斯內(nèi)核平滑給定圖像沟于,防止對(duì)高頻噪聲過(guò)于敏感。
  2. 然后植康,選取一個(gè)關(guān)鍵點(diǎn)旷太,在關(guān)鍵點(diǎn)附近定義的鄰域內(nèi)選擇一對(duì)隨機(jī)像素,關(guān)鍵點(diǎn)周圍的鄰域稱為補(bǔ)丁,它是具有一定像素寬度和高度的正方形供璧。

隨機(jī)對(duì)中的第一個(gè)像素存崖,這里顯示的是藍(lán)色正方形,是從以關(guān)鍵點(diǎn)為中心的高斯分布繪制的睡毒,具有標(biāo)準(zhǔn)偏差或Sigma的擴(kuò)展来惧。隨機(jī)對(duì)中的第二個(gè)像素,這里顯示為黃色正方形演顾,是從以該第一像素為中心的高斯分布繪制的供搀,標(biāo)準(zhǔn)偏差Sigma大于2。經(jīng)驗(yàn)表明钠至,這種高斯選擇提高了特征匹配率葛虐。
然后,BRIEF通過(guò)如下比較兩個(gè)像素的亮度開(kāi)始為關(guān)鍵點(diǎn)構(gòu)造二進(jìn)制描述符:如果第一像素比第二像素亮棉钧,則它將數(shù)值1分配給描述符中的對(duì)應(yīng)位屿脐,否則它分配的值為零。


在這個(gè)例子中宪卿,我們看到摄悯,第二個(gè)像素比第一個(gè)像素亮,所以我們給特征向量的第一個(gè)比特位賦值為0愧捕。特征向量的第一位對(duì)應(yīng)于該關(guān)鍵點(diǎn)的第一對(duì)隨機(jī)點(diǎn)奢驯。
現(xiàn)在,對(duì)于相同的關(guān)鍵點(diǎn)次绘,BRIEF選擇一個(gè)新的隨機(jī)像素對(duì)瘪阁,比較它們的亮度并將一或零分配給下一個(gè)比特和特征向量。在我們的例子中邮偎,我們看到現(xiàn)在第一個(gè)像素比第二個(gè)更亮管跺,因此我們?cè)谔卣飨蛄恐袨榈诙环峙湟弧?duì)于256位向量禾进,BRIEF然后在移動(dòng)到下一個(gè)關(guān)鍵點(diǎn)之前對(duì)相同的關(guān)鍵點(diǎn)重復(fù)此過(guò)程256次豁跑。然后將256像素亮度比較的結(jié)果放入該一個(gè)關(guān)鍵點(diǎn)的二進(jìn)制特征向量中。

ORB 比例和旋轉(zhuǎn)不變性

ORB使用FAST檢測(cè)圖像中的關(guān)鍵點(diǎn)泻云,它需要做幾個(gè)額外的步驟確保他可以檢測(cè)到物體艇拍,無(wú)論它們的大小或圖像中的位置。


  1. ORB算法從構(gòu)建圖像金字塔開(kāi)始
    圖像金字塔是單個(gè)圖像的多尺度表示宠纯,其由一系列圖像組成卸夕,所有圖像都是不同分辨率的原始圖像的版本。
    金字塔中的每個(gè)級(jí)別都包含上一級(jí)圖像的下采樣版本婆瓜。下采樣意味著圖像分辨率已降低快集。
    在該示例中贡羔,圖像被下采樣兩倍,因此最初為四乘四平方區(qū)域的部分現(xiàn)在是兩乘二的方形區(qū)域个初。
    圖像的下采樣版本包含像素減少乖寒,尺寸縮小了兩倍。在這里院溺,我們看到一個(gè)五層圖像金字塔的例子宵统。在每個(gè)級(jí)別,圖像被下采樣兩倍覆获,到第四級(jí)马澈,我們有一個(gè)圖像十六分之一原始圖像的分辨率。
    一旦ORB創(chuàng)建了圖像金字塔弄息,它使用快速算法在每個(gè)級(jí)別快速定位不同大小的圖像中的關(guān)鍵點(diǎn)痊班。由于金字塔的每個(gè)級(jí)別由原始圖像的較小版本組成,因此原始圖像中的任何對(duì)象也將在金字塔的每個(gè)級(jí)別上減小尺寸摹量。因此涤伐,通過(guò)在每個(gè)級(jí)別定位關(guān)鍵點(diǎn),ORB有效地定位不同比例的對(duì)象的關(guān)鍵點(diǎn)缨称。 以這種方式凝果,ORB部分尺度不變的。這非常重要睦尽,因?yàn)閷?duì)象不太可能在每個(gè)圖像中以完全相同的尺寸出現(xiàn)器净。
    所以,ORB具有與該圖像金字塔的每個(gè)級(jí)別相關(guān)聯(lián)的關(guān)鍵點(diǎn)当凡。
  2. 在金字塔的所有層面的關(guān)鍵點(diǎn)都被找到后ORB現(xiàn)在為每個(gè)關(guān)鍵點(diǎn)指定方向山害。像左或右面對(duì),取決于強(qiáng)度水平如何圍繞該關(guān)鍵點(diǎn)變化沿量。ORB將首先選擇金字塔零級(jí)的圖像浪慌。對(duì)于此圖像,它現(xiàn)在將計(jì)算其關(guān)鍵點(diǎn)的方向朴则。首先計(jì)算以關(guān)鍵點(diǎn)為中心的盒子內(nèi)的強(qiáng)度質(zhì)心权纤,強(qiáng)度質(zhì)心可以被認(rèn)為是給定貼片中的平均像素強(qiáng)度的位置。一旦計(jì)算出強(qiáng)度質(zhì)心乌妒,就可以通過(guò)將關(guān)鍵點(diǎn)的矢量繪制到強(qiáng)度質(zhì)心來(lái)獲得關(guān)鍵點(diǎn)的方向汹想,如此處所示。

    該特定關(guān)鍵點(diǎn)的方向向下并朝向左側(cè)芥被,因?yàn)閳D像的該區(qū)域中的亮度在該方向上增加欧宜。一旦為金字塔零級(jí)圖像中的每個(gè)關(guān)鍵點(diǎn)指定了方向,ORB現(xiàn)在對(duì)所有其他金字塔等級(jí)的圖像重復(fù)相同的處理拴魄。值得注意的是,在圖像金字塔的每個(gè)級(jí)別上,補(bǔ)丁大小的大小都沒(méi)有減小匹中。因此夏漱,在金字塔的每個(gè)級(jí)別由相同補(bǔ)丁覆蓋的圖像區(qū)域?qū)⒏蟆?/p>

在此圖像中,圓圈表示每個(gè)關(guān)鍵點(diǎn)的大小顶捷。在金字塔的較高級(jí)別中找到具有較大尺寸的關(guān)鍵點(diǎn)挂绰。

  1. 找到并指定關(guān)鍵點(diǎn)的方向后,ORB現(xiàn)在使用修改后的brief來(lái)創(chuàng)建特征向量服赎,稱為rBRIEF葵蒂。無(wú)論對(duì)象的方向如何,都可以為關(guān)鍵點(diǎn)創(chuàng)建相同的矢量重虑。這使ORB算法旋轉(zhuǎn)不變践付,這意味著它可以檢測(cè)到以任何角度旋轉(zhuǎn)的圖像中的相同關(guān)鍵點(diǎn)。RBRIEF以與Brief相同的方式開(kāi)始缺厉,通過(guò)在給定關(guān)鍵點(diǎn)周圍的定義補(bǔ)丁內(nèi)選擇256個(gè)隨機(jī)像素對(duì)來(lái)構(gòu)造256位向量永高。然后它按照關(guān)鍵點(diǎn)的方向角,旋轉(zhuǎn)這些隨機(jī)像素對(duì)提针,以便將隨機(jī)點(diǎn)與關(guān)鍵點(diǎn)的方向?qū)R命爬。最后,rBRIEF比較隨機(jī)像素對(duì)的亮度辐脖,并相應(yīng)地分配1和0來(lái)創(chuàng)建相應(yīng)的特征向量饲宛。圖像中找到的所有關(guān)鍵點(diǎn)的所有特征向量的集合,被稱為ORB描述符

首先嗜价,我們將讀入圖像落萎,然后構(gòu)建并顯示圖像金字塔的幾個(gè)層

import numpy as np
import matplotlib.pyplot as plt
import cv2

%matplotlib inline

# Read in the image
image = cv2.imread('images/rainbow_flag.jpg')

# Change color to RGB (from BGR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

plt.imshow(image)

level_1 = cv2.pyrDown(image)
level_2 = cv2.pyrDown(level_1)
level_3 = cv2.pyrDown(level_2)

# Display the images
f, (ax1,ax2,ax3,ax4) = plt.subplots(1, 4, figsize=(20,10))

ax1.set_title('original')
ax1.imshow(image)

ax2.imshow(level_1)
ax2.set_xlim([0, image.shape[1]])
ax2.set_ylim([image.shape[0], 0])

ax3.imshow(level_2)
ax3.set_xlim([0, image.shape[1]])
ax3.set_ylim([image.shape[0], 0])

ax4.imshow(level_3)
ax4.set_xlim([0, image.shape[1]])
ax4.set_ylim([image.shape[0], 0])

視頻中ORB

ORB常用的一種, 是跟蹤和識(shí)別實(shí)時(shí)視頻流中的對(duì)象炭剪。 然后练链,對(duì)于傳入視頻流中的每個(gè)幀,我們計(jì)算ORB描述符奴拦,并使用匹配函數(shù)進(jìn)行比較媒鼓。 如果我們發(fā)現(xiàn)匹配函數(shù)返回超過(guò)某個(gè)匹配閾值的匹配數(shù),我們就可以斷定對(duì)象在幀中错妖。網(wǎng)格閾值是一個(gè)可以設(shè)置的自由參數(shù)绿鸣。例如,如果特定對(duì)象的ORB描述符具有100個(gè)關(guān)鍵點(diǎn)暂氯,則可以將閾值設(shè)置為該特定對(duì)象的關(guān)鍵點(diǎn)數(shù)量的35%潮模,50%或90%。如果將閾值設(shè)置為35%痴施,則意味著描述該對(duì)象的100個(gè)關(guān)鍵點(diǎn)中至少有35個(gè)關(guān)鍵點(diǎn)必須匹配擎厢,以便說(shuō)明該對(duì)象在幀中究流。所有這些步驟都可以在近乎實(shí)時(shí)的情況下完成,因?yàn)镺RBS二進(jìn)制描述符动遭, 計(jì)算和比較非撤姨剑快。 當(dāng)您想要檢測(cè)具有許多不受圖像背景影響的一致特征的對(duì)象時(shí)厘惦,ORB算法工作得最好偷仿。例如,ORB可以很好地用于面部檢測(cè)宵蕉,因?yàn)槟槻坑泻芏嗵卣髟途玻热缪劬妥旖?無(wú)論人身在何處,這種情況都不會(huì)改變羡玛。

Oriented FAST and Rotated BRIEF (ORB)
  1. Loading Images and Importing Resources
    cv2.imread()函數(shù)將圖像加載為BGR别智,我們將圖像轉(zhuǎn)換為RGB,將BGR圖像轉(zhuǎn)換為灰度進(jìn)行分析缝左。
%matplotlib inline

import cv2
import matplotlib.pyplot as plt

# Set the default figure size
plt.rcParams['figure.figsize'] = [20,10]

# Load the training image
image = cv2.imread('./images/face.jpeg')

# Convert the training image to RGB
training_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Convert the training image to gray Scale
training_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Display the images
plt.subplot(121)
plt.title('Original Training Image')
plt.imshow(training_image)
plt.subplot(122)
plt.title('Gray Scale Training Image')
plt.imshow(training_gray, cmap = 'gray')
plt.show()
  1. 定位關(guān)鍵點(diǎn)
    ORB算法的第一步是定位訓(xùn)練圖像中的所有關(guān)鍵點(diǎn)亿遂。 找到關(guān)鍵點(diǎn)后,ORB會(huì)創(chuàng)建相應(yīng)的二進(jìn)制特征向量渺杉,并在ORB描述符中將它們組合在一起蛇数。使用OpenCV的ORB類來(lái)定位關(guān)鍵點(diǎn)并創(chuàng)建相應(yīng)的ORB描述符。 使用ORB_create()函數(shù)設(shè)置ORB算法的參數(shù)是越。 ORB_create()函數(shù)的參數(shù)及其默認(rèn)值如下:
    cv2.ORB_create(nfeatures = 500, scaleFactor = 1.2, nlevels = 8, edgeThreshold = 31, firstLevel = 0, WTA_K = 2, scoreType = HARRIS_SCORE, patchSize = 31, fastThreshold = 20)
    參數(shù):
  • nfeatures - int
    確定要定位的最大特征數(shù)(關(guān)鍵點(diǎn))耳舅。
  • scaleFactor - float
    金字塔抽取率必須大于1. ORB使用圖像金字塔來(lái)查找要素,因此必須提供金字塔中每個(gè)圖層與金字塔所具有的級(jí)別數(shù)之間的比例因子倚评。 scaleFactor = 2表示經(jīng)典金字塔浦徊,其中每個(gè)下一級(jí)的像素比前一級(jí)少4倍。大比例因子將減少發(fā)現(xiàn)特征數(shù)量天梧。
  • nlevels - int
    金字塔等級(jí)的數(shù)量盔性,最小級(jí)別的線性大小等于input_image_linear_size / pow(scaleFactor,nlevels)呢岗。
  • edgeThreshold - - int
    未檢測(cè)到要素的邊框大小冕香。 由于關(guān)鍵點(diǎn)具有特定的像素大小,因此必須從搜索中排除圖像的邊緣后豫。 邊緣閾值的大小應(yīng)等于或大于patchSize參數(shù)悉尾。
  • firstLevel - int
    此參數(shù)允許您確定應(yīng)將哪個(gè)級(jí)別視為金字塔中的第一級(jí)別。 它在當(dāng)前實(shí)現(xiàn)中應(yīng)為0挫酿。 通常构眯,具有統(tǒng)一標(biāo)度的金字塔等級(jí)被認(rèn)為是第一級(jí)。
  • WTA_K - int
    用于生成定向的BRIEF描述符的每個(gè)元素的隨機(jī)像素的數(shù)量早龟。 可能的值為2,3和4惫霸,其中2為默認(rèn)值猫缭。 例如,值3意味著一次選擇三個(gè)隨機(jī)像素來(lái)比較它們的亮度它褪。 返回最亮像素的索引饵骨。 由于有3個(gè)像素翘悉,因此返回的索引將為0,1或2茫打。
  • scoreType - int
    此參數(shù)可以設(shè)置為HARRIS_SCORE或FAST_SCORE。 默認(rèn)的HARRIS_SCORE表示Harris角算法用于對(duì)要素進(jìn)行排名妖混。 該分?jǐn)?shù)僅用于保留最佳功能老赤。 FAST_SCORE生成的關(guān)鍵點(diǎn)稍差,但計(jì)算起來(lái)要快一些制市。
  • patchSize - int
    面向BRIEF描述符使用的補(bǔ)丁的大小抬旺。 當(dāng)然,在較小的金字塔層上祥楣,由特征覆蓋的感知圖像區(qū)域?qū)⒏蟆?br> 我們可以看到开财,cv2。 ORB_create()函數(shù)支持各種參數(shù)误褪。 前兩個(gè)參數(shù)(nfeatures和scaleFactor)可能是您最有可能改變的參數(shù)责鳍。 其他參數(shù)可以安全地保留其默認(rèn)值,您將獲得良好的結(jié)果兽间。

在下面的代碼中历葛,我們將使用ORB_create()函數(shù)將我們想要檢測(cè)的關(guān)鍵點(diǎn)的最大數(shù)量設(shè)置為200,并將金字塔抽取率設(shè)置為2.1嘀略。 然后恤溶,我們將使用.detectAndCompute(image)方法定位給定訓(xùn)練圖像中的關(guān)鍵點(diǎn)并計(jì)算其對(duì)應(yīng)的ORB描述符。 最后帜羊,我們將使用cv2.drawKeypoints()函數(shù)來(lái)可視化ORB算法找到的關(guān)鍵點(diǎn)咒程。

# Import copy to make copies of the training image
import copy

# Set the default figure size
plt.rcParams['figure.figsize'] = [14.0, 7.0]

# Set the parameters of the ORB algorithm by specifying the maximum number of keypoints to locate and
# the pyramid decimation ratio
orb = cv2.ORB_create(200, 2.0)

# Find the keypoints in the gray scale training image and compute their ORB descriptor.
# The None parameter is needed to indicate that we are not using a mask.
keypoints, descriptor = orb.detectAndCompute(training_gray, None)

# Create copies of the training image to draw our keypoints on
keyp_without_size = copy.copy(training_image)
keyp_with_size = copy.copy(training_image)

# Draw the keypoints without size or orientation on one copy of the training image 
cv2.drawKeypoints(training_image, keypoints, keyp_without_size, color = (0, 255, 0))

# Draw the keypoints with size and orientation on the other copy of the training image
cv2.drawKeypoints(training_image, keypoints, keyp_with_size, flags = cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Display the image with the keypoints without size or orientation
plt.subplot(121)
plt.title('Keypoints Without Size or Orientation')
plt.imshow(keyp_without_size)

# Display the image with the keypoints with size and orientation
plt.subplot(122)
plt.title('Keypoints With Size and Orientation')
plt.imshow(keyp_with_size)
plt.show()

# Print the number of keypoints detected
print("\nNumber of keypoints Detected: ", len(keypoints))

正如我們?cè)谟覉D所示,每個(gè)關(guān)鍵點(diǎn)都有一個(gè)中心讼育,一個(gè)大小和一個(gè)角度帐姻。 中心確定圖像中每個(gè)關(guān)鍵點(diǎn)的位置; 每個(gè)關(guān)鍵點(diǎn)的大小由BRIEF用來(lái)創(chuàng)建其特征向量的補(bǔ)丁大小決定; 角度告訴我們由rBRIEF確定的關(guān)鍵點(diǎn)的方向。

特征匹配

一旦我們獲得了訓(xùn)練和查詢圖像的ORB描述符窥淆,最后一步就是使用相應(yīng)的ORB描述符在兩個(gè)圖像之間進(jìn)行關(guān)鍵點(diǎn)匹配卖宠。 這種匹配通常由匹配函數(shù)執(zhí)行。 最常用的匹配函數(shù)之一稱為Brute-Force忧饭。
在下面的代碼中扛伍,我們將使用OpenCV的BFMatcher類來(lái)比較訓(xùn)練和查詢圖像中的關(guān)鍵點(diǎn)。使用cv2.BFMatcher()函數(shù)設(shè)置Brute-Force匹配器的參數(shù)词裤。 cv2.BFMatcher()函數(shù)的參數(shù)及其默認(rèn)值如下:

  • normType
    指定用于確定匹配質(zhì)量的度量刺洒。默認(rèn)情況下鳖宾,NoMyType=CV2..NoqILL2,它測(cè)量?jī)蓚€(gè)描述符之間的距離逆航。然而鼎文,對(duì)于二進(jìn)制描述符,如由ORB創(chuàng)建的因俐,漢明度量(Hamming metric)更適合拇惋。漢明度量通過(guò)計(jì)算二進(jìn)制描述符之間的不同位的數(shù)目來(lái)確定距離。當(dāng)使用WTAYK=2創(chuàng)建ORB描述符時(shí)抹剩,選擇兩個(gè)隨機(jī)像素并在亮度上進(jìn)行比較撑帖。最亮像素的索引以0或1返回。這樣的輸出只占用1位澳眷,因此應(yīng)該使用CV2..NojyHaMin度量胡嘿。另一方面,如果使用WTA_K=3創(chuàng)建ORB描述符钳踊,則選擇三個(gè)隨機(jī)像素并在亮度方面進(jìn)行比較衷敌。最亮像素的索引以0, 1或2的形式返回。這種輸出將占用2比特拓瞪,因此應(yīng)該使用漢明距離的特殊變體缴罗,稱為cv2.NORM_HAMMING2(2代表2比特)。然后吴藻,對(duì)于所選擇的任何度量瞒爬,當(dāng)比較訓(xùn)練和查詢圖像中的關(guān)鍵點(diǎn)時(shí),認(rèn)為度量較泄当ぁ(它們之間的距離)的對(duì)是最佳匹配侧但。
  • crossCheck - bool
    一個(gè)布爾變量,可以設(shè)置為“True”或“False”航罗。 交叉檢查對(duì)于消除錯(cuò)誤匹配非常有用禀横。 交叉檢查通過(guò)執(zhí)行兩次匹配過(guò)程來(lái)工作。 第一次將訓(xùn)練圖像中的關(guān)鍵點(diǎn)與查詢圖像中的關(guān)鍵點(diǎn)進(jìn)行比較; 然而粥血,第二次柏锄,將查詢圖像中的關(guān)鍵點(diǎn)與訓(xùn)練圖像中的關(guān)鍵點(diǎn)進(jìn)行比較(比較向后進(jìn)行)。 啟用交叉檢查時(shí)复亏,只有當(dāng)訓(xùn)練圖像中的關(guān)鍵點(diǎn)A與查詢圖像中的關(guān)鍵點(diǎn)B最佳匹配時(shí)趾娃,匹配才被視為有效,反之亦然(即缔御,如果查詢中的關(guān)鍵點(diǎn)B 圖像是訓(xùn)練圖像中點(diǎn)A的最佳匹配抬闷。

一旦設(shè)置了* BFMatcher *的參數(shù),我們就可以使用.match(descriptors_train,descriptors_query)方法笤成,使用它們的ORB描述符找到訓(xùn)練圖像和查詢圖像之間的匹配關(guān)鍵點(diǎn)评架。 最后,我們將使用cv2.drawMatches()函數(shù)來(lái)可視化Brute-Force匹配器找到的匹配關(guān)鍵點(diǎn)炕泳。 此函數(shù)水平堆疊訓(xùn)練和查詢圖像纵诞,并將訓(xùn)練圖像中關(guān)鍵點(diǎn)的線條繪制到查詢圖像中對(duì)應(yīng)的最佳匹配關(guān)鍵點(diǎn)。 請(qǐng)記住培遵,為了更清楚地查看ORB算法的屬性浙芙,在以下示例中,我們將使用與我們的訓(xùn)練和查詢圖像相同的圖像荤懂。

import cv2
import matplotlib.pyplot as plt

# Set the default figure size
plt.rcParams['figure.figsize'] = [14.0, 7.0]

# Load the training image
image1 = cv2.imread('./images/face.jpeg')

# Load the query image
image2 = cv2.imread('./images/face.jpeg')

# Convert the training image to RGB
training_image = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)

# Convert the query image to RGB
query_image = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)

# Display the training and query images
plt.subplot(121)
plt.title('Training Image')
plt.imshow(training_image)
plt.subplot(122)
plt.title('Query Image')
plt.imshow(query_image)
plt.show()

# Convert the training image to gray scale
training_gray = cv2.cvtColor(training_image, cv2.COLOR_BGR2GRAY)

# Convert the query image to gray scale
query_gray = cv2.cvtColor(query_image, cv2.COLOR_BGR2GRAY)

# Set the parameters of the ORB algorithm by specifying the maximum number of keypoints to locate and
# the pyramid decimation ratio
orb = cv2.ORB_create(1000, 2.0)

# Find the keypoints in the gray scale training and query images and compute their ORB descriptor.
# The None parameter is needed to indicate that we are not using a mask in either case.
keypoints_train, descriptors_train = orb.detectAndCompute(training_gray, None)
keypoints_query, descriptors_query = orb.detectAndCompute(query_gray, None)

# Create a Brute Force Matcher object. Set crossCheck to True so that the BFMatcher will only return consistent
# pairs. Such technique usually produces best results with minimal number of outliers when there are enough matches.
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck = True)

# Perform the matching between the ORB descriptors of the training image and the query image
matches = bf.match(descriptors_train, descriptors_query)

# The matches with shorter distance are the ones we want. So, we sort the matches according to distance
matches = sorted(matches, key = lambda x : x.distance)

# Connect the keypoints in the training image with their best matching keypoints in the query image.
# The best matches correspond to the first elements in the sorted matches list, since they are the ones
# with the shorter distance. We draw the first 300 mathces and use flags = 2 to plot the matching keypoints
# without size or orientation.
result = cv2.drawMatches(training_gray, keypoints_train, query_gray, keypoints_query, matches[:300], query_gray, flags = 2)

# Display the best matching points
plt.title('Best Matching Points')
plt.imshow(result)
plt.show()

# Print the number of keypoints detected in the training image
print("Number of Keypoints Detected In The Training Image: ", len(keypoints_train))

# Print the number of keypoints detected in the query image
print("Number of Keypoints Detected In The Query Image: ", len(keypoints_query))

# Print total number of matching points between the training and query images
print("\nNumber of Matching Keypoints Between The Training and Query Images: ", len(matches))

ORB 主要屬性

  • 比例不變性
  • 旋轉(zhuǎn)不變性
  • 照明不變性
  • 噪音不變性

Scale Invariance

ORB算法是尺度不變的茁裙。 這意味著它能夠檢測(cè)圖像中的對(duì)象塘砸,無(wú)論其大小如何节仿。 為了看到這一點(diǎn),我們現(xiàn)在將使用我們的蠻力匹配器來(lái)匹配訓(xùn)練圖像和原始訓(xùn)練圖像大小的1/4的查詢圖像之間的點(diǎn)掉蔬。

import cv2
import matplotlib.pyplot as plt

# Set the default figure size
plt.rcParams['figure.figsize'] = [14.0, 7.0]

# Load the training image
image1 = cv2.imread('./images/face.jpeg')

# Load the query image
image2 = cv2.imread('./images/faceQS.png')

# Convert the training image to RGB
training_image = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)

# Convert the query image to RGB
query_image = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)

# Display the images
plt.subplot(121)
plt.title('Training Image')
plt.imshow(training_image)
plt.subplot(122)
plt.title('Query Image')
plt.imshow(query_image)
plt.show()

# Convert the training image to gray scale
training_gray = cv2.cvtColor(training_image, cv2.COLOR_BGR2GRAY)

# Convert the query image to gray scale
query_gray = cv2.cvtColor(query_image, cv2.COLOR_BGR2GRAY)

# Set the parameters of the ORB algorithm by specifying the maximum number of keypoints to locate and
# the pyramid decimation ratio
orb = cv2.ORB_create(1000, 2.0)

# Find the keypoints in the gray scale training and query images and compute their ORB descriptor.
# The None parameter is needed to indicate that we are not using a mask in either case.
keypoints_train, descriptors_train = orb.detectAndCompute(training_gray, None)
keypoints_query, descriptors_query = orb.detectAndCompute(query_gray, None)

# Create a Brute Force Matcher object. Set crossCheck to True so that the BFMatcher will only return consistent
# pairs. Such technique usually produces best results with minimal number of outliers when there are enough matches.
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck = True)

# Perform the matching between the ORB descriptors of the training image and the query image
matches = bf.match(descriptors_train, descriptors_query)

# The matches with shorter distance are the ones we want. So, we sort the matches according to distance
matches = sorted(matches, key = lambda x : x.distance)

# Connect the keypoints in the training image with their best matching keypoints in the query image.
# The best matches correspond to the first elements in the sorted matches list, since they are the ones
# with the shorter distance. We draw the first 30 mathces and use flags = 2 to plot the matching keypoints
# without size or orientation.
result = cv2.drawMatches(training_gray, keypoints_train, query_gray, keypoints_query, matches[:30], query_gray, flags = 2)

# Display the best matching points
plt.title('Best Matching Points')
plt.imshow(result)
plt.show()

# Print the shape of the training image
print('\nThe Training Image has shape:', training_gray.shape)

#Print the shape of the query image
print('The Query Image has shape:', query_gray.shape)

# Print the number of keypoints detected in the training image
print("\nNumber of Keypoints Detected In The Training Image: ", len(keypoints_train))

# Print the number of keypoints detected in the query image
print("Number of Keypoints Detected In The Query Image: ", len(keypoints_query))

# Print total number of matching points between the training and query images
print("\nNumber of Matching Keypoints Between The Training and Query Images: ", len(matches))

在上面的示例中廊宪,請(qǐng)注意訓(xùn)練圖像是553 x 471像素,而查詢圖像是138 x 117像素女轿,是原始訓(xùn)練圖像大小的1/4箭启。 另請(qǐng)注意,查詢圖像中檢測(cè)到的關(guān)鍵點(diǎn)數(shù)量?jī)H為65蛉迹,遠(yuǎn)小于訓(xùn)練圖像中的831個(gè)關(guān)鍵點(diǎn)傅寡。 然而,我們可以看到我們的蠻力匹配器可以將查詢圖像中的大多數(shù)關(guān)鍵點(diǎn)與訓(xùn)練圖像中的相應(yīng)關(guān)鍵點(diǎn)進(jìn)行匹配北救。

Rotational Invariance

ORB算法也是旋轉(zhuǎn)不變的荐操。 這意味著無(wú)論方向如何,它都能夠檢測(cè)圖像中的對(duì)象珍策。 為了看到這一點(diǎn)托启,我們現(xiàn)在將使用我們的Brute-Force匹配器來(lái)匹配訓(xùn)練圖像和旋轉(zhuǎn)了90度的查詢圖像之間的點(diǎn)。

import cv2
import matplotlib.pyplot as plt

# Set the default figure size
plt.rcParams['figure.figsize'] = [14.0, 7.0]

# Load the training image
image1 = cv2.imread('./images/face.jpeg')

# Load the query image
image2 = cv2.imread('./images/faceR.jpeg')

# Convert the training image to RGB
training_image = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)

# Convert the query image to RGB
query_image = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)

# Display the images
plt.subplot(121)
plt.title('Training Image')
plt.imshow(training_image)
plt.subplot(122)
plt.title('Query Image')
plt.imshow(query_image)
plt.show()

# Convert the training image to gray scale
training_gray = cv2.cvtColor(training_image, cv2.COLOR_BGR2GRAY)

# Convert the query image to gray scale
query_gray = cv2.cvtColor(query_image, cv2.COLOR_BGR2GRAY)

# Set the parameters of the ORB algorithm by specifying the maximum number of keypoints to locate and
# the pyramid decimation ratio
orb = cv2.ORB_create(1000, 2.0)

# Find the keypoints in the gray scale training and query images and compute their ORB descriptor.
# The None parameter is needed to indicate that we are not using a mask in either case.
keypoints_train, descriptors_train = orb.detectAndCompute(training_gray, None)
keypoints_query, descriptors_query = orb.detectAndCompute(query_gray, None)

# Create a Brute Force Matcher object. Set crossCheck to True so that the BFMatcher will only return consistent
# pairs. Such technique usually produces best results with minimal number of outliers when there are enough matches.
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck = True)

# Perform the matching between the ORB descriptors of the training image and the query image
matches = bf.match(descriptors_train, descriptors_query)

# The matches with shorter distance are the ones we want. So, we sort the matches according to distance
matches = sorted(matches, key = lambda x : x.distance)

# Connect the keypoints in the training image with their best matching keypoints in the query image.
# The best matches correspond to the first elements in the sorted matches list, since they are the ones
# with the shorter distance. We draw the first 100 mathces and use flags = 2 to plot the matching keypoints
# without size or orientation.
result = cv2.drawMatches(training_gray, keypoints_train, query_gray, keypoints_query, matches[:100], query_gray, flags = 2)

# Display the best matching points
plt.title('Best Matching Points')
plt.imshow(result)
plt.show()

# Print the number of keypoints detected in the training image
print("\nNumber of Keypoints Detected In The Training Image: ", len(keypoints_train))

# Print the number of keypoints detected in the query image
print("Number of Keypoints Detected In The Query Image: ", len(keypoints_query))

# Print total number of matching points between the training and query images
print("\nNumber of Matching Keypoints Between The Training and Query Images: ", len(matches))

在上面的例子中攘宙,我們看到在兩個(gè)圖像中檢測(cè)到的關(guān)鍵點(diǎn)的數(shù)量非常相似屯耸,即使查詢圖像被旋轉(zhuǎn),我們的Brute-Force匹配器仍然可以匹配找到的關(guān)鍵點(diǎn)的大約78%蹭劈。 此外疗绣,請(qǐng)注意大多數(shù)匹配關(guān)鍵點(diǎn)都接近特定的面部特征,例如眼睛铺韧,鼻子和嘴巴多矮。

Illumination Invariance

ORB算法也是照明不變的。 這意味著無(wú)論照明如何祟蚀,它都能夠檢測(cè)圖像中的物體工窍。 為了看到這一點(diǎn)割卖,我們現(xiàn)在將使用我們的Brute-Force匹配器來(lái)匹配訓(xùn)練圖像和更亮的查詢圖像之間的點(diǎn)。

import cv2
import matplotlib.pyplot as plt

# Set the default figure size
plt.rcParams['figure.figsize'] = [14.0, 7.0]

# Load the training image
image1 = cv2.imread('./images/face.jpeg')

# Load the query image
image2 = cv2.imread('./images/faceRI.png')

# Convert the training image to RGB
training_image = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)

# Convert the query image to RGB
query_image = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)

# Display the images
plt.subplot(121)
plt.title('Training Image')
plt.imshow(training_image)
plt.subplot(122)
plt.title('Query Image')
plt.imshow(query_image)
plt.show()

# Convert the training image to gray scale
training_gray = cv2.cvtColor(training_image, cv2.COLOR_BGR2GRAY)

# Convert the query image to gray scale
query_gray = cv2.cvtColor(query_image, cv2.COLOR_BGR2GRAY)

# Set the parameters of the ORB algorithm by specifying the maximum number of keypoints to locate and
# the pyramid decimation ratio
orb = cv2.ORB_create(1000, 2.0)

# Find the keypoints in the gray scale training and query images and compute their ORB descriptor.
# The None parameter is needed to indicate that we are not using a mask in either case.
keypoints_train, descriptors_train = orb.detectAndCompute(training_gray, None)
keypoints_query, descriptors_query = orb.detectAndCompute(query_gray, None)

# Create a Brute Force Matcher object. Set crossCheck to True so that the BFMatcher will only return consistent
# pairs. Such technique usually produces best results with minimal number of outliers when there are enough matches.
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck = True)

# Perform the matching between the ORB descriptors of the training image and the query image
matches = bf.match(descriptors_train, descriptors_query)

# The matches with shorter distance are the ones we want. So, we sort the matches according to distance
matches = sorted(matches, key = lambda x : x.distance)

# Connect the keypoints in the training image with their best matching keypoints in the query image.
# The best matches correspond to the first elements in the sorted matches list, since they are the ones
# with the shorter distance. We draw the first 100 mathces and use flags = 2 to plot the matching keypoints
# without size or orientation.
result = cv2.drawMatches(training_gray, keypoints_train, query_gray, keypoints_query, matches[:100], query_gray, flags = 2)

# Display the best matching points
plt.title('Best Matching Points')
plt.imshow(result)
plt.show()

# Print the number of keypoints detected in the training image
print("\nNumber of Keypoints Detected In The Training Image: ", len(keypoints_train))

# Print the number of keypoints detected in the query image
print("Number of Keypoints Detected In The Query Image: ", len(keypoints_query))

# Print total number of matching points between the training and query images
print("\nNumber of Matching Keypoints Between The Training and Query Images: ", len(matches))

在上面的例子中患雏,我們看到在兩個(gè)圖像中檢測(cè)到的關(guān)鍵點(diǎn)的數(shù)量再次非常相似鹏溯,即使查詢圖像更亮,我們的Brute-Force匹配器仍然可以匹配找到的關(guān)鍵點(diǎn)的大約63%淹仑。

Noise Invariance

ORB算法也是噪聲不變的丙挽。 這意味著它能夠檢測(cè)圖像中的對(duì)象,即使圖像具有一定程度的噪聲匀借。 為了看到這一點(diǎn)颜阐,我們現(xiàn)在將使用我們的Brute-Force匹配器來(lái)匹配訓(xùn)練圖像和具有大量噪聲的查詢圖像之間的點(diǎn)。

import cv2
import matplotlib.pyplot as plt

# Set the default figure size
plt.rcParams['figure.figsize'] = [14.0, 7.0]

# Load the training image
image1 = cv2.imread('./images/face.jpeg')

# Load the noisy, gray scale query image. 
image2 = cv2.imread('./images/faceRN5.png')

# Convert the query image to gray scale
query_gray = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)

# Convert the training image to gray scale
training_gray = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)

# Display the images
plt.subplot(121)
plt.imshow(training_gray, cmap = 'gray')
plt.title('Gray Scale Training Image')
plt.subplot(122)
plt.imshow(query_gray, cmap = 'gray')
plt.title('Query Image')
plt.show()

# Set the parameters of the ORB algorithm by specifying the maximum number of keypoints to locate and
# the pyramid decimation ratio
orb = cv2.ORB_create(1000, 1.3)

# Find the keypoints in the gray scale training and query images and compute their ORB descriptor.
# The None parameter is needed to indicate that we are not using a mask in either case. 
keypoints_train, descriptors_train = orb.detectAndCompute(training_gray, None)
keypoints_query, descriptors_query = orb.detectAndCompute(query_gray, None)

# Create a Brute Force Matcher object. We set crossCheck to True so that the BFMatcher will only return consistent
# pairs. Such technique usually produces best results with minimal number of outliers when there are enough matches.
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck = True)

# Perform the matching between the ORB descriptors of the training image and the query image
matches = bf.match(descriptors_train, descriptors_query)

# The matches with shorter distance are the ones we want. So, we sort the matches according to distance
matches = sorted(matches, key = lambda x : x.distance)

# Connect the keypoints in the training image with their best matching keypoints in the query image.
# The best matches correspond to the first elements in the sorted matches list, since they are the ones
# with the shorter distance. We draw the first 100 mathces and use flags = 2 to plot the matching keypoints
# without size or orientation.
result = cv2.drawMatches(training_gray, keypoints_train, query_gray, keypoints_query, matches[:100], query_gray, flags = 2)

# we display the image
plt.title('Best Matching Points')
plt.imshow(result)
plt.show()

# Print the number of keypoints detected in the training image
print("Number of Keypoints Detected In The Training Image: ", len(keypoints_train))

# Print the number of keypoints detected in the query image
print("Number of Keypoints Detected In The Query Image: ", len(keypoints_query))

# Print total number of matching points between the training and query images
print("\nNumber of Matching Keypoints Between The Training and Query Images: ", len(matches))

在上面的例子中吓肋,我們?cè)俅慰吹絻蓚€(gè)圖像中檢測(cè)到的關(guān)鍵點(diǎn)的數(shù)量非常相似凳怨,即使查詢圖像有很多噪聲,我們的Brute-Force匹配器仍然可以找到大約63%的關(guān)鍵點(diǎn)是鬼。 此外肤舞,請(qǐng)注意大多數(shù)匹配關(guān)鍵點(diǎn)都接近特定的面部特征,例如眼睛均蜜,鼻子和嘴巴李剖。 此外,我們可以看到有一些功能不太匹配囤耳,但可能因?yàn)閳D像區(qū)域中的強(qiáng)度模式相似而被選中篙顺。 我們還要指出,在這種情況下充择,我們使用的金字塔抽取率為1.3德玫,而不是前面示例中使用的值2.0,因?yàn)樵谶@種特殊情況下聪铺,它會(huì)產(chǎn)生更好的結(jié)果化焕。

目標(biāo)檢測(cè)

實(shí)現(xiàn)ORB算法以在另一圖像中檢測(cè)訓(xùn)練圖像中的面部拇涤。 像往常一樣矢门,我們將首先加載我們的培訓(xùn)和查詢圖像。

import cv2
import matplotlib.pyplot as plt

# Set the default figure size
plt.rcParams['figure.figsize'] = [14.0, 7.0]

# Load the training image
image1 = cv2.imread('./images/face.jpeg')

# Load the query image
image2 = cv2.imread('./images/Team.jpeg')

# Convert the training image to RGB
training_image = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)

# Convert the query image to RGB
query_image = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)

# Display the images
plt.subplot(121)
plt.imshow(training_image)
plt.title('Training Image')
plt.subplot(122)
plt.imshow(query_image)
plt.title('Query Image')
plt.show()

在這種特定情況下墨坚,訓(xùn)練圖像包含面部键兜,因此凤类,如我們所見(jiàn),檢測(cè)到的大多數(shù)關(guān)鍵點(diǎn)接近面部特征普气,例如眼睛谜疤,鼻子和嘴巴。 另一方面,我們的查詢圖像是一組人的圖片夷磕,其中一個(gè)是我們想要檢測(cè)的女人履肃。 現(xiàn)在讓我們檢測(cè)查詢圖像的關(guān)鍵點(diǎn)。

# Set the default figure size
plt.rcParams['figure.figsize'] = [34.0, 34.0]

# Convert the training image to gray scale
training_gray = cv2.cvtColor(training_image, cv2.COLOR_BGR2GRAY)

# Convert the query image to gray scale
query_gray = cv2.cvtColor(query_image, cv2.COLOR_BGR2GRAY)

# Set the parameters of the ORB algorithm by specifying the maximum number of keypoints to locate and
# the pyramid decimation ratio
orb = cv2.ORB_create(5000, 2.0)

# Find the keypoints in the gray scale training and query images and compute their ORB descriptor.
# The None parameter is needed to indicate that we are not using a mask in either case.  
keypoints_train, descriptors_train = orb.detectAndCompute(training_gray, None)
keypoints_query, descriptors_query = orb.detectAndCompute(query_gray, None)

# Create copies of the query images to draw our keypoints on
query_img_keyp = copy.copy(query_image)

# Draw the keypoints with size and orientation on the copy of the query image
cv2.drawKeypoints(query_image, keypoints_query, query_img_keyp, flags = cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Display the query image with the keypoints with size and orientation
plt.title('Keypoints With Size and Orientation', fontsize = 30)
plt.imshow(query_img_keyp)
plt.show()

# Print the number of keypoints detected
print("\nNumber of keypoints Detected: ", len(keypoints_query))

我們可以看到查詢圖像在圖像的許多部分都有關(guān)鍵點(diǎn)坐桩。 現(xiàn)在我們已經(jīng)獲得了訓(xùn)練圖像和查詢圖像的關(guān)鍵點(diǎn)和ORB描述符尺棋,我們可以使用Brute-Force匹配器來(lái)嘗試在查詢圖像中找到女性的臉部。

# Set the default figure size
plt.rcParams['figure.figsize'] = [34.0, 34.0]

# Create a Brute Force Matcher object. We set crossCheck to True so that the BFMatcher will only return consistent
# pairs. Such technique usually produces best results with minimal number of outliers when there are enough matches.
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck = True)

# Perform the matching between the ORB descriptors of the training image and the query image
matches = bf.match(descriptors_train, descriptors_query)

# The matches with shorter distance are the ones we want. So, we sort the matches according to distance
matches = sorted(matches, key = lambda x : x.distance)

# Connect the keypoints in the training image with their best matching keypoints in the query image.
# The best matches correspond to the first elements in the sorted matches list, since they are the ones
# with the shorter distance. We draw the first 85 mathces and use flags = 2 to plot the matching keypoints
# without size or orientation.
result = cv2.drawMatches(training_gray, keypoints_train, query_gray, keypoints_query, matches[:85], query_gray, flags = 2)

# we display the image
plt.title('Best Matching Points', fontsize = 30)
plt.imshow(result)
plt.show()

# Print the number of keypoints detected in the training image
print("Number of Keypoints Detected In The Training Image: ", len(keypoints_train))

# Print the number of keypoints detected in the query image
print("Number of Keypoints Detected In The Query Image: ", len(keypoints_query))

# Print total number of matching Keypoints between the training and query images
print("\nNumber of Matching Keypoints Between The Training and Query Images: ", len(matches))

我們可以清楚地看到绵跷,即使查詢圖像中有許多面部和對(duì)象膘螟,我們的Brute-Force匹配器也能夠在查詢圖像中正確定位女性的面部。

Histogram of Oriented Gradients (HOG)方向梯度直方圖

直方圖(Histogram)

直方圖就是數(shù)據(jù)分布的一種圖形表現(xiàn)



以灰度圖像為例:
假設(shè)你想繪制出這張薄餅圖的強(qiáng)度數(shù)據(jù)直方圖,像素值的范圍在 0 到 255 之間,所以我們可以把這些值分成若干組,創(chuàng)建 32 個(gè)組 每組包含 8 個(gè)像素值 所以第一組范圍是 0 到 7碾局、然后 8 到 15 以此類推一直到 248 到 255


方向梯度

方向很簡(jiǎn)單 指的就是圖像梯度的方向或朝向荆残,用 Sobel 算子來(lái)計(jì)算梯度的幅值和方向
把三個(gè)術(shù)語(yǔ)結(jié)合在一起,HOG 就是指一張有關(guān)圖像梯度方向的直方圖

  1. 首先 HOG 會(huì)接受一張圖像净当,然后計(jì)算每個(gè)像素的梯度幅值和方向
  2. HOG 實(shí)際上會(huì)把這些像素分成若干個(gè)較大的正方形單元 單元大小通常是 8 乘 8 如果圖片小一些 單元也就小一些内斯,如果是 8 乘 8 的單元,那就有 64 個(gè)梯度值蚯瞧,HOG 會(huì)計(jì)算每個(gè)單元相同的梯度方向有多少嘿期,將這些梯度的幅值相加,得到梯度強(qiáng)度
  3. 接著 HOG 會(huì)把所有方向數(shù)據(jù)放到直方圖里埋合,這個(gè)直方圖有九組 也就是九個(gè)值范圍,不過(guò)你可以建立更多組來(lái)進(jìn)一步分類數(shù)據(jù)



?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末萄传,一起剝皮案震驚了整個(gè)濱河市甚颂,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌秀菱,老刑警劉巖振诬,帶你破解...
    沈念sama閱讀 216,402評(píng)論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異衍菱,居然都是意外死亡赶么,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門脊串,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)辫呻,“玉大人,你說(shuō)我怎么就攤上這事琼锋》殴耄” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,483評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵缕坎,是天一觀的道長(zhǎng)怖侦。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么匾寝? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,165評(píng)論 1 292
  • 正文 為了忘掉前任搬葬,我火速辦了婚禮,結(jié)果婚禮上艳悔,老公的妹妹穿的比我還像新娘踩萎。我一直安慰自己,他們只是感情好很钓,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,176評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布香府。 她就那樣靜靜地躺著,像睡著了一般码倦。 火紅的嫁衣襯著肌膚如雪企孩。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,146評(píng)論 1 297
  • 那天袁稽,我揣著相機(jī)與錄音勿璃,去河邊找鬼。 笑死推汽,一個(gè)胖子當(dāng)著我的面吹牛补疑,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播歹撒,決...
    沈念sama閱讀 40,032評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼莲组,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了暖夭?” 一聲冷哼從身側(cè)響起锹杈,我...
    開(kāi)封第一講書(shū)人閱讀 38,896評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎迈着,沒(méi)想到半個(gè)月后竭望,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡裕菠,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,536評(píng)論 2 332
  • 正文 我和宋清朗相戀三年咬清,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片奴潘。...
    茶點(diǎn)故事閱讀 39,696評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡旧烧,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出萤彩,到底是詐尸還是另有隱情粪滤,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評(píng)論 5 343
  • 正文 年R本政府宣布雀扶,位于F島的核電站杖小,受9級(jí)特大地震影響肆汹,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜予权,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,008評(píng)論 3 325
  • 文/蒙蒙 一昂勉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧扫腺,春花似錦岗照、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至躁劣,卻和暖如春迫吐,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背账忘。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,815評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工志膀, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人鳖擒。 一個(gè)月前我還...
    沈念sama閱讀 47,698評(píng)論 2 368
  • 正文 我出身青樓溉浙,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親蒋荚。 傳聞我的和親對(duì)象是個(gè)殘疾皇子戳稽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,592評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容