1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Mon Sep 20 10:37:45 2021
4
+
5
+ @author: cosmi
6
+ """
7
+
8
+
9
+ """
10
+ Experimental UV Absorption Index program using UV-Pass Filter on DJI Mavic 2 Pro
11
+ JPEG 16-bit combo images taken using Ultraviolet-Only Pass Filter
12
+ Useful for Batch Processing Multiple Images
13
+
14
+ %(c)-J. Campbell MuonRay Enterprises 2020
15
+ This Python script was created using the Spyder Editor
16
+ """
17
+ from scipy import misc
18
+
19
+ import warnings
20
+ warnings .filterwarnings ('ignore' )
21
+
22
+ from PIL import Image
23
+ import pylab
24
+ import rof
25
+
26
+ import imageio
27
+ import numpy as np
28
+ from matplotlib import pyplot as plt # For image viewing
29
+
30
+ #!/usr/bin/python
31
+ import getopt
32
+ import sys
33
+
34
+ import matplotlib .pyplot as plt
35
+ from matplotlib import colors
36
+ from matplotlib import ticker
37
+ from matplotlib .colors import LinearSegmentedColormap
38
+
39
+ #dng reading requires libraw to work
40
+
41
+ # Open an image
42
+ image = misc .imread ('DJI_0212.JPG' )
43
+
44
+ # Get the red band from the rgb image, and open it as a numpy matrix
45
+ #NIR = image[:, :, 0]
46
+
47
+ #ir = np.asarray(NIR, float)
48
+
49
+
50
+ ir = (image [:,:,0 ]).astype ('float' )
51
+
52
+
53
+ # Get one of the IR image bands (all bands should be same)
54
+ #blue = image[:, :, 2]
55
+
56
+ #r = np.asarray(blue, float)
57
+
58
+ r = (image [:,:,2 ]).astype ('float' )
59
+
60
+
61
+ g = (image [:,:,1 ]).astype ('float' )
62
+
63
+
64
+ ir = (image [:,:,0 ]).astype ('float' )
65
+
66
+ denoised_g_channel = g
67
+
68
+
69
+ """ negating noise in the output green channel using
70
+ an implementation of the Rudin-Osher-Fatemi (ROF) denoising model
71
+ using the numerical procedure presented in eq (11) A. Chambolle (2005).
72
+
73
+ The ROF model has the interesting property that it finds a smoother version
74
+ of the image while preserving edges and structures.
75
+
76
+
77
+ Input: noisy input image (grayscale), initial guess for U, weight of
78
+ the TV-regularizing term, steplength, tolerance for stop criterion.
79
+
80
+ Output: denoised and detextured image, texture residual. """
81
+
82
+ U ,T = rof .denoise (denoised_g_channel ,denoised_g_channel )
83
+
84
+ pylab .figure ()
85
+ pylab .gray ()
86
+ pylab .imshow (U )
87
+ pylab .axis ('equal' )
88
+ pylab .axis ('off' )
89
+ pylab .show ()
90
+
91
+
92
+ #(NIR + Green)
93
+ irg = np .add (ir , U )
94
+
95
+
96
+ L = 0.5 ;
97
+
98
+ rplusb = np .add (ir , r )
99
+ rplusbplusg = np .add (ir , r , U )
100
+ rminusb = np .subtract (ir , r )
101
+ oneplusL = np .add (1 , L )
102
+ # Create a numpy matrix of zeros to hold the calculated UVRI values for each pixel
103
+ uvri = np .zeros (r .size ) # The UVRI image will be the same size as the input image
104
+
105
+ # Calculate UV Reflectance Index
106
+
107
+ uvri = np .true_divide (np .subtract (U , rminusb ), np .add (U , rplusb ))
108
+
109
+
110
+ # Display the results
111
+ output_name = 'UVReflectanceIndex4.jpg'
112
+
113
+ #a nice selection of grayscale colour palettes
114
+ cols1 = ['blue' , 'green' , 'yellow' , 'red' ]
115
+ cols2 = ['gray' , 'gray' , 'red' , 'yellow' , 'green' ]
116
+ cols3 = ['gray' , 'blue' , 'green' , 'yellow' , 'red' ]
117
+
118
+ cols4 = ['black' , 'gray' , 'blue' , 'green' , 'yellow' , 'red' ]
119
+
120
+ def create_colormap (args ):
121
+ return LinearSegmentedColormap .from_list (name = 'custom1' , colors = cols4 )
122
+
123
+ #colour bar to match grayscale units
124
+ def create_colorbar (fig , image ):
125
+ position = fig .add_axes ([0.125 , 0.19 , 0.2 , 0.05 ])
126
+ norm = colors .Normalize (vmin = - 1. , vmax = 1. )
127
+ cbar = plt .colorbar (image ,
128
+ cax = position ,
129
+ orientation = 'horizontal' ,
130
+ norm = norm )
131
+ cbar .ax .tick_params (labelsize = 6 )
132
+ tick_locator = ticker .MaxNLocator (nbins = 3 )
133
+ cbar .locator = tick_locator
134
+ cbar .update_ticks ()
135
+ cbar .set_label ("UVRI" , fontsize = 10 , x = 0.5 , y = 0.5 , labelpad = - 25 )
136
+
137
+ fig , ax = plt .subplots ()
138
+ image = ax .imshow (uvri , cmap = create_colormap (colors ))
139
+ plt .axis ('off' )
140
+
141
+ create_colorbar (fig , image )
142
+
143
+ extent = ax .get_window_extent ().transformed (fig .dpi_scale_trans .inverted ())
144
+ fig .savefig (output_name , dpi = 600 , transparent = True , bbox_inches = extent , pad_inches = 0 )
145
+ # plt.show()
0 commit comments