-
Notifications
You must be signed in to change notification settings - Fork 0
/
NDVIquaddisplayonly.py
124 lines (82 loc) · 3.14 KB
/
NDVIquaddisplayonly.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#the program displays an R/G/B/NDVI quad video.
#NDVI equations from https://github.com/robintw/RPiNDVI/blob/master/ndvi.py
import time
import numpy as np
import cv2
import picamera
import picamera.array
def disp_multiple(im1=None, im2=None, im3=None, im4=None):
"""
Combines four images for display.
"""
height, width = im1.shape
combined = np.zeros((2 * height, 2 * width, 3), dtype=np.uint8)
combined[0:height, 0:width, :] = cv2.cvtColor(im1, cv2.COLOR_GRAY2RGB)
combined[height:, 0:width, :] = cv2.cvtColor(im2, cv2.COLOR_GRAY2RGB)
combined[0:height, width:, :] = cv2.cvtColor(im3, cv2.COLOR_GRAY2RGB)
combined[height:, width:, :] = im4
return combined
def label(image, text):
"""
Labels the given image with the given text
"""
return cv2.putText(image, text, (0, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (255,255,255),4)
def contrast_stretch(im):
"""
Performs a simple contrast stretch of the given image, from 5-95%.
"""
in_min = np.percentile(im, 5)
in_max = np.percentile(im, 95)
out_min = 0.0
out_max = 255.0
out = im - in_min
out *= ((out_min - out_max) / (in_min - in_max))
out += in_min
return out
def run():
with picamera.PiCamera() as camera:
# Set the camera resolution
x = 400
camera.resolution = (int(1.33 * x), x)
# Various optional camera settings below:
# camera.framerate = 5
# camera.awb_mode = 'off'
# camera.awb_gains = (0.5, 0.5)
# Need to sleep to give the camera time to get set up properly
time.sleep(1)
with picamera.array.PiRGBArray(camera) as stream:
# Loop constantly
while True:
# Grab data from the camera, in colour format
# NOTE: This comes in BGR rather than RGB, which is important
# for later!
camera.capture(stream, format='bgr', use_video_port=True)
image = stream.array
# Get the individual colour components of the image
b, g, r = cv2.split(image)
# Calculate the NDVI
# Bottom of fraction
bottom = (r.astype(float) + b.astype(float))
bottom[bottom == 0] = 0.01 # Make sure we don't divide by zero!
ndvi = (r.astype(float) - b) / bottom
ndvi = contrast_stretch(ndvi)
ndvi = ndvi.astype(np.uint8)
ndvi = cv2.applyColorMap(ndvi, cv2.COLORMAP_JET)
# Do the labelling
label(b, 'Blue')
label(g, 'Green')
label(r, 'NIR')
label(ndvi, 'NDVI')
# Combine ready for display
combined = disp_multiple(b, g, r, ndvi)
# Display
cv2.imshow('image', combined)
stream.truncate(0)
# If we press ESC then break out of the loop
c = cv2.waitKey(7) % 0x100
if c == 27:
break
# clean up or things will get messy
cv2.destroyAllWindows()
if __name__ == '__main__':
run()