pyosv.plot.cube

  1from mayavi import mlab
  2import numpy as np
  3
  4
  5def plot3d(img : np.ndarray, animate : bool = False) -> None:
  6    '''
  7        3D-Plot of a satellite image.
  8
  9        Parameters:
 10        ----------
 11            - img : np.ndarray
 12                a WxHxB image, with width W, height H and B bands (B can be 3 or 1)
 13            - animate : bool
 14                activate animation mode (dafault : False)
 15        
 16        Returns:
 17        --------
 18        Nothing, it will display the image
 19
 20        Usage:
 21        ------
 22
 23        ```python
 24        import numpy as np
 25
 26        img         = np.array(  
 27            [[
 28                [0.1, 0.2, 0.3],  
 29                [0.4, 0.5, 0.6],  
 30                [0.7, 0.8, 0.9]
 31             ],
 32             [
 33                [0.9, 0.8, 0.7],  
 34                [0.6, 0.5, 0.4],  
 35                [0.3, 0.2, 0.1]
 36             ]
 37            ]  
 38        ) 
 39
 40        # Making channel last
 41        img = np.moveaxis(img, 0, -1)
 42
 43        plot3d(img, animate=True)
 44
 45        ```
 46
 47        Output:
 48        -------
 49        Nothing, it will display the image
 50
 51    '''
 52
 53    if len(img.shape) != 3:
 54        raise Exception('Error: lenght of image shape must be 3 - (space, space, channels)')
 55
 56    fig = mlab.figure(figure='', bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(600, 600))
 57    v = mlab.volume_slice(img, slice_index=0, plane_orientation='z_axes', figure=fig)  # depth slice
 58
 59    if animate:
 60        @mlab.animate
 61        def anim():
 62            ct = 0
 63            while True:
 64                if ct >= img.shape[-1]:
 65                    ct = 0
 66                v.mlab_source.scalars = img[:, :, ct]
 67                ct += 1
 68                yield
 69        anim()
 70    mlab.show()
 71
 72
 73def cube_plot(img : np.ndarray, save_path : str, band_thickness : int = 3, cmap : str = 'jet', show : bool = True) -> None:
 74    '''
 75        Cube plot of a satellite image.
 76
 77        Parameters:
 78        ----------
 79            - img : np.ndarray
 80                a WxHxB image, with width W, height H and B bands (B can be 3 or 1)
 81            - save_path : str
 82                path where to save figure
 83            - band_thickness : int
 84                thikness of each satellite band
 85            - cmap : str
 86                colormap for the plot
 87            - show : bool
 88                if True will show the plot, othewise will be only saved to a file
 89        
 90        Returns:
 91        --------
 92        Nothing, it will display the image
 93
 94        Usage:
 95        ------
 96
 97        ```python
 98        import numpy as np
 99
100        img         = np.array(  
101            [[
102                [0.1, 0.2, 0.3],  
103                [0.4, 0.5, 0.6],  
104                [0.7, 0.8, 0.9]
105             ],
106             [
107                [0.9, 0.8, 0.7],  
108                [0.6, 0.5, 0.4],  
109                [0.3, 0.2, 0.1]
110             ]
111            ]  
112        ) 
113
114        # Making channel last
115        img = np.moveaxis(img, 0, -1)
116
117        cube_plot(img, 'img.png', band_thickness=10, cmap='jet', show=True)
118
119        ```
120
121        Output:
122        -------
123        Nothing, it will display the image
124
125    '''
126    
127    if len(img.shape) != 3: raise Exception("Error: lenght of img shape must be 3")
128
129
130    img = np.repeat(img, band_thickness, axis=-1)
131
132    fig = mlab.figure(figure='', bgcolor=(1,1,1), fgcolor=(0, 0, 0), size=(600, 600))
133
134    scalars = img  # specifying the data array
135
136    # Crossline slices
137    mlab.volume_slice(scalars, slice_index=0, plane_orientation='x_axes', figure=fig, colormap=cmap)  # crossline slice
138    mlab.volume_slice(scalars, slice_index=img.shape[0], plane_orientation='x_axes', figure=fig, colormap=cmap)  # crossline slice
139
140    # Inline slices
141    mlab.volume_slice(scalars, slice_index=0, plane_orientation='y_axes', figure=fig, colormap=cmap)  # inline slice
142    mlab.volume_slice(scalars, slice_index=img.shape[1], plane_orientation='y_axes', figure=fig, colormap=cmap)  # inline slice
143
144    # Depth slices
145    mlab.volume_slice(scalars, slice_index=img.shape[-1], plane_orientation='z_axes', figure=fig, colormap=cmap)  # depth slice
146    mlab.volume_slice(scalars, slice_index=0, plane_orientation='z_axes', figure=fig, colormap=cmap)  # depth slice
147
148
149    mlab.draw()
150    if save_path is not None: mlab.savefig(filename=save_path)
151    
152    if show: mlab.show()
153    else: 
154        mlab.clf()
155        mlab.close(scene=None, all=True)
156
157
158def stacked_cube_plot(imgs : list, save_path : str, cmaps : list, bands_thickness : list, spacing : int = 10, show : bool = True) -> None:
159    '''
160        Cubes plot of a satellite image.
161
162        Parameters:
163        ----------
164            - imgs : list 
165                list of WxHxB image (ndarray), with width W, height H and B bands (B can be 3 or 1)
166            - save_path : str
167                path where to save figure
168            - cmaps : list
169                list of colormap for the plot
170            - bands_thickness : list
171                list of int representing thickness of each satellite band
172            - spacing : int
173                add empty space between cubes
174            - show : bool
175                if True will show the plot, othewise will be only saved to a file
176        
177        Returns:
178        --------
179        Nothing, it will display the image
180
181        Usage:
182        ------
183
184        ```python
185        import numpy as np
186
187        img_1         = np.array(  
188            [[
189                [0.1, 0.2, 0.3],  
190                [0.4, 0.5, 0.6],  
191                [0.7, 0.8, 0.9]
192             ],
193             [
194                [0.9, 0.8, 0.7],  
195                [0.6, 0.5, 0.4],  
196                [0.3, 0.2, 0.1]
197             ]
198            ]  
199        )
200
201        img_2         = np.array(  
202            [[
203                [0.1, 0.2, 0.3],  
204                [0.4, 0.5, 0.6],  
205                [0.7, 0.8, 0.9]
206             ],
207             [
208                [0.9, 0.8, 0.7],  
209                [0.6, 0.5, 0.4],  
210                [0.3, 0.2, 0.1]
211             ]
212            ]  
213        ) 
214
215        # Making channel last
216        img_1 = np.moveaxis(img_1, 0, -1)
217        img_2 = np.moveaxis(img_2, 0, -1)
218
219
220        stacked_cube_plot([img_1, img_2], 'img.png', band_thickness=[10,10], spacing=10, cmap='jet', show=True)
221
222        ```
223
224        Output:
225        -------
226        Nothing, it will display the image
227
228    '''
229
230    z0 = 0
231    
232    fig = mlab.figure(figure='', bgcolor=(1,1,1), fgcolor=(0, 0, 0), size=(600, 600))
233    
234    for i, img in enumerate(imgs):
235
236        if len(img.shape) != 3: raise Exception("Error: lenght of img shape must be 3")
237
238        img = np.repeat(img, bands_thickness[i], axis=-1)
239        scalars = img  # specifying the data array
240
241        if i == 0: z1 = img.shape[-1]
242
243        x, y, z = np.mgrid[0:img.shape[0]:img.shape[0]*1j, 0:img.shape[1]:img.shape[1]*1j, z0:z1:img.shape[-1]*1j]
244
245        # Crossline slices
246        mlab.volume_slice(x,y,z,scalars, slice_index=0, plane_orientation='x_axes', figure=fig, colormap=cmaps[i])  # crossline slice
247        mlab.volume_slice(x,y,z,scalars, slice_index=img.shape[0], plane_orientation='x_axes', figure=fig, colormap=cmaps[i])  # crossline slice
248
249        # Inline slices
250        mlab.volume_slice(x,y,z,scalars, slice_index=0, plane_orientation='y_axes', figure=fig, colormap=cmaps[i])  # inline slice
251        mlab.volume_slice(x,y,z,scalars, slice_index=img.shape[1], plane_orientation='y_axes', figure=fig, colormap=cmaps[i])  # inline slice
252
253        # Depth slices
254        mlab.volume_slice(x,y,z,scalars, slice_index=img.shape[-1], plane_orientation='z_axes', figure=fig, colormap=cmaps[i])  # depth slice
255        mlab.volume_slice(x,y,z,scalars, slice_index=0, plane_orientation='z_axes', figure=fig, colormap=cmaps[i])  # depth slice
256
257        
258        z0 = z1 + spacing
259        z1 = z0 + img.shape[-1]
260
261    mlab.draw()
262    if save_path is not None: mlab.savefig(filename=save_path)
263    
264    if show: mlab.show()
265    else: 
266        mlab.clf()
267        mlab.close(scene=None, all=True)
268
269
270
271if __name__ == "__main__":
272    import numpy as np
273
274    img   = 0.5*np.ones((100,100,3))
275    img2  = np.ones((100,100,3))
276
277    
278
279    
280    stacked_cube_plot([img, img2, img, img2, img], None, cmaps=['jet','jet','jet', 'jet', 'jet'], bands_thickness=[50,10,10,10,1], spacing=30, show=True)
def plot3d(img: numpy.ndarray, animate: bool = False) -> None:
 6def plot3d(img : np.ndarray, animate : bool = False) -> None:
 7    '''
 8        3D-Plot of a satellite image.
 9
10        Parameters:
11        ----------
12            - img : np.ndarray
13                a WxHxB image, with width W, height H and B bands (B can be 3 or 1)
14            - animate : bool
15                activate animation mode (dafault : False)
16        
17        Returns:
18        --------
19        Nothing, it will display the image
20
21        Usage:
22        ------
23
24        ```python
25        import numpy as np
26
27        img         = np.array(  
28            [[
29                [0.1, 0.2, 0.3],  
30                [0.4, 0.5, 0.6],  
31                [0.7, 0.8, 0.9]
32             ],
33             [
34                [0.9, 0.8, 0.7],  
35                [0.6, 0.5, 0.4],  
36                [0.3, 0.2, 0.1]
37             ]
38            ]  
39        ) 
40
41        # Making channel last
42        img = np.moveaxis(img, 0, -1)
43
44        plot3d(img, animate=True)
45
46        ```
47
48        Output:
49        -------
50        Nothing, it will display the image
51
52    '''
53
54    if len(img.shape) != 3:
55        raise Exception('Error: lenght of image shape must be 3 - (space, space, channels)')
56
57    fig = mlab.figure(figure='', bgcolor=(1, 1, 1), fgcolor=(0, 0, 0), size=(600, 600))
58    v = mlab.volume_slice(img, slice_index=0, plane_orientation='z_axes', figure=fig)  # depth slice
59
60    if animate:
61        @mlab.animate
62        def anim():
63            ct = 0
64            while True:
65                if ct >= img.shape[-1]:
66                    ct = 0
67                v.mlab_source.scalars = img[:, :, ct]
68                ct += 1
69                yield
70        anim()
71    mlab.show()

3D-Plot of a satellite image.

Parameters:

- img : np.ndarray
    a WxHxB image, with width W, height H and B bands (B can be 3 or 1)
- animate : bool
    activate animation mode (dafault : False)

Returns:

Nothing, it will display the image

Usage:

import numpy as np

img         = np.array(  
    [[
        [0.1, 0.2, 0.3],  
        [0.4, 0.5, 0.6],  
        [0.7, 0.8, 0.9]
     ],
     [
        [0.9, 0.8, 0.7],  
        [0.6, 0.5, 0.4],  
        [0.3, 0.2, 0.1]
     ]
    ]  
) 

# Making channel last
img = np.moveaxis(img, 0, -1)

plot3d(img, animate=True)

Output:

Nothing, it will display the image

def cube_plot( img: numpy.ndarray, save_path: str, band_thickness: int = 3, cmap: str = 'jet', show: bool = True) -> None:
 74def cube_plot(img : np.ndarray, save_path : str, band_thickness : int = 3, cmap : str = 'jet', show : bool = True) -> None:
 75    '''
 76        Cube plot of a satellite image.
 77
 78        Parameters:
 79        ----------
 80            - img : np.ndarray
 81                a WxHxB image, with width W, height H and B bands (B can be 3 or 1)
 82            - save_path : str
 83                path where to save figure
 84            - band_thickness : int
 85                thikness of each satellite band
 86            - cmap : str
 87                colormap for the plot
 88            - show : bool
 89                if True will show the plot, othewise will be only saved to a file
 90        
 91        Returns:
 92        --------
 93        Nothing, it will display the image
 94
 95        Usage:
 96        ------
 97
 98        ```python
 99        import numpy as np
100
101        img         = np.array(  
102            [[
103                [0.1, 0.2, 0.3],  
104                [0.4, 0.5, 0.6],  
105                [0.7, 0.8, 0.9]
106             ],
107             [
108                [0.9, 0.8, 0.7],  
109                [0.6, 0.5, 0.4],  
110                [0.3, 0.2, 0.1]
111             ]
112            ]  
113        ) 
114
115        # Making channel last
116        img = np.moveaxis(img, 0, -1)
117
118        cube_plot(img, 'img.png', band_thickness=10, cmap='jet', show=True)
119
120        ```
121
122        Output:
123        -------
124        Nothing, it will display the image
125
126    '''
127    
128    if len(img.shape) != 3: raise Exception("Error: lenght of img shape must be 3")
129
130
131    img = np.repeat(img, band_thickness, axis=-1)
132
133    fig = mlab.figure(figure='', bgcolor=(1,1,1), fgcolor=(0, 0, 0), size=(600, 600))
134
135    scalars = img  # specifying the data array
136
137    # Crossline slices
138    mlab.volume_slice(scalars, slice_index=0, plane_orientation='x_axes', figure=fig, colormap=cmap)  # crossline slice
139    mlab.volume_slice(scalars, slice_index=img.shape[0], plane_orientation='x_axes', figure=fig, colormap=cmap)  # crossline slice
140
141    # Inline slices
142    mlab.volume_slice(scalars, slice_index=0, plane_orientation='y_axes', figure=fig, colormap=cmap)  # inline slice
143    mlab.volume_slice(scalars, slice_index=img.shape[1], plane_orientation='y_axes', figure=fig, colormap=cmap)  # inline slice
144
145    # Depth slices
146    mlab.volume_slice(scalars, slice_index=img.shape[-1], plane_orientation='z_axes', figure=fig, colormap=cmap)  # depth slice
147    mlab.volume_slice(scalars, slice_index=0, plane_orientation='z_axes', figure=fig, colormap=cmap)  # depth slice
148
149
150    mlab.draw()
151    if save_path is not None: mlab.savefig(filename=save_path)
152    
153    if show: mlab.show()
154    else: 
155        mlab.clf()
156        mlab.close(scene=None, all=True)

Cube plot of a satellite image.

Parameters:

- img : np.ndarray
    a WxHxB image, with width W, height H and B bands (B can be 3 or 1)
- save_path : str
    path where to save figure
- band_thickness : int
    thikness of each satellite band
- cmap : str
    colormap for the plot
- show : bool
    if True will show the plot, othewise will be only saved to a file

Returns:

Nothing, it will display the image

Usage:

import numpy as np

img         = np.array(  
    [[
        [0.1, 0.2, 0.3],  
        [0.4, 0.5, 0.6],  
        [0.7, 0.8, 0.9]
     ],
     [
        [0.9, 0.8, 0.7],  
        [0.6, 0.5, 0.4],  
        [0.3, 0.2, 0.1]
     ]
    ]  
) 

# Making channel last
img = np.moveaxis(img, 0, -1)

cube_plot(img, 'img.png', band_thickness=10, cmap='jet', show=True)

Output:

Nothing, it will display the image

def stacked_cube_plot( imgs: list, save_path: str, cmaps: list, bands_thickness: list, spacing: int = 10, show: bool = True) -> None:
159def stacked_cube_plot(imgs : list, save_path : str, cmaps : list, bands_thickness : list, spacing : int = 10, show : bool = True) -> None:
160    '''
161        Cubes plot of a satellite image.
162
163        Parameters:
164        ----------
165            - imgs : list 
166                list of WxHxB image (ndarray), with width W, height H and B bands (B can be 3 or 1)
167            - save_path : str
168                path where to save figure
169            - cmaps : list
170                list of colormap for the plot
171            - bands_thickness : list
172                list of int representing thickness of each satellite band
173            - spacing : int
174                add empty space between cubes
175            - show : bool
176                if True will show the plot, othewise will be only saved to a file
177        
178        Returns:
179        --------
180        Nothing, it will display the image
181
182        Usage:
183        ------
184
185        ```python
186        import numpy as np
187
188        img_1         = np.array(  
189            [[
190                [0.1, 0.2, 0.3],  
191                [0.4, 0.5, 0.6],  
192                [0.7, 0.8, 0.9]
193             ],
194             [
195                [0.9, 0.8, 0.7],  
196                [0.6, 0.5, 0.4],  
197                [0.3, 0.2, 0.1]
198             ]
199            ]  
200        )
201
202        img_2         = np.array(  
203            [[
204                [0.1, 0.2, 0.3],  
205                [0.4, 0.5, 0.6],  
206                [0.7, 0.8, 0.9]
207             ],
208             [
209                [0.9, 0.8, 0.7],  
210                [0.6, 0.5, 0.4],  
211                [0.3, 0.2, 0.1]
212             ]
213            ]  
214        ) 
215
216        # Making channel last
217        img_1 = np.moveaxis(img_1, 0, -1)
218        img_2 = np.moveaxis(img_2, 0, -1)
219
220
221        stacked_cube_plot([img_1, img_2], 'img.png', band_thickness=[10,10], spacing=10, cmap='jet', show=True)
222
223        ```
224
225        Output:
226        -------
227        Nothing, it will display the image
228
229    '''
230
231    z0 = 0
232    
233    fig = mlab.figure(figure='', bgcolor=(1,1,1), fgcolor=(0, 0, 0), size=(600, 600))
234    
235    for i, img in enumerate(imgs):
236
237        if len(img.shape) != 3: raise Exception("Error: lenght of img shape must be 3")
238
239        img = np.repeat(img, bands_thickness[i], axis=-1)
240        scalars = img  # specifying the data array
241
242        if i == 0: z1 = img.shape[-1]
243
244        x, y, z = np.mgrid[0:img.shape[0]:img.shape[0]*1j, 0:img.shape[1]:img.shape[1]*1j, z0:z1:img.shape[-1]*1j]
245
246        # Crossline slices
247        mlab.volume_slice(x,y,z,scalars, slice_index=0, plane_orientation='x_axes', figure=fig, colormap=cmaps[i])  # crossline slice
248        mlab.volume_slice(x,y,z,scalars, slice_index=img.shape[0], plane_orientation='x_axes', figure=fig, colormap=cmaps[i])  # crossline slice
249
250        # Inline slices
251        mlab.volume_slice(x,y,z,scalars, slice_index=0, plane_orientation='y_axes', figure=fig, colormap=cmaps[i])  # inline slice
252        mlab.volume_slice(x,y,z,scalars, slice_index=img.shape[1], plane_orientation='y_axes', figure=fig, colormap=cmaps[i])  # inline slice
253
254        # Depth slices
255        mlab.volume_slice(x,y,z,scalars, slice_index=img.shape[-1], plane_orientation='z_axes', figure=fig, colormap=cmaps[i])  # depth slice
256        mlab.volume_slice(x,y,z,scalars, slice_index=0, plane_orientation='z_axes', figure=fig, colormap=cmaps[i])  # depth slice
257
258        
259        z0 = z1 + spacing
260        z1 = z0 + img.shape[-1]
261
262    mlab.draw()
263    if save_path is not None: mlab.savefig(filename=save_path)
264    
265    if show: mlab.show()
266    else: 
267        mlab.clf()
268        mlab.close(scene=None, all=True)

Cubes plot of a satellite image.

Parameters:

- imgs : list 
    list of WxHxB image (ndarray), with width W, height H and B bands (B can be 3 or 1)
- save_path : str
    path where to save figure
- cmaps : list
    list of colormap for the plot
- bands_thickness : list
    list of int representing thickness of each satellite band
- spacing : int
    add empty space between cubes
- show : bool
    if True will show the plot, othewise will be only saved to a file

Returns:

Nothing, it will display the image

Usage:

import numpy as np

img_1         = np.array(  
    [[
        [0.1, 0.2, 0.3],  
        [0.4, 0.5, 0.6],  
        [0.7, 0.8, 0.9]
     ],
     [
        [0.9, 0.8, 0.7],  
        [0.6, 0.5, 0.4],  
        [0.3, 0.2, 0.1]
     ]
    ]  
)

img_2         = np.array(  
    [[
        [0.1, 0.2, 0.3],  
        [0.4, 0.5, 0.6],  
        [0.7, 0.8, 0.9]
     ],
     [
        [0.9, 0.8, 0.7],  
        [0.6, 0.5, 0.4],  
        [0.3, 0.2, 0.1]
     ]
    ]  
) 

# Making channel last
img_1 = np.moveaxis(img_1, 0, -1)
img_2 = np.moveaxis(img_2, 0, -1)


stacked_cube_plot([img_1, img_2], 'img.png', band_thickness=[10,10], spacing=10, cmap='jet', show=True)

Output:

Nothing, it will display the image