-
Notifications
You must be signed in to change notification settings - Fork 0
/
vegetations_sample.rb
59 lines (47 loc) · 1.77 KB
/
vegetations_sample.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'pycall/import'
include PyCall::Import
pyimport :cv2
pyimport :numpy, as: :np
image_path = "sliding_window/window_143.jpeg"
image = cv2.imread(image_path)
blue, green, red = cv2.split(image)
blue = blue.astype(np.float32)
green = green.astype(np.float32)
red = red.astype(np.float32)
# 1. Green Leaf Index (GLI)
gli = (2 * green - red - blue) / (2 * green + red + blue + 1e-5)
# 2. Excess Green Index (ExG)
exg = 2 * green - red - blue
# 3. Visible Atmospherically Resistant Index (VARI)
vari = (green - red) / (green + red - blue + 1e-5)
# 4. Normalized Green-Red Difference Index (NGRDI)
ngrdi = (green - red) / (green + red + 1e-5)
# 5. Excess Red Index (ExR)
exr = 1.4 * red - green
# 6. Combined Greenness Index (COM)
com = exg - exr
# 7. Triangular Greenness Index (TGI)
tgi = -0.5 * (190 * (red - green) - 120 * (red - blue))
# 8. Color Index of Vegetation Extraction (CIVE)
cive = 0.441 * red - 0.811 * green + 0.385 * blue + 18.787
# 9. Vegetative Index (VEG)
veg = green / ((red ** 0.667) * (blue ** 0.333) + 1e-5)
# Function to normalize and save each index
def normalize_and_save(index, name)
normalized = cv2.normalize(index, nil, 0, 255, cv2.NORM_MINMAX)
normalized = normalized.astype(np.uint8)
normalized = cv2.applyColorMap(normalized, cv2.COLORMAP_JET)
cv2.imwrite("processed_images/#{name}.jpeg", normalized)
puts "#{name} index saved as processed_images/#{name}.jpeg"
end
# Normalize and save each vegetation index
normalize_and_save(gli, "GLI")
normalize_and_save(exg, "ExG")
normalize_and_save(vari, "VARI")
normalize_and_save(ngrdi, "NGRDI")
normalize_and_save(exr, "ExR")
normalize_and_save(com, "COM")
normalize_and_save(tgi, "TGI")
normalize_and_save(cive, "CIVE")
normalize_and_save(veg, "VEG")
puts "All vegetation indices have been calculated and saved."