added csv extraction

This commit is contained in:
Tom Selier 2023-09-29 21:49:27 +02:00
parent b44cad7102
commit aab3e22fd8
6 changed files with 169 additions and 22 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,9 @@
label,avg_b,avg_g,avg_r,std_b,std_g,std_r,
accasia,69.59916027137541,93.23599740738842,92.76413721475456,29.650789354560942,28.164331202979742,28.33379470933227,
berk,87.54178037532186,107.36707160860637,105.9353394079013,41.72180441785215,40.129023307835965,38.64035787139545,
eik,103.74168566057762,119.53735094782796,117.95327527937933,34.77238255525745,33.829618649660944,32.82833612530793,
els,138.35231618986808,177.39126510134065,181.7525877978574,51.65085237511483,43.383068821542174,42.175152137736305,
esdoorn,47.365041621150056,77.05362430457048,69.52487212460042,25.207693719301673,24.628219686465727,22.25080719620302,
es,63.945103048399794,95.91903641356423,90.20861703612438,20.389426811481062,18.395161770474054,17.716844447273072,
linde,111.6252480236714,123.48265354152022,129.6323361059727,35.25451067293944,34.66671249273108,33.99378641928277,
plataan,47.74452753592992,82.96830215527001,85.52863037536183,14.8187819404046,18.13170218888685,19.52480824126827,
1 label avg_b avg_g avg_r std_b std_g std_r
2 accasia 69.59916027137541 93.23599740738842 92.76413721475456 29.650789354560942 28.164331202979742 28.33379470933227
3 berk 87.54178037532186 107.36707160860637 105.9353394079013 41.72180441785215 40.129023307835965 38.64035787139545
4 eik 103.74168566057762 119.53735094782796 117.95327527937933 34.77238255525745 33.829618649660944 32.82833612530793
5 els 138.35231618986808 177.39126510134065 181.7525877978574 51.65085237511483 43.383068821542174 42.175152137736305
6 esdoorn 47.365041621150056 77.05362430457048 69.52487212460042 25.207693719301673 24.628219686465727 22.25080719620302
7 es 63.945103048399794 95.91903641356423 90.20861703612438 20.389426811481062 18.395161770474054 17.716844447273072
8 linde 111.6252480236714 123.48265354152022 129.6323361059727 35.25451067293944 34.66671249273108 33.99378641928277
9 plataan 47.74452753592992 82.96830215527001 85.52863037536183 14.8187819404046 18.13170218888685 19.52480824126827

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,9 @@
label,avg_b,avg_g,avg_r,std_b,std_g,std_r,
accasia,35.90440242881845,79.12766192516973,96.03904497370279,10.316069136947837,32.12312271750131,28.112318716352682,
berk,37.269905856891874,66.77426733032776,109.28940151500194,12.477741919995777,32.92938477640848,39.80902152951404,
eik,39.76121762093227,41.20602771946309,121.12686669901616,23.03425646554001,25.365001089124593,33.494192142922856,
els,27.25760648235924,68.91241506727702,182.27155476463224,4.423353762285157,34.149610367865485,42.029268409707505,
esdoorn,40.44041857512247,131.63818552294646,77.84993286553097,9.535383780285505,39.540648429565636,24.46222201958771,
es,38.37579404108352,98.88868379328436,96.68345708286985,6.84651057363783,30.56118627938561,18.229878712396474,
linde,36.8388961321136,41.02522196067761,130.70675471758108,31.06746560545738,22.601057153094764,34.24357220627512,
plataan,30.208627954766115,123.22600953975079,88.02246907749901,4.833302068080008,20.980674302352114,19.054660495972172,
1 label avg_b avg_g avg_r std_b std_g std_r
2 accasia 35.90440242881845 79.12766192516973 96.03904497370279 10.316069136947837 32.12312271750131 28.112318716352682
3 berk 37.269905856891874 66.77426733032776 109.28940151500194 12.477741919995777 32.92938477640848 39.80902152951404
4 eik 39.76121762093227 41.20602771946309 121.12686669901616 23.03425646554001 25.365001089124593 33.494192142922856
5 els 27.25760648235924 68.91241506727702 182.27155476463224 4.423353762285157 34.149610367865485 42.029268409707505
6 esdoorn 40.44041857512247 131.63818552294646 77.84993286553097 9.535383780285505 39.540648429565636 24.46222201958771
7 es 38.37579404108352 98.88868379328436 96.68345708286985 6.84651057363783 30.56118627938561 18.229878712396474
8 linde 36.8388961321136 41.02522196067761 130.70675471758108 31.06746560545738 22.601057153094764 34.24357220627512
9 plataan 30.208627954766115 123.22600953975079 88.02246907749901 4.833302068080008 20.980674302352114 19.054660495972172

View File

@ -4,24 +4,13 @@ import cv2
import os import os
import math import math
def calcWeightedStd(values, weights): def calcNormalFunc(mean : float, sd : float, len : int):
N = len(values)
x_bar = np.average(values, weights=weights, axis=0)
numerator = 0
for i in range(N):
numerator += weights[i] * ((values[i] - x_bar) ** 2)
denominator = np.sum(weights)
return math.sqrt(numerator / denominator)
def calcNormalFunc(mean, sd, len):
f = np.zeros(len, dtype=np.longdouble) f = np.zeros(len, dtype=np.longdouble)
# calculate PDF
for x in range(len): for x in range(len):
exp = (-(x - mean) ** 2)/(2 * sd ** 2) exp = (-(x - mean) ** 2)/(2 * sd ** 2)
f[x] = 1 / math.sqrt(2 * np.pi * sd** 20 ) * (math.exp(exp)) f[x] = 1 / math.sqrt(2 * np.pi * sd** 20 ) * (math.exp(exp))
# normalize PDF
max = np.amax(f) max = np.amax(f)
min = np.amin(f) min = np.amin(f)
for x in range(len): for x in range(len):
@ -29,70 +18,162 @@ def calcNormalFunc(mean, sd, len):
return f return f
## Constants ##
DATASET_PATH = "dataset\\" DATASET_PATH = "dataset\\"
CSV_PATH = "src\\experiments\\algorithms\\data\\"
BARK_TYPES = 8 BARK_TYPES = 8
## Arrays ##
avgs_bgr = [[[],[],[]] for x in range(BARK_TYPES)] avgs_bgr = [[[],[],[]] for x in range(BARK_TYPES)]
stds_bgr = [[[],[],[]] for x in range(BARK_TYPES)]
wgts_bgr = [[[],[],[]] for x in range(BARK_TYPES)] wgts_bgr = [[[],[],[]] for x in range(BARK_TYPES)]
avgs_hsv = [[[],[],[]] for x in range(BARK_TYPES)] avgs_hsv = [[[],[],[]] for x in range(BARK_TYPES)]
stds_hsv = [[[],[],[]] for x in range(BARK_TYPES)]
wgts_hsv = [[[],[],[]] for x in range(BARK_TYPES)] wgts_hsv = [[[],[],[]] for x in range(BARK_TYPES)]
labels = ["" for x in range(BARK_TYPES)] labels = ["" for x in range(BARK_TYPES)]
## Data extraction ##
i = -1 i = -1
last_name = "" last_name = ""
for file in os.listdir(DATASET_PATH): for file in os.listdir(DATASET_PATH):
# Store name + iterate tree type #
name = file.split('_')[0] name = file.split('_')[0]
if(name != last_name): if(name != last_name):
last_name = name last_name = name
i += 1 i += 1
labels[i] = name labels[i] = name
# Load picture #
im_bgr = cv2.imread(os.path.join(DATASET_PATH + file), 1) im_bgr = cv2.imread(os.path.join(DATASET_PATH + file), 1)
assert im_bgr is not None, "Something went wrong" assert im_bgr is not None, "Something went wrong"
im_hsv = cv2.cvtColor(im_bgr, cv2.COLOR_BGR2HSV) im_hsv = cv2.cvtColor(im_bgr, cv2.COLOR_BGR2HSV)
# Data storing #
for j in range(3): for j in range(3):
avgs_bgr[i][j].append(np.mean(im_bgr[:, :, j])) avgs_bgr[i][j].append(np.mean(im_bgr[:, :, j]))
stds_bgr[i][j].append(np.std(im_bgr[:, :, j]))
wgts_bgr[i][j].append(len(im_bgr[:, :, j])) wgts_bgr[i][j].append(len(im_bgr[:, :, j]))
avgs_hsv[i][j].append(np.mean(im_hsv[:, :, j])) avgs_hsv[i][j].append(np.mean(im_hsv[:, :, j]))
stds_hsv[i][j].append(np.std(im_hsv[:, :, j]))
wgts_hsv[i][j].append(len(im_hsv[:, :, j])) wgts_hsv[i][j].append(len(im_hsv[:, :, j]))
## Arrays ##
w_avg_bgr = [[0, 0, 0] for x in range(BARK_TYPES)] w_avg_bgr = [[0, 0, 0] for x in range(BARK_TYPES)]
w_std_bgr = [[0, 0, 0] for x in range(BARK_TYPES)] w_std_bgr = [[0, 0, 0] for x in range(BARK_TYPES)]
w_std_bgr = [[0, 0, 0] for x in range(BARK_TYPES)]
w_avg_hsv = [[0, 0, 0] for x in range(BARK_TYPES)] w_avg_hsv = [[0, 0, 0] for x in range(BARK_TYPES)]
w_std_hsv = [[0, 0, 0] for x in range(BARK_TYPES)] w_std_hsv = [[0, 0, 0] for x in range(BARK_TYPES)]
w_std_hsv = [[0, 0, 0] for x in range(BARK_TYPES)]
#TODO: Mag de weighted std berekening met avgs? Denk t nie ## Statistics calculations ##
for i in range(BARK_TYPES): for i in range(BARK_TYPES):
for j in range(3): for j in range(3):
w_avg_bgr[i][j] = np.average(avgs_bgr[i][j], weights=wgts_bgr[i][j], axis=0) w_avg_bgr[i][j] = np.average(
w_std_bgr[i][j] = calcWeightedStd(avgs_bgr[i][j], wgts_bgr[i][j]) avgs_bgr[i][j],
weights=wgts_bgr[i][j],
w_avg_hsv[i][j] = np.average(avgs_hsv[i][j], weights=wgts_hsv[i][j], axis=0) axis=0)
w_std_hsv[i][j] = calcWeightedStd(avgs_hsv[i][j], wgts_hsv[i][j]) w_std_bgr[i][j] = np.average(
stds_bgr[i][j],
weights=wgts_bgr[i][j])
w_avg_hsv[i][j] = np.average(
avgs_hsv[i][j],
weights=wgts_hsv[i][j],
axis=0)
w_std_hsv[i][j] = np.average(
stds_hsv[i][j],
weights=wgts_hsv[i][j])
## Plots + Normal calculations ##
fig, axs = plt.subplots(2,4) fig, axs = plt.subplots(2,4)
row = 0 row = 0
col = -1 col = -1
colours = ('b', 'g', 'r') colours = ('b', 'g', 'r')
for i in range(BARK_TYPES): for i in range(BARK_TYPES):
# Rows and columns #
row = 1 if i > 3 else 0 row = 1 if i > 3 else 0
col += 1 col += 1
if i == 4: col = 0 if i == 4: col = 0
# Normal and plotting #
for j in range(3): for j in range(3):
f = calcNormalFunc(w_avg_bgr[i][j], w_std_bgr[i][j], 255) f = calcNormalFunc(w_avg_bgr[i][j], w_std_bgr[i][j], 255)
label = "$\mu:$ %d, $\sigma: %d$"%(w_avg_bgr[i][j],w_std_bgr[i][j]) label = "$\mu:$ %d, $\sigma: %d$"%(w_avg_bgr[i][j],w_std_bgr[i][j])
axs[row, col].plot(f, c=colours[j], label=label) axs[row, col].plot(f, c=colours[j], label=label)
# Plot params #
axs[row, col].grid() axs[row, col].grid()
axs[row, col].legend() axs[row, col].legend()
axs[row, col].set_title(str(labels[i])) axs[row, col].set_title(str(labels[i]))
plt.show(block=False)
fig, axs = plt.subplots(2,4)
row = 0
col = -1
for i in range(BARK_TYPES):
# Rows and columns #
row = 1 if i > 3 else 0
col += 1
if i == 4: col = 0
# Normal and plotting #
for j in range(3):
f = calcNormalFunc(w_avg_hsv[i][j], w_std_hsv[i][j], 255)
label = "$\mu:$ %d, $\sigma: %d$"%(w_avg_hsv[i][j], w_std_hsv[i][j])
axs[row, col].plot(f, label=label)
# Plot params #
axs[row, col].grid()
axs[row, col].legend()
axs[row, col].set_title(str(labels[i]))
plt.show() plt.show()
## CSVs ##
# BGR mean and std #
with open(CSV_PATH + "bgr_stats.csv", 'w', newline='') as file:
file.write("label,avg_b,avg_g,avg_r,std_b,std_g,std_r,\n")
for i in range(BARK_TYPES):
file.write(labels[i] + ',')
for j in range(3):
file.write(str(w_avg_bgr[i][j]) + ',')
for j in range(3):
file.write(str(w_std_bgr[i][j]) + ',')
file.write('\n')
# BGR normal curve per channel #
id = ('B', 'G', 'R')
with open(CSV_PATH + "bgr_curve.csv", 'w', newline='') as file:
for i in range(BARK_TYPES):
for j in range(3):
file.write(labels[i] + ',')
file.write(id[j] + ',')
f = calcNormalFunc(w_avg_bgr[i][j], w_std_bgr[i][j], 255)
for k in range(255):
file.write(str(f[k]) + ',')
file.write('\n')
# HSV mean and std #
with open(CSV_PATH + "hsv_stats.csv", 'w', newline='') as file:
file.write("label,avg_b,avg_g,avg_r,std_b,std_g,std_r,\n")
for i in range(BARK_TYPES):
file.write(labels[i] + ',')
for j in range(3):
file.write(str(w_avg_hsv[i][j]) + ',')
for j in range(3):
file.write(str(w_std_hsv[i][j]) + ',')
file.write('\n')
# BGR normal curve per channel #
id = ('H', 'S', 'V')
with open(CSV_PATH + "hsv_curve.csv", 'w', newline='') as file:
for i in range(BARK_TYPES):
for j in range(3):
file.write(labels[i] + ',')
file.write(id[j] + ',')
f = calcNormalFunc(w_avg_hsv[i][j], w_std_hsv[i][j], 255)
for k in range(255):
file.write(str(f[k]) + ',')
file.write('\n')

View File

@ -79,7 +79,7 @@ for folder in os.listdir(DATASET_PATH):
print("Done!") print("Done!")
## Pandas ## ## Pandas ##
with open(CSV_PATH + "data.csv" , 'w', newline='') as file: with open(CSV_PATH + "sift.csv" , 'w', newline='') as file:
for i in range(len(labels)): for i in range(len(labels)):
file.write(labels[i] + '\n') file.write(labels[i] + '\n')
for j in range(len(tot_magnitudes[i])): for j in range(len(tot_magnitudes[i])):