当前位置:网站首页>使用 OpenCV 提取图像的 HOG、SURF 及 LBP 特征 (含代码)
使用 OpenCV 提取图像的 HOG、SURF 及 LBP 特征 (含代码)
2022-07-31 05:15:00 【Cassiel_cx】
HOG
HOG 全称为 Histogram of Oriented Gradient,即方向梯度直方图,由于使用该特征进行行人检测的效果良好而被广泛应用。HOG 是一种在计算机视觉和图像处理中用来进行目标检测的特征描述算子,通过计算和统计图像局部区域的方向梯度直方图来构成特征。HOG 特征提取流程图如下图所示:

(1)采用 Gamma 校正法对输入图像进行颜色空间的标准化,目的是调节图像的对比度,降低图像局部的阴影和光照变化所造成的影响,同时可以抑制噪音的干扰。Gamma 校正法公式如下所示,式中 Gamma 一般为0.5:

(2)计算图像每个像素的梯度 (包括大小和方向);主要是为了捕获轮廓信息,同时进一步弱化光照的干扰。图像中像素点 (x,y) 的梯度为:

(3)将图像划分成小 Cell(例如 3*3 像素/Cell),并统计每个 Cell 的梯度直方图(不同梯度的个数),即可形成每个 Cell 的特征
(4)将若干个 Cell 组成一个 Block,一个 Block 内所有 Cell 的特征串联起来便得到该 Block 的HOG 特征,

代码实现
import cv2
import numpy as np
def normalization(image):
image = image
image -= image.min()
image = image / (image.max() - image.min())
image *= 255
image = image.astype(np.uint8)
return image
reff_image = cv2.imread(os.path.join('C:/Users/DELL/Desktop/imgs', image_dir[j]), -1)
reff_image = normalization(reff_image)
cell_size = (6, 6)
num_cells_per_block = (2, 2)
block_size = (num_cells_per_block[0] * cell_size[0], num_cells_per_block[1] * cell_size[1])
x_cells = reff_image.shape[1] // cell_size[0]
y_cells = reff_image.shape[0] // cell_size[1]
h_stride = 1
v_stride = 1
block_stride = (cell_size[0] * h_stride, cell_size[1] * v_stride)
num_bins = 9
win_size = (x_cells * cell_size[0] , y_cells * cell_size[1])
hog = cv2.HOGDescriptor(win_size, block_size, block_stride, cell_size, num_bins)
hog_descriptor_reff = hog.compute(reff_image)SURF
SURF 特征是 SIFT 特征的变体,SIFT 的最大缺点是计算耗时。SURF 把 DOH 中高斯二阶微分的目标进行简化,使得卷积平滑操作仅需要转换成加减运算,SURF 的鲁棒性好且时间复杂度低。SURF 特征不仅保持 SIFT 的尺度不变性与选择不变性,而且对光照变化和仿射变化同样具有很强的鲁棒性。特征提取步骤如下:
(1)构建特征点邻域的正方形区域;
(2)将上一步骤建立的正方形区域划分为几个子区域,在每个子区域里,计算网络空间里的特征向量,该特征包含 Harr 小波对水平与垂直方向的响应及响应和的绝对值;
(3)计算每个子区域的特征并进行累加,将子区域进行累计得到最终的特征向量描述子。
代码实现
import cv2
import numpy as np
reff_image = cv2.imread(os.path.join('C:/Users/DELL/Desktop/imgs', image_dir[j]), -1)
gray = cv2.cvtColor(reff_image , cv2.COLOR_RGB2GRAY)
# set Hessian threshold
detector = cv2.xfeatures2d.SURF_create(2000)
# find keypoints and descriptors directly
kps, des = detector.detectAndCompute(reff_image, None)
reff_image = cv2.drawKeypoints(image=reff_image, outImage=reff_image, keypoints=kps, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS, color=(255, 0, 0))需要注意的是:使用新版 opencv-python 库时会提示:
AttributeError: module 'cv2' has no attribute 'xfeatures2d'
原因是新版 OpenCV 没有该算法的版权,解决方法是卸载掉当前版本的 opencv-python 库,重新安装之前版本的库 (如:opencv-python==3.4.2.16) :
pip uninstall opencv-python
pip install opencv-python==3.4.2.16
# or
pip install opencv-contrib-python
LBP
LBP 全称为 Local Binary Pattern,即局部二值模式特征,是一种用来描述图像局部纹理特征的算子。LBP 特征计算简单、效果较好,数据量小,因此 LBP 特征在计算机视觉的许多领域都得到了广泛的应用。LBP 特征具有灰度不变性和旋转不变性等显著优点,例如对光照不敏感。
LBP 定义在像素点周围 3*3 区域内,以中心像素为阈值,将相邻 8 个像素点的灰度值与中心像素值进行比较,如果周围像素值大于中心像素值,则该像素点的位置被标为 1。3*3 区域内的 8 个点可以产生 8 位二进制数,这个二进制数字就是中心像素点的 LBP 值。

代码实现
import cv2
import numpy as np
from skimage.feature import local_binary_pattern
reff_image = cv2.imread(os.path.join('C:/Users/DELL/Desktop/imgs', image_dir[j]), -1)
# LBP算法中范围半径的取值
radius = 1
# 领域像素点数
n_points = 8 * radius
reff_lbp = local_binary_pattern(reff_image, n_points, radius)边栏推荐
猜你喜欢
![[Cloud Native] What should I do if SQL (and stored procedures) run too slowly?](/img/40/be3a30743ee2bc6c82f82ab852f29c.png)
[Cloud Native] What should I do if SQL (and stored procedures) run too slowly?

Global scope and function scope in js
![[uiautomation] Get WeChat friend list (stored in txt)](/img/26/1f3424c5998c52c6e10ced8529012a.png)
[uiautomation] Get WeChat friend list (stored in txt)

Notes on creating a new virtual machine in Hyper-V

Understanding of objects and functions in js

unicloud 云开发记录

Tencent Cloud GPU Desktop Server Driver Installation

Nmap的下载与安装

为数学而歌之伯努利家族

flutter arr dependencies
随机推荐
js中的全局作用域与函数作用域
Build vulhub vulnerability shooting range on kali
cocos2d-x-3.2图片灰化效果
网页截图与反向代理
What is GameFi?
Using IIS10 to build an asp website in win11
2021 Mianjing - Embrace Change
sql 添加 default 约束
自定dialog 布局没有居中解决方案
为数学而歌之伯努利家族
【云原生】微服务Nacos的简单介绍与使用
2021年京东数据分析工程师秋招笔试编程题
sql 外键约束【表关系绑定】
Artifact SSMwar exploded Error deploying artifact.See server log for details
TransactionTemplate 事务编程式写法
Sqlite column A data is copied to column B
What is the difference between NFT and digital collection?
This in js points to the prototype object
quick-3.5 lua调用c++
Global scope and function scope in js