pyosv.plot.plot

  1import matplotlib.pyplot as plt
  2import numpy as np
  3
  4def plot(img : np.ndarray, hist : bool = False) -> None:
  5    '''
  6        Plot a satellite image and its histogram.
  7
  8        Only gray-scale or RGB-like are accepted.
  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            - hist : bool
 15                if True display also the histogram (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        ) 
 35
 36        # Adding fake axis
 37        img = img[:,:,None]
 38
 39        plot(img, hist=True)
 40
 41        ```
 42
 43        Output:
 44        -------
 45        Nothing, it will display the image
 46    '''
 47    
 48
 49    if len(img.shape) != 3:
 50        raise Exception('Error: lenght of image shape must be 3 - (space, space, channels)')
 51
 52    if ((img.shape[-1] != 1) and (img.shape[-1] != 3)):
 53        raise Exception("Error: numer of channels admitted are 1 or 3")
 54
 55
 56    ncols = 1
 57    if hist: ncols = 2
 58    
 59    fig, axes = plt.subplots(nrows = 1, ncols = ncols, figsize = (4*ncols, 4))
 60
 61    if hist:
 62        axes[0].imshow(img)
 63        axes[0].set_title('Image')
 64        for b in range(img.shape[-1]):
 65            axes[1].hist(img[:,:,b].flatten(), 200, label='Band {}'.format(b))
 66        axes[1].legend()
 67        axes[1].set_title('Histogram')
 68    else:
 69        axes.imshow(img)
 70        axes.set_title('Image')
 71        
 72    fig.tight_layout()
 73    plt.show()
 74
 75
 76def bands_plot(img : np.ndarray, hist : bool = False) -> None:
 77    '''
 78        Plot a satellite image bands and relative histograms.
 79
 80        Parameters:
 81        ----------
 82            - img : np.ndarray
 83                a WxHxB image, with width W, height H and B bands
 84            - hist : bool
 85                if True display also the histograms (dafault : False)
 86        
 87        Returns:
 88        --------
 89        Nothing, it will display the image
 90
 91        Usage:
 92        ------
 93
 94        ```python
 95        import numpy as np
 96
 97        img         = np.array(  
 98            [[
 99                [0.1, 0.2, 0.3],  
100                [0.4, 0.5, 0.6],  
101                [0.7, 0.8, 0.9]
102             ],
103             [
104                [1.1, 1.2, 1.3],  
105                [1.4, 1.5, 1.6],  
106                [1.7, 1.8, 1.9]
107             ]
108            ]  
109        ) 
110
111        # Adding fake axis
112        img = np.moveaxis(img, 0, -1)
113
114        bands_plot(img, hist=True)
115
116        ```
117
118        Output:
119        -------
120        Nothing, it will display the image
121    '''
122    
123    if len(img.shape) != 3:
124        raise Exception('Error: lenght of image shape must be 3 - (space, space, channels)')
125    
126    ncols = img.shape[-1]
127    nrows = 1
128    if hist: nrows = 2
129    
130    fig, axes = plt.subplots(nrows = nrows, ncols = ncols, figsize = (4*ncols, 4*nrows))
131
132    for c in range(ncols):
133        
134        if hist:
135            axes[0,c].imshow(img[:,:,c])
136            axes[0,c].set_title('Bands {}'.format(c))
137                
138            axes[1,c].hist(img[:,:,c].flatten(), 200)
139            axes[1,c].set_title('Histogram of Band {}'.format(c))
140        else:
141            axes[c].imshow(img[:,:,c])
142            axes[c].set_title('Band {}'.format(c))
143
144    fig.tight_layout()
145    plt.show()
def plot(img: numpy.ndarray, hist: bool = False) -> None:
 5def plot(img : np.ndarray, hist : bool = False) -> None:
 6    '''
 7        Plot a satellite image and its histogram.
 8
 9        Only gray-scale or RGB-like are accepted.
10
11        Parameters:
12        ----------
13            - img : np.ndarray
14                a WxHxB image, with width W, height H and B bands (B can be 3 or 1)
15            - hist : bool
16                if True display also the histogram (dafault : False)
17        
18        Returns:
19        --------
20        Nothing, it will display the image
21
22        Usage:
23        ------
24
25        ```python
26        import numpy as np
27
28        img         = np.array(  
29            [[
30                [0.1, 0.2, 0.3],  
31                [0.4, 0.5, 0.6],  
32                [0.7, 0.8, 0.9]
33             ]
34            ]  
35        ) 
36
37        # Adding fake axis
38        img = img[:,:,None]
39
40        plot(img, hist=True)
41
42        ```
43
44        Output:
45        -------
46        Nothing, it will display the image
47    '''
48    
49
50    if len(img.shape) != 3:
51        raise Exception('Error: lenght of image shape must be 3 - (space, space, channels)')
52
53    if ((img.shape[-1] != 1) and (img.shape[-1] != 3)):
54        raise Exception("Error: numer of channels admitted are 1 or 3")
55
56
57    ncols = 1
58    if hist: ncols = 2
59    
60    fig, axes = plt.subplots(nrows = 1, ncols = ncols, figsize = (4*ncols, 4))
61
62    if hist:
63        axes[0].imshow(img)
64        axes[0].set_title('Image')
65        for b in range(img.shape[-1]):
66            axes[1].hist(img[:,:,b].flatten(), 200, label='Band {}'.format(b))
67        axes[1].legend()
68        axes[1].set_title('Histogram')
69    else:
70        axes.imshow(img)
71        axes.set_title('Image')
72        
73    fig.tight_layout()
74    plt.show()

Plot a satellite image and its histogram.

Only gray-scale or RGB-like are accepted.

Parameters:

- img : np.ndarray
    a WxHxB image, with width W, height H and B bands (B can be 3 or 1)
- hist : bool
    if True display also the histogram (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]
     ]
    ]  
) 

# Adding fake axis
img = img[:,:,None]

plot(img, hist=True)

Output:

Nothing, it will display the image

def bands_plot(img: numpy.ndarray, hist: bool = False) -> None:
 77def bands_plot(img : np.ndarray, hist : bool = False) -> None:
 78    '''
 79        Plot a satellite image bands and relative histograms.
 80
 81        Parameters:
 82        ----------
 83            - img : np.ndarray
 84                a WxHxB image, with width W, height H and B bands
 85            - hist : bool
 86                if True display also the histograms (dafault : False)
 87        
 88        Returns:
 89        --------
 90        Nothing, it will display the image
 91
 92        Usage:
 93        ------
 94
 95        ```python
 96        import numpy as np
 97
 98        img         = np.array(  
 99            [[
100                [0.1, 0.2, 0.3],  
101                [0.4, 0.5, 0.6],  
102                [0.7, 0.8, 0.9]
103             ],
104             [
105                [1.1, 1.2, 1.3],  
106                [1.4, 1.5, 1.6],  
107                [1.7, 1.8, 1.9]
108             ]
109            ]  
110        ) 
111
112        # Adding fake axis
113        img = np.moveaxis(img, 0, -1)
114
115        bands_plot(img, hist=True)
116
117        ```
118
119        Output:
120        -------
121        Nothing, it will display the image
122    '''
123    
124    if len(img.shape) != 3:
125        raise Exception('Error: lenght of image shape must be 3 - (space, space, channels)')
126    
127    ncols = img.shape[-1]
128    nrows = 1
129    if hist: nrows = 2
130    
131    fig, axes = plt.subplots(nrows = nrows, ncols = ncols, figsize = (4*ncols, 4*nrows))
132
133    for c in range(ncols):
134        
135        if hist:
136            axes[0,c].imshow(img[:,:,c])
137            axes[0,c].set_title('Bands {}'.format(c))
138                
139            axes[1,c].hist(img[:,:,c].flatten(), 200)
140            axes[1,c].set_title('Histogram of Band {}'.format(c))
141        else:
142            axes[c].imshow(img[:,:,c])
143            axes[c].set_title('Band {}'.format(c))
144
145    fig.tight_layout()
146    plt.show()

Plot a satellite image bands and relative histograms.

Parameters:

- img : np.ndarray
    a WxHxB image, with width W, height H and B bands
- hist : bool
    if True display also the histograms (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]
     ],
     [
        [1.1, 1.2, 1.3],  
        [1.4, 1.5, 1.6],  
        [1.7, 1.8, 1.9]
     ]
    ]  
) 

# Adding fake axis
img = np.moveaxis(img, 0, -1)

bands_plot(img, hist=True)

Output:

Nothing, it will display the image