World Foundation Models
  • Home
  • Projeto

Referências

  • Diffusion models
  • Autoregressive models
  • Tokens
    • Cosmos Tokenizer
      • Cosmos Tokenizer
      • Decomposição de imagem com Wavelets
        • Importando pacotes e imagens
        • Decomposição de um nível
        • Decomposição de dois níveis
        • Referências
    • DSC Tokenization
  • Cosmos Applications
World Foundation Models
  • Referências
  • Tokens
  • Cosmos Tokenizer
  • Decomposição de imagem com Wavelets

Decomposição de imagem com Wavelets¶

Importando pacotes e imagens¶

In [1]:
Copied!
# Importando pacotes
import os
import pywt
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.image import imread
# Importando pacotes import os import pywt import numpy as np import matplotlib.pyplot as plt from matplotlib.image import imread
In [2]:
Copied!
# Importando imagem
ronaldinho = imread(
    os.path.join('..', 'images', 'cosmos_tokenizer', 'example_images', 'ronaldinho.jpg')
)
cat_on_snow = imread(
    os.path.join(
        '..', 'images', 'cosmos_tokenizer', 'example_images', 'cat_on_snow.png'
    )
)
# Importando imagem ronaldinho = imread( os.path.join('..', 'images', 'cosmos_tokenizer', 'example_images', 'ronaldinho.jpg') ) cat_on_snow = imread( os.path.join( '..', 'images', 'cosmos_tokenizer', 'example_images', 'cat_on_snow.png' ) )

Decomposição de um nível¶

In [3]:
Copied!
# Decomposição Wavelet
n = 1
w = 'haar'

# Armazenando coeficientes para reconstrução
arr_rgb = []

for c in range(3):
    channel = ronaldinho[..., c]
    coeffs = pywt.wavedec2(channel, wavelet=w, level=n)

    # Normalizando coeficientes para visualização
    coeffs_norm = list(coeffs)
    coeffs_norm[0] /= np.abs(coeffs_norm[0]).max()
    for i in range(1, n + 1):
        coeffs_norm[i] = tuple(
            d / np.abs(d).max() if np.abs(d).max() != 0 else d for d in coeffs_norm[i]
        )
    arr, coeff_slices = pywt.coeffs_to_array(coeffs_norm)
    arr_rgb.append(arr)
# Decomposição Wavelet n = 1 w = 'haar' # Armazenando coeficientes para reconstrução arr_rgb = [] for c in range(3): channel = ronaldinho[..., c] coeffs = pywt.wavedec2(channel, wavelet=w, level=n) # Normalizando coeficientes para visualização coeffs_norm = list(coeffs) coeffs_norm[0] /= np.abs(coeffs_norm[0]).max() for i in range(1, n + 1): coeffs_norm[i] = tuple( d / np.abs(d).max() if np.abs(d).max() != 0 else d for d in coeffs_norm[i] ) arr, coeff_slices = pywt.coeffs_to_array(coeffs_norm) arr_rgb.append(arr)
In [4]:
Copied!
# Reconstruindo imagem
arr_rgb = np.stack(arr_rgb, axis=-1)

# Visualizando imagem
plt.figure(figsize=(16, 16))
plt.imshow(np.clip(arr_rgb, 0, 1))

plt.show()
# Reconstruindo imagem arr_rgb = np.stack(arr_rgb, axis=-1) # Visualizando imagem plt.figure(figsize=(16, 16)) plt.imshow(np.clip(arr_rgb, 0, 1)) plt.show()
No description has been provided for this image

Decomposição de dois níveis¶

In [5]:
Copied!
# Decomposição Wavelet
n = 2
w = 'haar'

# Armazenando coeficientes para reconstrução
arr_rgb = []

for c in range(3):
    channel = cat_on_snow[..., c]
    coeffs = pywt.wavedec2(channel, wavelet=w, level=n)

    # Normalizando coeficientes para visualização
    coeffs_norm = list(coeffs)
    coeffs_norm[0] /= np.abs(coeffs_norm[0]).max()
    for i in range(1, n + 1):
        coeffs_norm[i] = tuple(
            d / np.abs(d).max() if np.abs(d).max() != 0 else d for d in coeffs_norm[i]
        )
    arr, coeff_slices = pywt.coeffs_to_array(coeffs_norm)
    arr_rgb.append(arr)
# Decomposição Wavelet n = 2 w = 'haar' # Armazenando coeficientes para reconstrução arr_rgb = [] for c in range(3): channel = cat_on_snow[..., c] coeffs = pywt.wavedec2(channel, wavelet=w, level=n) # Normalizando coeficientes para visualização coeffs_norm = list(coeffs) coeffs_norm[0] /= np.abs(coeffs_norm[0]).max() for i in range(1, n + 1): coeffs_norm[i] = tuple( d / np.abs(d).max() if np.abs(d).max() != 0 else d for d in coeffs_norm[i] ) arr, coeff_slices = pywt.coeffs_to_array(coeffs_norm) arr_rgb.append(arr)
In [6]:
Copied!
# Reconstruindo imagem
arr_rgb = np.stack(arr_rgb, axis=-1)

# Visualizando imagem
plt.figure(figsize=(16, 16))
plt.imshow(np.clip(arr_rgb, 0, 1))

plt.show()
# Reconstruindo imagem arr_rgb = np.stack(arr_rgb, axis=-1) # Visualizando imagem plt.figure(figsize=(16, 16)) plt.imshow(np.clip(arr_rgb, 0, 1)) plt.show()
No description has been provided for this image

Referências¶

  • Data Driven Science & Engineering: Machine Learning, Dynamical Systems, and Control by S. L. Brunton and J. N. Kutz Cambridge Textbook, 2019 Copyright 2019, All Rights Reserved http://databookuw.com/
    • github - dynamicslab/databook_python - Capítulo 2 Seção 6 - Decomposição de imagem com Wavelets
    • github - dynamicslab/databook_python - Capítulo 2 Seção 6 - Compressão de imagem com Wavelets
Previous Next

Built with MkDocs using a theme provided by Read the Docs.
« Previous Next »