OpenCV-Python 图像的角点特征检测

162 阅读1分钟
import numpy as np
import matplotlib.pyplot as plt
import cv2 as cv
# 设置兼容中文
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

1.Harris角点检测

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-f3ZuzXGS-1637754891566)(attachment:image.png)]

chessboard = cv.imread('img/chessboard.jpg')
# 转化为灰度图
gray = cv.cvtColor(chessboard,cv.COLOR_BGR2GRAY)
# 数据类型转化为float32
gray = np.float32(gray)
# Harris角点检测
res = cv.cornerHarris(gray,2,3,0.04)
# 将检测大于0.001的认为是角点,并设置其颜色为红色
chessboard[res>0.001*res.max()] = [0,0,255]
# 可视化
plt.figure(dpi=500)
plt.imshow(chessboard[:,:,::-1])
<matplotlib.image.AxesImage at 0x1c37f82f6a0>




[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-32l4aOjK-1637754891568)(output_8_1.png)]

2.Shi-Tomasi角点检测

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Qi21CECs-1637754891570)(attachment:image.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uLQMxmcI-1637754891573)(attachment:image.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-meMgbtE7-1637754891573)(attachment:image.png)]

tv = cv.imread('img/tv.jpg')
# 转化为灰度图
gray = cv.cvtColor(tv,cv.COLOR_BGR2GRAY)
res = cv.goodFeaturesToTrack(gray,1000,0.01,10)
res
array([[[ 39., 363.]],

       [[ 39., 424.]],

       [[216., 431.]],

       ...,

       [[450., 209.]],

       [[331., 198.]],

       [[365., 421.]]], dtype=float32)
for i in res:
    x,y = i.ravel()
    cv.circle(tv,(int(x),int(y)),2,(0,0,255),-1)
plt.figure(dpi=400)
plt.imshow(tv[:,:,::-1])
<matplotlib.image.AxesImage at 0x1c353603910>




[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SRDa4BbL-1637754891574)(output_17_1.png)]

总结

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VPtzeRki-1637754891575)(attachment:image.png)]