pyosv.pre.hist_normalizer

  1from ..utils.mapper import mapFromTo
  2
  3
  4from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  5from tkinter import Tk, mainloop, Scale, HORIZONTAL
  6import matplotlib.pyplot as plt
  7import numpy as np
  8
  9
 10def hist_normalizer(img : np.ndarray) -> None:
 11    '''
 12        Open a gui that helps to stretch the histogram of an image
 13     
 14        Parameters:
 15        -----------
 16            - img : np.ndarray 
 17                a WxHxB image, with width W, height H and B bands
 18        
 19        Returns:
 20        --------
 21        Nothing, it will display and image
 22
 23        Usage:
 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                [0.1, 0.2, 0.3],  
 36                [0.4, 0.5, 0.6],  
 37                [0.7, 0.8, 0.9]
 38                ],
 39            [
 40                [0.1, 0.2, 0.3],  
 41                [0.4, 0.5, 0.6],  
 42                [0.7, 0.8, 0.9]
 43                ]
 44            ]  
 45        ) 
 46
 47        # Making channels last
 48        img = np.moveaxis(img, 0, -1)
 49
 50        hist_normalizer(img)
 51        ```
 52
 53        Output:
 54        -------
 55        Nothing, it will display an image
 56    '''
 57    
 58
 59    if len(img.shape) != 3:
 60        raise Exception('Error: lenght of image shape must be 3 - (space, space, channels)')
 61
 62    # Update plot
 63    def plot(img):
 64        # Clear axis
 65        axes[0].cla()
 66        axes[1].cla()
 67        
 68        # Plot image and histogram
 69        axes[0].set_title('Image')
 70        img = mapFromTo(img, wmin.get(), wmax.get(), 0, 1)
 71        axes[0].imshow(img)
 72    
 73        axes[1].set_title('Histogram')
 74        axes[1].set_xlabel('Mapped Pixel Values')
 75        axes[1].set_ylabel('Frequency')
 76    
 77        for k in range(img.shape[-1]):
 78            axes[1].hist(img[:,:,k].flatten(), 200, label = 'Band-{}'.format(k))   
 79            
 80        axes[1].legend()
 81        chart_type.draw()
 82    
 83    # Scale values using the sliders
 84    def scalevalue(value, other = img):
 85        image = np.clip(img, wmin.get(), wmax.get())    
 86        plot(image)
 87    
 88    figure, axes = plt.subplots(nrows = 1, ncols = 2, figsize=(10,5), dpi=100)
 89    
 90    # GUI
 91    root = Tk()
 92    root.title('Histrogram Scaler')
 93    #root.geometry('1000x620')
 94    #root.resizable(False, False)
 95    
 96    chart_type = FigureCanvasTkAgg(figure, root)
 97    chart_type.get_tk_widget().pack()
 98    
 99    wmin = Scale(root, from_=np.min(img), to=np.max(img), length = 400, label='Minimum', orient=HORIZONTAL, command = scalevalue)
100    wmin.set(np.min(img))
101    wmin.pack()
102    wmax = Scale(root, from_=np.min(img), to=np.max(img), length = 400, label='Maximum', orient=HORIZONTAL, command = scalevalue)
103    wmax.set(np.max(img))
104    wmax.pack()
105    
106    mainloop()
def hist_normalizer(img: numpy.ndarray) -> None:
 11def hist_normalizer(img : np.ndarray) -> None:
 12    '''
 13        Open a gui that helps to stretch the histogram of an image
 14     
 15        Parameters:
 16        -----------
 17            - img : np.ndarray 
 18                a WxHxB image, with width W, height H and B bands
 19        
 20        Returns:
 21        --------
 22        Nothing, it will display and image
 23
 24        Usage:
 25        ------
 26        ```python
 27        import numpy as np  
 28
 29        img         = np.array(  
 30            [[
 31                [0.1, 0.2, 0.3],  
 32                [0.4, 0.5, 0.6],  
 33                [0.7, 0.8, 0.9]
 34                ],
 35            [
 36                [0.1, 0.2, 0.3],  
 37                [0.4, 0.5, 0.6],  
 38                [0.7, 0.8, 0.9]
 39                ],
 40            [
 41                [0.1, 0.2, 0.3],  
 42                [0.4, 0.5, 0.6],  
 43                [0.7, 0.8, 0.9]
 44                ]
 45            ]  
 46        ) 
 47
 48        # Making channels last
 49        img = np.moveaxis(img, 0, -1)
 50
 51        hist_normalizer(img)
 52        ```
 53
 54        Output:
 55        -------
 56        Nothing, it will display an image
 57    '''
 58    
 59
 60    if len(img.shape) != 3:
 61        raise Exception('Error: lenght of image shape must be 3 - (space, space, channels)')
 62
 63    # Update plot
 64    def plot(img):
 65        # Clear axis
 66        axes[0].cla()
 67        axes[1].cla()
 68        
 69        # Plot image and histogram
 70        axes[0].set_title('Image')
 71        img = mapFromTo(img, wmin.get(), wmax.get(), 0, 1)
 72        axes[0].imshow(img)
 73    
 74        axes[1].set_title('Histogram')
 75        axes[1].set_xlabel('Mapped Pixel Values')
 76        axes[1].set_ylabel('Frequency')
 77    
 78        for k in range(img.shape[-1]):
 79            axes[1].hist(img[:,:,k].flatten(), 200, label = 'Band-{}'.format(k))   
 80            
 81        axes[1].legend()
 82        chart_type.draw()
 83    
 84    # Scale values using the sliders
 85    def scalevalue(value, other = img):
 86        image = np.clip(img, wmin.get(), wmax.get())    
 87        plot(image)
 88    
 89    figure, axes = plt.subplots(nrows = 1, ncols = 2, figsize=(10,5), dpi=100)
 90    
 91    # GUI
 92    root = Tk()
 93    root.title('Histrogram Scaler')
 94    #root.geometry('1000x620')
 95    #root.resizable(False, False)
 96    
 97    chart_type = FigureCanvasTkAgg(figure, root)
 98    chart_type.get_tk_widget().pack()
 99    
100    wmin = Scale(root, from_=np.min(img), to=np.max(img), length = 400, label='Minimum', orient=HORIZONTAL, command = scalevalue)
101    wmin.set(np.min(img))
102    wmin.pack()
103    wmax = Scale(root, from_=np.min(img), to=np.max(img), length = 400, label='Maximum', orient=HORIZONTAL, command = scalevalue)
104    wmax.set(np.max(img))
105    wmax.pack()
106    
107    mainloop()

Open a gui that helps to stretch the histogram of an image

Parameters:

- img : np.ndarray 
    a WxHxB image, with width W, height H and B bands

Returns:

Nothing, it will display and 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.1, 0.2, 0.3],  
        [0.4, 0.5, 0.6],  
        [0.7, 0.8, 0.9]
        ],
    [
        [0.1, 0.2, 0.3],  
        [0.4, 0.5, 0.6],  
        [0.7, 0.8, 0.9]
        ]
    ]  
) 

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

hist_normalizer(img)

Output:

Nothing, it will display an image