图像特征提取 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()
相关推荐
bamb0013 小时前
一个项目带你入门AI应用开发01
python
05664613 小时前
Python康复训练——常用标准库
开发语言·python·学习
昆曲之源_娄江河畔13 小时前
Python如何安装flask, pymssql
开发语言·python·flask·pymssql
05664614 小时前
Python康复训练——控制流与函数
开发语言·python·学习
天使day14 小时前
FastAPI快速入门
python·fastapi
databook14 小时前
用相关性分析消除“冗余特征”
python·机器学习·scikit-learn
幻想时空15 小时前
地图数据采集
python
梦想不只是梦与想15 小时前
Python 中的类型判断方法
python·type·isinstance
alphaTao16 小时前
LeetCode 每日一题 2026/7/27-2026/8/2
python·算法·leetcode
sali-tec16 小时前
C# 基于OpenCv的视觉工作流-章98-频域滤波
图像处理·人工智能·opencv·计算机视觉