图像特征提取 python

1. 边缘检测 (Edge Detection)

1.1 Sobel 算子

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

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 边缘检测是一种多级边缘检测算法,效果更好。

# 应用 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 角点检测是一种经典的角点检测方法。

# 读取图像
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 算法的改进。

# 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 是一种用于检测和描述局部特征的算法。

# 初始化 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 的加速版本,速度更快。

# 初始化 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 是一种快速且高效的特征检测和描述算法。

# 初始化 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. TCP协议是安全的吗?

    2024-06-09 11:00:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-09 11:00:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-09 11:00:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-09 11:00:02       18 阅读

热门阅读

  1. vue脚手架 笔记01

    2024-06-09 11:00:02       8 阅读
  2. 金融数据分析----code详解版

    2024-06-09 11:00:02       7 阅读
  3. 深入理解交叉熵损失CrossEntropyLoss - 损失函数

    2024-06-09 11:00:02       9 阅读
  4. 深入浅出服务发现:构建动态微服务架构的基石

    2024-06-09 11:00:02       7 阅读
  5. 事件驱动架构:新时代的软件设计范式

    2024-06-09 11:00:02       6 阅读
  6. C/C++开发,,pthreads-win32官网,pthreads-win32

    2024-06-09 11:00:02       7 阅读
  7. SpringBoot集成ClickHouse,含集成kerberos认证

    2024-06-09 11:00:02       9 阅读
  8. Angular知识概览

    2024-06-09 11:00:02       9 阅读
  9. Mac电脑arm64芯片Cocoapods 的 ffi 兼容问题

    2024-06-09 11:00:02       6 阅读
  10. 0105__学习一个 Linux 命令:objcopy 命令

    2024-06-09 11:00:02       10 阅读