1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Mon May 25 02:34:05 2020
4
+
5
+ @author: cosmi
6
+ """
7
+
8
+
9
+ """
10
+ Experimental Vegetation Index Mapping program using DJI Mavic 2 Pro
11
+ JPEG 16-bit combo images taken using InfraBlue Filter
12
+
13
+ %(c)-J. Campbell MuonRay Enterprises 2019
14
+ This Python script was created using the Spyder Editor
15
+
16
+ """
17
+
18
+ import warnings
19
+ warnings .filterwarnings ('ignore' )
20
+ from scipy import misc
21
+ import imageio
22
+ import numpy as np
23
+ from matplotlib import pyplot as plt # For image viewing
24
+
25
+ #!/usr/bin/python
26
+ import getopt
27
+ import sys
28
+
29
+ import matplotlib .pyplot as plt
30
+ from matplotlib import colors
31
+ from matplotlib import ticker
32
+ from matplotlib .colors import LinearSegmentedColormap
33
+
34
+ #dng reading requires libraw to work
35
+
36
+ # Open an image
37
+ image = misc .imread ('PANO0003.jpg' )
38
+
39
+ # Get the red band from the rgb image, and open it as a numpy matrix
40
+ #NIR = image[:, :, 0]
41
+
42
+ #ir = np.asarray(NIR, float)
43
+
44
+
45
+ ir = (image [:,:,0 ]).astype ('float' )
46
+
47
+
48
+ # Get one of the IR image bands (all bands should be same)
49
+ #blue = image[:, :, 2]
50
+
51
+ #r = np.asarray(blue, float)
52
+
53
+ r = (image [:,:,2 ]).astype ('float' )
54
+
55
+
56
+ # Create a numpy matrix of zeros to hold the calculated NDVI values for each pixel
57
+ ndvi = np .zeros (r .size ) # The NDVI image will be the same size as the input image
58
+
59
+ # Calculate NDVI
60
+ ndvi = np .true_divide (np .subtract (ir , r ), np .add (ir , r ))
61
+
62
+
63
+ # Display the results
64
+ output_name = 'InfraBlueNDVI3.jpg'
65
+
66
+ #a nice selection of grayscale colour palettes
67
+ cols1 = ['blue' , 'green' , 'yellow' , 'red' ]
68
+ cols2 = ['gray' , 'gray' , 'red' , 'yellow' , 'green' ]
69
+ cols3 = ['gray' , 'blue' , 'green' , 'yellow' , 'red' ]
70
+
71
+ cols4 = ['black' , 'gray' , 'blue' , 'green' , 'yellow' , 'red' ]
72
+
73
+ def create_colormap (args ):
74
+ return LinearSegmentedColormap .from_list (name = 'custom1' , colors = cols3 )
75
+
76
+ #colour bar to match grayscale units
77
+ def create_colorbar (fig , image ):
78
+ position = fig .add_axes ([0.125 , 0.19 , 0.2 , 0.05 ])
79
+ norm = colors .Normalize (vmin = - 1. , vmax = 1. )
80
+ cbar = plt .colorbar (image ,
81
+ cax = position ,
82
+ orientation = 'horizontal' ,
83
+ norm = norm )
84
+ cbar .ax .tick_params (labelsize = 6 )
85
+ tick_locator = ticker .MaxNLocator (nbins = 3 )
86
+ cbar .locator = tick_locator
87
+ cbar .update_ticks ()
88
+ cbar .set_label ("NDVI" , fontsize = 10 , x = 0.5 , y = 0.5 , labelpad = - 25 )
89
+
90
+ fig , ax = plt .subplots ()
91
+ image = ax .imshow (ndvi , cmap = create_colormap (colors ))
92
+ plt .axis ('off' )
93
+
94
+ create_colorbar (fig , image )
95
+
96
+ extent = ax .get_window_extent ().transformed (fig .dpi_scale_trans .inverted ())
97
+ fig .savefig (output_name , dpi = 600 , transparent = True , bbox_inches = extent , pad_inches = 0 )
98
+ # plt.show()
0 commit comments