6种不同的图像特征提取代码
时间: 2024-05-07 15:23:30 浏览: 5
这里给出6种不同的图像特征提取代码:
1. SIFT特征提取:
```python
import cv2
# 读取图像
img = cv2.imread('image.jpg')
# 创建SIFT对象
sift = cv2.xfeatures2d.SIFT_create()
# 检测关键点和描述符
keypoints, descriptors = sift.detectAndCompute(img, None)
# 绘制关键点
img_with_keypoints = cv2.drawKeypoints(img, keypoints, None)
# 显示图像
cv2.imshow('Image with keypoints', img_with_keypoints)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
2. SURF特征提取:
```python
import cv2
# 读取图像
img = cv2.imread('image.jpg')
# 创建SURF对象
surf = cv2.xfeatures2d.SURF_create()
# 检测关键点和描述符
keypoints, descriptors = surf.detectAndCompute(img, None)
# 绘制关键点
img_with_keypoints = cv2.drawKeypoints(img, keypoints, None)
# 显示图像
cv2.imshow('Image with keypoints', img_with_keypoints)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
3. ORB特征提取:
```python
import cv2
# 读取图像
img = cv2.imread('image.jpg')
# 创建ORB对象
orb = cv2.ORB_create()
# 检测关键点和描述符
keypoints, descriptors = orb.detectAndCompute(img, None)
# 绘制关键点
img_with_keypoints = cv2.drawKeypoints(img, keypoints, None)
# 显示图像
cv2.imshow('Image with keypoints', img_with_keypoints)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
4. Harris角点检测:
```python
import cv2
import numpy as np
# 读取图像
img = cv2.imread('image.jpg')
# 转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 计算Harris角点
dst = cv2.cornerHarris(gray, 2, 3, 0.04)
# 找到角点
dst = cv2.dilate(dst, None)
ret, dst = cv2.threshold(dst, 0.01 * dst.max(), 255, 0)
dst = np.uint8(dst)
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
# 绘制角点
for i in range(1, len(stats)):
cv2.circle(img, (int(centroids[i][0]), int(centroids[i][1])), 5, (0, 255, 0), 2)
# 显示图像
cv2.imshow('Image with corners', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
5. HOG特征提取:
```python
import cv2
# 读取图像
img = cv2.imread('image.jpg')
# 创建HOG对象
hog = cv2.HOGDescriptor()
# 计算HOG特征
hog_features = hog.compute(img)
# 显示HOG特征
cv2.imshow('HOG features', hog_features)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
6. Haar特征提取:
```python
import cv2
# 读取图像
img = cv2.imread('image.jpg')
# 创建Haar分类器
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# 检测人脸
faces = face_cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
# 绘制人脸矩形
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
# 显示图像
cv2.imshow('Image with faces', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```