import numpy as np
import cv2 as cv
import os
from matplotlib import pyplot as plt
def draw_keypoints(img, corners, color, radius=2):
# 要绘制的图像为灰度图,但绘制的点为彩色点
img = np.repeat(img[..., np.newaxis], 3, -1)
# 将图像复制成三通道类似于RGB,否则cv.circle()画点时会按img本身的灰度图处理
for c in corners:
cv.circle(img, tuple(c), radius, color, thickness=-1)
# cv.circle中要求输入的点为tuple, thickness=-1代表为实心圆
return img
def visulize_kp(imgpath, keypointpath, savepath):
'''
:param imgpath: the path of the image need to be visulized
:param keypointpath: the path of the file recorded the keypoints, usually with the tail '.npz'
:param savepath
:return: none
'''
img = cv.imread(imgpath,0)
kplist = np.load(keypointpath)['pts']
kp = []
for n in kplist:
kp.append((int(n[0]),int(n[1])))
img2 = draw_keypoints(img, kp, (0, 255, 0))
# kp = [cv.KeyPoint(p[1], p[0], 0.1) for p in kp] # 需要将二维特征点坐标转化为KeyPoint对象
# img2 = cv.drawKeypoints(img, kp, None, color=(255,0,0)) # 此种方法绘制的点虽为彩色但是是用圆圈表示,而非实心点
if not os.path.exists(savepath):
os.makedirs(savepath)
savename = os.path.split(imgpath)[1]
tsavepath = os.path.join(savepath, savename)
cv.imwrite(tsavepath, img2)