图像特征提取 python

1. 边缘检测 (Edge Detection)

1.1 Sobel 算子

Sobel 算子是一种边缘检测算子,通过计算图像梯度来检测边缘。

python 复制代码
import cv2
import numpy as np

# 读取图像
image = cv2.imread('image.jpg', 0)

# 应用 Sobel 算子
sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5)
sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)
sobel = cv2.magnitude(sobel_x, sobel_y)

# 显示结果
cv2.imshow('Sobel Edge Detection', sobel)
cv2.waitKey(0)
cv2.destroyAllWindows()
1.2 Canny 边缘检测

Canny 边缘检测是一种多级边缘检测算法,效果更好。

python 复制代码
# 应用 Canny 边缘检测
canny_edges = cv2.Canny(image, 100, 200)

# 显示结果
cv2.imshow('Canny Edge Detection', canny_edges)
cv2.waitKey(0)
cv2.destroyAllWindows()

2. 角点检测 (Corner Detection)

2.1 Harris 角点检测

Harris 角点检测是一种经典的角点检测方法。

python 复制代码
# 读取图像
image = cv2.imread('image.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Harris 角点检测
gray_image = np.float32(gray_image)
dst = cv2.cornerHarris(gray_image, 2, 3, 0.04)
image[dst > 0.01 * dst.max()] = [0, 0, 255]

# 显示结果
cv2.imshow('Harris Corners', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
2.2 Shi-Tomasi 角点检测

Shi-Tomasi 角点检测是对 Harris 算法的改进。

python 复制代码
# Shi-Tomasi 角点检测
corners = cv2.goodFeaturesToTrack(gray_image, 100, 0.01, 10)
corners = np.int0(corners)

for corner in corners:
    x, y = corner.ravel()
    cv2.circle(image, (x, y), 3, 255, -1)

# 显示结果
cv2.imshow('Shi-Tomasi Corners', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

3. 尺度不变特征变换 (SIFT)

SIFT 是一种用于检测和描述局部特征的算法。

python 复制代码
# 初始化 SIFT
sift = cv2.SIFT_create()

# 检测关键点并计算描述子
keypoints, descriptors = sift.detectAndCompute(image, None)

# 在图像中绘制关键点
sift_image = cv2.drawKeypoints(image, keypoints, None)

# 显示结果
cv2.imshow('SIFT Keypoints', sift_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

4. 加速稳健特征 (SURF)

SURF 是 SIFT 的加速版本,速度更快。

python 复制代码
# 初始化 SURF
surf = cv2.xfeatures2d.SURF_create()

# 检测关键点并计算描述子
keypoints, descriptors = surf.detectAndCompute(image, None)

# 在图像中绘制关键点
surf_image = cv2.drawKeypoints(image, keypoints, None)

# 显示结果
cv2.imshow('SURF Keypoints', surf_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

5. ORB (Oriented FAST and Rotated BRIEF)

ORB 是一种快速且高效的特征检测和描述算法。

python 复制代码
# 初始化 ORB
orb = cv2.ORB_create()

# 检测关键点并计算描述子
keypoints, descriptors = orb.detectAndCompute(image, None)

# 在图像中绘制关键点
orb_image = cv2.drawKeypoints(image, keypoints, None)

# 显示结果
cv2.imshow('ORB Keypoints', orb_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
相关推荐
拜小白1 小时前
【论文解读】PanoContext:全景场景理解的全房间 3D 上下文模型
计算机视觉·全景图
Keep_Trying_Go2 小时前
MobileSAMv2分割算法详解
图像处理·人工智能·计算机视觉
kobe_OKOK_7 小时前
__getattr__和__getattribute__如何用
python
richard_yuu8 小时前
OpenCV 实战第 9 篇:Template Matching vs 特征匹配,ROI 定位该选哪个
opencv
全栈弄潮儿8 小时前
Python实战第2期: 变量与数据类型
后端·python·agent
长沙三为智能科技8 小时前
家政小程序门店运营模块设计:会员、下单、派单、结算四段链路的落地拆解
python·小程序·内容运营
Cc.Y8 小时前
Java零基础入门:可变字符串与包装类:StringBuilder、StringBuffer 与 通讯录管理系统实战
java·开发语言·python
lank_M8 小时前
截图OCR预处理在普通屏上翻车,Retina截图却没事
图像处理·人工智能·计算机视觉·ocr
启观川8 小时前
数据结构与算法 -第 3 章 常用算法-动态规划
数据结构·笔记·python·算法
码爸8 小时前
排序算法介绍
python·算法·排序算法