Advertisement
mirosh111000

ImageCompressor

Nov 16th, 2023
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. from sklearn.decomposition import TruncatedSVD
  4. import matplotlib.pyplot as plt
  5. import matplotlib.image as mpimg
  6.  
  7.  
  8.  
  9. class ImageCompressor:
  10.     def __init__(self, n_components):
  11.         self.n_components = n_components
  12.         self.svd = TruncatedSVD(n_components=self.n_components)
  13.  
  14.     def fit_transform(self, img):
  15.         img_red = img[:, :, 0]
  16.         img_green = img[:, :, 1]
  17.         img_blue = img[:, :, 2]
  18.  
  19.         components_red = self.svd.fit_transform(img_red)
  20.         compressed_red = np.dot(components_red, self.svd.components_)
  21.  
  22.         components_green = self.svd.fit_transform(img_green)
  23.         compressed_green = np.dot(components_green, self.svd.components_)
  24.  
  25.         components_blue = self.svd.fit_transform(img_blue)
  26.         compressed_blue = np.dot(components_blue, self.svd.components_)
  27.  
  28.         compressed_red = compressed_red.astype(int)
  29.         compressed_blue = compressed_blue.astype(int)
  30.         compressed_green = compressed_green.astype(int)
  31.  
  32.         compressed_array = np.stack((compressed_red, compressed_green, compressed_blue), axis=2)
  33.         compressed_array = np.clip(compressed_array, 0, 255)
  34.  
  35.         return compressed_array
  36.  
  37.  
  38.  
  39. img = mpimg.imread('IMG_2844.jpg')
  40. df = img[:, :, 0]
  41. img0 = plt.imshow(df, cmap='grey')
  42. plt.title('Оригінальне зображення')
  43. plt.show()
  44.  
  45. svd = TruncatedSVD(n_components=df.shape[1])
  46. svd.fit(df)
  47.  
  48. eigenvalues = svd.singular_values_
  49.  
  50. plt.bar(range(1, 21), eigenvalues[:20])
  51. plt.xlabel('Номер власного значення')
  52. plt.ylabel('Власне значення')
  53. plt.title('Гістограма перших 20 власних значень')
  54. plt.show()
  55.  
  56. explained_variance_ratio = svd.explained_variance_ratio_
  57. cumulative_explained_variance = np.cumsum(explained_variance_ratio)
  58.  
  59. plt.plot(range(1, len(cumulative_explained_variance) + 1), 100 - cumulative_explained_variance * 100, marker='o')
  60. plt.xlabel('Кількість використаних компонент')
  61. plt.ylabel('Втрата інформації, %')
  62. plt.show()
  63.  
  64. n_components_to_plot = [1, 3, 5, 10, 15, 20]  
  65.  
  66. fig, axs = plt.subplots(2, 3, figsize=(15, 10))
  67. axs = axs.ravel()
  68.  
  69. for i, n in enumerate(n_components_to_plot):
  70.     svd = TruncatedSVD(n_components=n)
  71.     components = svd.fit_transform(df)
  72.     reconstructed_df = pd.DataFrame(np.dot(components, svd.components_))
  73.     axs[i].imshow(reconstructed_df, cmap="gray")
  74.     axs[i].set_title(f'Кількість компонент = {n}')
  75.  
  76. plt.show()
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. plt.title('Оригінальне зображення')
  84. plt.imshow(img)
  85. plt.show()
  86.  
  87. n_components_to_plot = [1, 3, 5, 10, 15, 20]
  88.  
  89. fig, axs = plt.subplots(2, 3, figsize=(15, 10))
  90. axs = axs.ravel()
  91.  
  92. for i, n in enumerate(n_components_to_plot):
  93.     compressor = ImageCompressor(n_components=n)
  94.     reconstructed_img = compressor.fit_transform(img)
  95.     axs[i].imshow(reconstructed_img)
  96.     axs[i].set_title(f'Кількість компонент = {n}')
  97.  
  98. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement