File size: 5,936 Bytes
bd67cfe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import numpy as np
import matplotlib.pyplot as plt

def origin_plus_noise(X_hat: np.ndarray, X_noise: np.ndarray, X: np.ndarray, image_size: tuple, reduce: int, idx: int=2) -> None:
    """
    Display the original image, the noise, and the image with added noise side by side.
    
    Parameters:
    - X_hat (numpy.ndarray): Original image data.
    - X_noise (numpy.ndarray): Noise data to be added to the original image.
    - image_size (tuple): Size of the original image as (height, width).
    - reduce (int): Factor to downscale the image dimensions.
    - idx (int, optional): Index of the image to be displayed. Default is 2.
    """

    # Calculate reduced image size based on the 'reduce' factor
    img_size = [i//reduce for i in image_size]
    
    # Retrieve the specified image from the data
    X_hat_i = X_hat[:,idx].reshape(img_size[1],img_size[0])
    X_noise_i = X_noise[:,idx].reshape(img_size[1],img_size[0])
    X_i = X[:,idx].reshape(img_size[1],img_size[0])

    # Set up the figure for displaying images
    plt.figure(figsize=(12,3))  # Adjusted size for better visualization
    
    # Display the original image
    plt.subplot(151)  # Adjusted to 1x4 grid for space to '+' and '=' symbols
    plt.imshow(X_hat_i, cmap=plt.cm.gray)
    plt.title('Image(Original)')
    plt.axis('off')  # Hide axis for a cleaner look
    
    # Place '+' symbol between images
    plt.subplot(152)
    plt.text(0.5, 0.5, '+', fontsize=20, ha='center', va='center')
    plt.axis('off')  # Hide axis
    
    # Display the noise
    plt.subplot(153)
    plt.imshow(X_noise_i, cmap=plt.cm.gray)
    plt.title('Noise')
    plt.axis('off')  # Hide axis for a cleaner look
    
    # Place '=' symbol between images
    plt.subplot(154)
    plt.text(0.5, 0.5, '=', fontsize=20, ha='center', va='center')
    plt.axis('off')  # Hide axis
    
    # Display the image with added noise
    plt.subplot(155)
    plt.imshow(X_i, cmap=plt.cm.gray)
    plt.title('Image(Noise)')
    plt.axis('off')  # Hide axis for a cleaner look
    
    # Render the figure
    plt.tight_layout()  # Ensure no overlap between subplots
    plt.show()

def origin_versus_dictrep(X: np.ndarray, D: np.ndarray, R: np.ndarray, X_noise: np.ndarray, image_size: tuple, reduce: int, idx: int) -> None:
    """
    Display the original, noise-added, and dictionary-reconstructed images side by side.
    
    Parameters:
    - X (numpy.ndarray): Original data matrix of shape (n_samples, n_features).
    - D (numpy.ndarray): Basis matrix obtained from dictionary learning.
    - R (numpy.ndarray): Coefficient matrix.
    - X_noise (numpy.ndarray): Noise-added version of the original data matrix.
    - image_size (tuple): Tuple containing the height and width of the image.
    - reduce (int): Factor by which the image size is reduced for visualization.
    - idx (int): Index of the image to display.

    Returns:
    None. The function will plot and display the images using matplotlib.
    """

    DR = np.dot(D, R).reshape(X.shape[0], X.shape[1])
    # Calculate reduced image size based on the 'reduce' factor
    img_size = [i//reduce for i in image_size]
    
    # Retrieve the specified image from the data
    X_i = X[:,idx].reshape(img_size[1],img_size[0])
    X_noise_i = X_noise[:,idx].reshape(img_size[1],img_size[0])
    DR_i = DR[:,idx].reshape(img_size[1],img_size[0])

    # Set up the figure for displaying images
    plt.figure(figsize=(12,3))  # Adjusted size for better visualization

    # Display the original image
    plt.subplot(131)
    plt.imshow(X_i, cmap=plt.cm.gray)
    plt.title('Image(Original)')
    plt.axis('off')

    # Display the reconstructed image
    plt.subplot(132)
    plt.imshow(X_noise_i, cmap=plt.cm.gray)
    plt.title('Image(Noise)')
    plt.axis('off')

    # Display the sparse coefficients
    plt.subplot(133)
    plt.imshow(DR_i, cmap=plt.cm.gray)
    plt.title('Image(Reconstructed))')
    plt.axis('off')

    # Render the figure
    plt.tight_layout()
    plt.show()
    
    return X_i, X_noise_i, DR_i

def origin_noise_dictrep(X: np.ndarray, X_noise: np.ndarray, D: np.ndarray, R: np.ndarray, image_size: tuple, reduce: int, idx: int) -> None:
    """
    Display the original image, its noise version, and its dictionary-reconstructed representation side by side.
    
    Parameters:
    - X (numpy.ndarray): Original data matrix of shape (n_samples, n_features).
    - X_noise (numpy.ndarray): Noise-added version of the original data matrix.
    - D (numpy.ndarray): Basis matrix obtained from dictionary learning.
    - R (numpy.ndarray): Coefficient matrix.
    - image_size (tuple): Tuple containing the height and width of the image.
    - reduce (int): Factor by which the image size is reduced for visualization.
    - idx (int): Index of the image to display.

    Returns:
    None. The function will plot and display the images using matplotlib.
    """
    
    DR = np.dot(D, R).reshape(X.shape[0], X.shape[1])
    # Calculate reduced image size based on the 'reduce' factor
    img_size = [i//reduce for i in image_size]
    
    # Retrieve the specified image from the data
    X_i = X[:,idx].reshape(img_size[1],img_size[0])
    X_noise_i = X_noise[:,idx].reshape(img_size[1],img_size[0])
    DR_i = DR[:,idx].reshape(img_size[1],img_size[0])

    # Set up the figure for displaying images
    plt.figure(figsize=(12,3))  # Adjusted size for better visualization

    # Display the original image
    plt.subplot(131)
    plt.imshow(X_i, cmap=plt.cm.gray)
    plt.title('Image(Original)')
    plt.axis('off')

    # Display the noise
    plt.subplot(132)
    plt.imshow(X_noise_i, cmap=plt.cm.gray)
    plt.title('Image(Noise)')
    plt.axis('off')

    # Display the reconstructed image
    plt.subplot(133)
    plt.imshow(DR_i, cmap=plt.cm.gray)
    plt.title('Image(Reconstructed)')
    plt.axis('off')

    # Render the figure
    plt.tight_layout()
    plt.show()