From 43c82fb95cef260379b976dccf42f47f79187688 Mon Sep 17 00:00:00 2001 From: Tom Selier Date: Sat, 16 Sep 2023 16:46:09 +0200 Subject: [PATCH] added some feature detection algorithms --- src/helpers/algorithms/orb.py | 36 ++++++++++++++++++++++++++++++++++ src/helpers/algorithms/sift.py | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 src/helpers/algorithms/orb.py create mode 100644 src/helpers/algorithms/sift.py diff --git a/src/helpers/algorithms/orb.py b/src/helpers/algorithms/orb.py new file mode 100644 index 0000000..56aa4d3 --- /dev/null +++ b/src/helpers/algorithms/orb.py @@ -0,0 +1,36 @@ +import cv2 +import os +import matplotlib.pyplot as plt +import numpy as np + +path = os.path.join(os.getcwd(), './res/trees/accasia_sq1_original.png') + +orb = cv2.ORB.create(nfeatures=int(10e6)) + +results = np.zeros(0) +labels = [] +for filename in os.listdir(os.path.join(os.getcwd(), "./res/trees/")): + directory = os.path.join(os.getcwd(), "./res/trees/") + path = os.path.join(directory, filename) + img = cv2.imread(path ,1) + gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) + + kp = orb.detect(gray, None) + kp, des = orb.compute(img, kp) + + gray = cv2.drawKeypoints( + gray, + kp, + None, + #flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS + ) + results = np.append(results, len(kp)) + labels.append(filename.split('_', 1)[0]) + print(len(des)) + cv2.imshow(path, gray) + +plt.bar(labels, results) +plt.show() + +cv2.waitKey(0) +cv2.destroyAllWindows() \ No newline at end of file diff --git a/src/helpers/algorithms/sift.py b/src/helpers/algorithms/sift.py new file mode 100644 index 0000000..e964ec5 --- /dev/null +++ b/src/helpers/algorithms/sift.py @@ -0,0 +1,35 @@ +import numpy as np +import matplotlib.pyplot as plt +import cv2 +import os + +# references: +# https://docs.opencv.org/4.x/da/df5/tutorial_py_sift_intro.html + + +path = os.path.join(os.getcwd(), './res/trees/accasia_sq1_original.png') + +sift = cv2.SIFT.create(enable_precise_upscale=True) + +results = np.zeros(0) +labels = [] +for filename in os.listdir(os.path.join(os.getcwd(), "./res/trees/")): + directory = os.path.join(os.getcwd(), "./res/trees/") + path = os.path.join(directory, filename) + img = cv2.imread(path ,1) + gray= cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) + kp = sift.detect(gray, None) + img = cv2.drawKeypoints( + gray, + kp, + img, + flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) + results = np.append(results, len(kp)) + labels.append(filename.split('_', 1)[0]) + cv2.imshow(filename,img) + +plt.bar(labels, results) +plt.show() + +cv2.waitKey(0) +cv2.destroyAllWindows()