Skip to content

Instantly share code, notes, and snippets.

@Calcifer777
Created November 30, 2024 09:00
Show Gist options
  • Save Calcifer777/c9177b12113bcdeb2d2003a5ea94e790 to your computer and use it in GitHub Desktop.
Save Calcifer777/c9177b12113bcdeb2d2003a5ea94e790 to your computer and use it in GitHub Desktop.
cv2-keypoint-detection
import cv2
import argparse
def detect_and_draw_keypoints(image_path: str, output_path: str) -> None:
"""Detect keypoints in the image using BRISK and save the output image with keypoints drawn.
Args:
image_path (str): Path to the input image.
output_path (str): Path to save the output image with keypoints.
"""
# Read the image
image = cv2.imread(image_path)
if image is None:
raise ValueError(f"Image not found at the path: {image_path}")
# Initialize the BRISK detector
brisk = cv2.BRISK_create(
thresh=5,
octaves=3,
patternScale=0.1,
)
# brisk = cv2.BRISK_create(
# thresh=10,
# octaves=3,
# radiusList=[1, 2],
# numberList=[1, 1],
# )
# Detect keypoints
keypoints = brisk.detect(image, None)
# Draw keypoints on the image
output_image = cv2.drawKeypoints(
image=image,
keypoints=keypoints,
outImage=image,
color=(146, 98, 240),
)
# Save the output image
cv2.imwrite(output_path, output_image)
print(f"Output image saved to: {output_path}")
def parse_args() -> argparse.Namespace:
"""Parse command line arguments.
Returns:
argparse.Namespace: Parsed command line arguments.
"""
parser = argparse.ArgumentParser(
description="Detect keypoints in an image using BRISK."
)
parser.add_argument("image_path", type=str, help="Path to the input image.")
parser.add_argument(
"output_path", type=str, help="Path to save the output image with keypoints."
)
return parser.parse_args()
def main() -> None:
"""Main function to run the keypoint detection and drawing."""
args = parse_args()
detect_and_draw_keypoints(args.image_path, args.output_path)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment