Python-OpenCV中的图像处理-图像特征
- 图像特征
- Harris角点检测
- 亚像素级精度的角点检测
- Shi-Tomasi角点检测
- SIFT(Scale-Invariant Feature Transfrom)
- SURF(Speeded-Up Robust Features)
图像特征
- 特征理解
- 特征检测
- 特征描述
Harris角点检测
- cv2.cornerHarris(img, blockSize, ksize, k, borderType=…)
- img:输入图像,数据类型为float32
- blockSize:角点检测中要考虑的领域大小
- ksize:Sobe求导中使用的窗口大小
- k:Harris角点检测方程中的自由参数,取值参数为 [0.04,0.06]
- borderType:边界类型
import numpy as np
import cv2
from matplotlib import pyplot as plt# img = cv2.imread('./resource/opencv/image/chessboard.png', cv2.IMREAD_COLOR)
img = cv2.imread('./resource/opencv/image/pattern.png', cv2.IMREAD_COLOR)gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)gray = np.float32(gray)# 输入图像必须是float32,最后一个参数在0.04到0.05之间
dst = cv2.cornerHarris(gray, 2, 3, 0.05)
dst = cv2.dilate(dst, None)img[dst>0.01*dst.max()] = [0, 0, 255]cv2.imshow('dst', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
亚像素级精度的角点检测
- cv2.cornerSubPix(img, corners, winSize, zeroZone, criteria)
最大精度的角点检测,首先要找到 Harris角点,然后将角点的重心传给这个函数进行修正。
import numpy as np
import cv2
from matplotlib import pyplot as pltimg = cv2.imread('./resource/opencv/image/subpixel.png', cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)gray = np.float32(gray)
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)criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)corners = cv2.cornerSubPix(gray, np.float32(centroids), (5,5), (-1, -1), criteria)res = np.hstack((centroids, corners))res = np.int0(res)
img[res[:,1],res[:,0]]=[0,0,255]
img[res[:,3],res[:,2]]=[0,255,0]cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Harris 角点用红色像素标出,绿色像素是修正后的角点。
Shi-Tomasi角点检测
- cv2.goodFeatureToTrack()
import numpy as np
import cv2
from matplotlib import pyplot as pltimg = cv2.imread('./resource/opencv/image/shitomasi_block.jpg', cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)corners = cv2.goodFeaturesToTrack(gray, 25, 0.01, 10)corners = np.int0(corners)for i in corners:x,y = i.ravel()cv2.circle(img, (x,y), 3, 255, -1)plt.imshow(img)
plt.show()
SIFT(Scale-Invariant Feature Transfrom)
-
SIFT,即尺度不变特征变换(Scale-invariant feature transform,SIFT),是用于图像处理领域的一种描述。这种描述具有尺度不变性,可在图像中检测出关键点,是一种局部特征描述子。
-
cv2.SIFT_create()
- kp = sift.detect(img, None):查找特征点
- kp, des = sift.compute(img, kp):计算特征点
- kp, des = sift.detectAndCompute(img, None) :直接找到特征点并计算描述符
-
cv2.drawKeypoints(img, kp, out_img, flags=cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS):画特征点
- img : 输入图像
- kp:图像特征点
- out_img:输出图像
- flags:
cv2.DRAW_MATCHES_FLAGS_DEFAULT
cv2.DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS
cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS
import numpy as np
import cv2# 读取图片
# img = cv2.imread('./resource/opencv/image/home.jpg')
img = cv2.imread('./resource/opencv/image/AverageMaleFace.jpg')
key_points = img.copy()# 实例化SIFT算法
sift = cv2.SIFT_create()# 得到特征点
kp = sift.detect(img, None)
print(np.array(kp).shape)# 绘制特征点
cv2.drawKeypoints(img, kp, key_points, flags=cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS)# 图片展示
cv2.imshow("key points", key_points)
cv2.waitKey(0)
cv2.destroyAllWindows()# 保存图片
# cv2.imwrite("key_points.jpg", key_points)# 计算特征
kp, des = sift.compute(img, kp)# 调试输出
print(des.shape)
print(des[0])cv2.imshow('kp', key_points)
cv2.waitKey(0)
cv2.destroyAllWindows()