《------往期经典推荐------》
二、机器学习实战专栏【链接】,已更新31期,欢迎关注,持续更新中~~
三、深度学习【Pytorch】专栏【链接】
四、【Stable Diffusion绘画系列】专栏【链接】
五、YOLOv8改进专栏【链接】,持续更新中~~
六、YOLO性能对比专栏【链接】,持续更新中~
《------正文------》
目录
引言
图像的基本特征提取可以在下面的文章中找到:
《使用Python进行图像特征提取【1】:归一化、边缘检测、图像滤波、直方图均衡》
除了传统的颜色、形状和纹理特征之外,还存在若干先进的技术来从图像中提取特征。在这篇文章中,我们将研究以下方法。
- 尺度不变特征变换
- ORB(Oriented FAST and Rotated Brief)
- Gabor滤波器
- 局部二进制模式(LBP)
上述方法通常用于许多图像处理和计算机视觉任务,如对象检测,识别和分类。
1.读取图像
我们将加载与上一篇文章中使用的相同的图像。
import cv2
import matplotlib.pyplot as plt
# Load image
image = cv2.imread('img.jpeg')
# Convert image to RGB (OpenCV loads in BGR)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display the image
plt.imshow(image_rgb)
plt.axis('off')
plt.show()
2.高级形状和关键点提取方法
2.1尺度不变特征变换
它用于识别和描述图像中的局部特征。据说它是尺度和旋转不变的,使其成为对象识别的强大特征提取技术。
# Initialize SIFT detector
sift = cv2.SIFT_create()
# Detect keypoints and descriptors
keypoints, descriptors = sift.detectAndCompute(image_rgb, None)
# Draw keypoints
image_with_keypoints = cv2.drawKeypoints(image_rgb, keypoints, None)
# Display keypoints
plt.imshow(image_with_keypoints)
plt.axis('off')
plt.show()
print(f"Number of keypoints detected: {len(keypoints)}")
2.2 ORB(Oriented FAST and Rotated Brief)
ORB是SIFT的一种替代方法,在计算效率很重要的情况下使用。它通常用于实时应用程序。
# Initialize ORB detector
orb = cv2.ORB_create()
# Detect keypoints and descriptors
keypoints_orb, descriptors_orb = orb.detectAndCompute(image_rgb, None)
# Draw keypoints
image_with_keypoints_orb = cv2.drawKeypoints(image_rgb, keypoints_orb, None)
# Display keypoints
plt.imshow(image_with_keypoints_orb)
plt.axis('off')
plt.show()
print(f"Number of ORB keypoints detected: {len(keypoints_orb)}")
3.纹理特征提取方法
3.1 Gabor滤波器
Gabor滤波器用于捕获纹理信息。这些滤波器对于捕获空间频率特性至关重要,并用于图像的纹理分析。
import numpy as np
from skimage.filters import gabor
# Apply Gabor filter
frequency = 0.2
gabor_image, gabor_response = gabor(gray_image, frequency=frequency)
# Display Gabor filtered image
plt.imshow(gabor_image, cmap='gray')
plt.title('Gabor Filtered Image')
plt.axis('off')
plt.show()
Gabor滤波器是方向性的,因此您可以使用不同的方向来捕获不同方向的纹理图案。
3.2局部二进制模式(LBP)
局部二值模式(LBP)通过将每个像素与其周围的邻居进行比较来捕获图像的局部结构。
from skimage.feature import local_binary_pattern
# Parameters for LBP
radius = 5 # Radius of the circular neighborhood
n_points = 5 * radius # Number of sampling points
# Apply LBP to the grayscale image
lbp = local_binary_pattern(gray_image, n_points, radius, method='uniform')
# Display the LBP image
plt.imshow(lbp, cmap='gray')
plt.title('Local Binary Patterns')
plt.axis('off')
plt.show()
# Histogram of LBP
hist, _ = np.histogram(lbp.ravel(), bins=np.arange(3, n_points + 3), range=(5, n_points + 2))
# Normalize the histogram
hist = hist.astype('float')
hist /= hist.sum()
print(f"LBP Histogram: {hist}")
4.特征提取方法综述
我们在上一篇文章中介绍了传统的特征提取方法,以及颜色、形状和纹理的高级特征提取技术。这里有一个简短的总结:
*颜色直方图:* 捕获图像中颜色的分布。
*轮廓:* 用于提取对象的形状。
*GLCM(灰度共生矩阵):* 提取统计纹理特征。
*SIFT和ORB:* 关键点检测,用于在图像中找到独特和不变的特征。
*Gabor滤波器:* 用于捕获空间频率和纹理信息。
*LBP(Local Binary Patterns):* 一种基于局部像素强度差异的简单有效的纹理分析方法。
结论
特征提取是图像处理和计算机视觉的重要组成部分,它将原始图像数据转换为可用于机器学习模型的有价值的信息或特征数据。在这篇第2部分的文章中,我们已经介绍了更多的方法来提取与颜色,形状和纹理相关的特征。每种方法都有自己的优势,可以根据项目的要求进行组合或单独使用。通过使用这些特征提取技术,您可以构建机器学习模型,这些模型是图像分类,对象检测和其他计算机视觉应用的更准确和更强大的模型。
好了,这篇文章就介绍到这里,喜欢的小伙伴感谢给点个赞和关注,更多精彩内容持续更新~~
关于本篇文章大家有任何建议或意见,欢迎在评论区留言交流!