Files
ocv_template_matching/ocv_xfeatures.py
T
2024-07-12 12:17:22 +02:00

38 lines
1.2 KiB
Python

from __future__ import print_function
import cv2 as cv
import numpy as np
import argparse
from matplotlib import pyplot as plt
parser = argparse.ArgumentParser(description='Code for Feature Detection tutorial.')
parser.add_argument('--input', help='Path to input image.', default='data/1.jpg')
args = parser.parse_args()
src = cv.imread(cv.samples.findFile(args.input), cv.IMREAD_GRAYSCALE)
if src is None:
print('Could not open or find the image:', args.input)
exit(0)
# -- Step 1: Detect the keypoints using SURF Detector
minHessian = 400
detector = cv.xfeatures2d.HarrisLaplaceFeatureDetector.create()
keypoints = detector.detect(src)
# -- Draw keypoints
img_keypoints = np.empty((src.shape[0], src.shape[1], 3), dtype=np.uint8)
cv.drawKeypoints(src, keypoints, img_keypoints)
# -- Show detected (drawn) keypoints
cv.imshow('SURF Keypoints', img_keypoints)
corners = cv.goodFeaturesToTrack(src, 25, 0.01, 10)
# Draw corners detected
print('** Number of corners detected:', corners.shape[0])
radius = 4
for i in range(corners.shape[0]):
cv.circle(src, (int(corners[i, 0, 0]), int(corners[i, 0, 1])), radius, (0, 0, 255))
cv.imshow('goodFeaturesToTrack', src)
cv.waitKey()