1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Mon May 25 18:51:53 2020
4
+
5
+ @author: cosmi
6
+ """
7
+
8
+ #Image stitching (Panorama Maker DNG Batch Version)"
9
+
10
+
11
+ import os
12
+ import rawpy
13
+ import imageio
14
+ import cv2
15
+ import numpy as np
16
+
17
+ ## Image Processing libraries
18
+ from skimage import exposure
19
+
20
+ import matplotlib .pyplot as plt
21
+ from matplotlib import colors
22
+ from matplotlib import ticker
23
+ from matplotlib .colors import LinearSegmentedColormap
24
+
25
+
26
+ images = []
27
+ for infile in os .listdir ("./" ):
28
+ print ( "file : " + infile )
29
+ if infile [- 3 :] == "tif" or infile [- 3 :] == "DNG" :
30
+ # print "is tif or DNG (RAW)"
31
+ outfile = infile [:- 3 ] + "jpg"
32
+
33
+ print ( "new filename : " + outfile )
34
+ dim = (640 ,360 )
35
+ raw = rawpy .imread (infile )
36
+ # Postprocessing, i.e demosaicing here, will always
37
+ #change the original pixel values. Typically what you want
38
+ # is to get a linearly postprocessed image so that roughly
39
+ #the number of photons are in linear relation to the pixel values.
40
+ #You can do that with:
41
+
42
+ rgb = raw .postprocess ()
43
+
44
+ rgb = cv2 .resize (rgb ,dim ,interpolation = cv2 .INTER_AREA )
45
+ rgb = cv2 .bitwise_not (~ rgb )
46
+
47
+
48
+ images .append (rgb )
49
+ #Read the images from your directory
50
+
51
+
52
+
53
+ #stitcher = cv2.createStitcher()
54
+ stitcher = cv2 .Stitcher .create ()
55
+ ret ,pano = stitcher .stitch (images )
56
+
57
+ if ret == cv2 .STITCHER_OK :
58
+
59
+ #need to swap colors here, demosaicing algoithm in rawpy jumbles to colors for some reason?:
60
+ truepano = cv2 .cvtColor (pano ,cv2 .COLOR_BGR2RGB )
61
+
62
+ #Apply gamma corrections: gamma values greater than 1 will shift the image histogram towards left and the output image will be darker than the input image. On the other hand, for gamma values less than 1, the histogram will shift towards right and the output image will be brighter than the input image.
63
+
64
+
65
+ gamma_corrected_pano = exposure .adjust_gamma (truepano , gamma = 1 , gain = 0.5 )
66
+
67
+
68
+ panoimage = gamma_corrected_pano
69
+
70
+ #apply histogram equalization
71
+ #using skimage (easy way)
72
+ hist_equalized_pano = exposure .equalize_hist (panoimage )
73
+
74
+
75
+ panoramaoutfile = "panoresult.png"
76
+
77
+
78
+ imageio .imsave (panoramaoutfile , pano )
79
+
80
+
81
+ cv2 .imshow ('Panorama' ,hist_equalized_pano )
82
+ cv2 .waitKey ()
83
+ cv2 .destroyAllWindows ()
84
+ else :
85
+ print ("Error during Stitching" )
86
+
87
+
88
+ # Open an image
89
+ image = pano
90
+
91
+ # Get the red band from the rgb image, and open it as a numpy matrix
92
+ #NIR = image[:, :, 0]
93
+
94
+ #ir = np.asarray(NIR, float)
95
+
96
+
97
+ ir = (image [:,:,0 ]).astype ('float' )
98
+
99
+
100
+ # Get one of the IR image bands (all bands should be same)
101
+ #blue = image[:, :, 2]
102
+
103
+ #r = np.asarray(blue, float)
104
+
105
+ r = (image [:,:,2 ]).astype ('float' )
106
+
107
+
108
+ # Create a numpy matrix of zeros to hold the calculated NDVI values for each pixel
109
+ ndvi = np .zeros (r .size ) # The NDVI image will be the same size as the input image
110
+
111
+ # Calculate NDVI
112
+ ndvi = np .true_divide (np .subtract (ir , r ), np .add (ir , r ))
113
+
114
+
115
+ # Display the results
116
+ output_name = 'InfraBlueNDVI3.jpg'
117
+
118
+ #a nice selection of grayscale colour palettes
119
+ cols1 = ['blue' , 'green' , 'yellow' , 'red' ]
120
+ cols2 = ['gray' , 'gray' , 'red' , 'yellow' , 'green' ]
121
+ cols3 = ['gray' , 'blue' , 'green' , 'yellow' , 'red' ]
122
+
123
+ cols4 = ['black' , 'gray' , 'blue' , 'green' , 'yellow' , 'red' ]
124
+
125
+ def create_colormap (args ):
126
+ return LinearSegmentedColormap .from_list (name = 'custom1' , colors = cols3 )
127
+
128
+ #colour bar to match grayscale units
129
+ def create_colorbar (fig , image ):
130
+ position = fig .add_axes ([0.125 , 0.19 , 0.2 , 0.05 ])
131
+ norm = colors .Normalize (vmin = - 1. , vmax = 1. )
132
+ cbar = plt .colorbar (image ,
133
+ cax = position ,
134
+ orientation = 'horizontal' ,
135
+ norm = norm )
136
+ cbar .ax .tick_params (labelsize = 6 )
137
+ tick_locator = ticker .MaxNLocator (nbins = 3 )
138
+ cbar .locator = tick_locator
139
+ cbar .update_ticks ()
140
+ cbar .set_label ("NDVI" , fontsize = 10 , x = 0.5 , y = 0.5 , labelpad = - 25 )
141
+
142
+ fig , ax = plt .subplots ()
143
+ image = ax .imshow (ndvi , cmap = create_colormap (colors ))
144
+ plt .axis ('off' )
145
+
146
+ create_colorbar (fig , image )
147
+
148
+ extent = ax .get_window_extent ().transformed (fig .dpi_scale_trans .inverted ())
149
+ fig .savefig (output_name , dpi = 600 , transparent = True , bbox_inches = extent , pad_inches = 0 )
150
+ # plt.show()
151
+
152
+
0 commit comments