Sift v3 improvements

This commit is contained in:
Tom Selier 2023-09-28 10:00:48 +02:00
parent 4331f3b090
commit 33b76cce9a

View File

@ -3,15 +3,19 @@ import cv2
import os
import matplotlib.pyplot as plt
DATASET_PATH = "C:\\Users\\tomse\\Documents\\Dataset\\"
DATASET_PATH = "C:\\Users\\tomse\\Downloads\\Dataset\\"
DATASET_FOLDERS_LEN = len(os.listdir(DATASET_PATH))
EARLY_BREAK = 3
EARLY_BREAK = 0
SCALE = 1
# Plataan, Berk, Accasia
sift = cv2.SIFT.create(enable_precise_upscale=True)
## Create 2D lists ##
max_magnitudes = [[] for x in range(DATASET_FOLDERS_LEN)]
avg_magnitudes = [[] for x in range(DATASET_FOLDERS_LEN)]
tot_magnitudes = [[] for x in range(DATASET_FOLDERS_LEN)]
counts = [[] for x in range(DATASET_FOLDERS_LEN)]
## Create other variables ##
@ -19,6 +23,9 @@ labels = ['' for x in range(DATASET_FOLDERS_LEN)]
i = 0
for folder in os.listdir(DATASET_PATH):
if not folder.endswith("_out"):
continue
if EARLY_BREAK:
print("Step %d/%d processing %s:"%(i+1, EARLY_BREAK, folder))
else:
@ -28,18 +35,27 @@ for folder in os.listdir(DATASET_PATH):
## Load an image ##
path = DATASET_PATH + folder + "\\" + file
full_image = cv2.imread(path, 0)
image = cv2.resize(full_image, (0, 0), fx=0.25, fy=0.25)
image = cv2.resize(full_image, (0, 0), fx=SCALE, fy=SCALE)
## Detect keypoints ##
kp = sift.detect(image, None)
image = cv2.drawKeypoints(
image,
kp,
image,
flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
## Average and Max size ##
magnitudes = [keypoint.size for keypoint in kp]
tot_magnitudes[i].append(np.sum(magnitudes))
max_magnitudes[i].append(np.amax(magnitudes))
avg_magnitudes[i].append(np.sum(magnitudes)/len(kp))
## Number of keypoints ##
counts[i].append(len(kp))
# cv2.imshow("Opencv tech", image)
# cv2.waitKey(0)
## Store labels ##
labels[i] = folder
@ -54,8 +70,21 @@ print("Done!")
## Plots ##
fig, ax = plt.subplots()
for i in range(DATASET_FOLDERS_LEN):
ax.scatter(avg_magnitudes[i], max_magnitudes[i],label=labels[i], alpha=0.7)
ax.legend()
# for i in range(DATASET_FOLDERS_LEN):
# ax.scatter(tot_magnitudes[i], max_magnitudes[i],label=labels[i], alpha=0.7)
# ax.scatter(avg_magnitudes[0], max_magnitudes[0], label=labels[0], alpha=0.7)
# ax.scatter(avg_magnitudes[7], max_magnitudes[7], label=labels[7], alpha=0.7)
# ax.set_xlabel("Gemiddelde magnitude")
# ax.set_ylabel("Maximale magnitude")
# ax.legend()
# ax.grid(True)
# plt.show()
ax.scatter(tot_magnitudes[2], max_magnitudes[2], label='Berk', alpha=0.7)
ax.scatter(tot_magnitudes[3], max_magnitudes[3], label='Els', alpha=0.7)
ax.scatter(tot_magnitudes[7], max_magnitudes[7], label='Plataan', alpha=0.7)
ax.set_xlabel("Total magnitude")
ax.set_ylabel("Maximal magnitude")
ax.legend()
ax.grid(True)
plt.show()
plt.show()