import cv2
import pywt
# Convert image to grayscale for processing
gray_image = rgb_image.convert("L")
gray_array = np.array(gray_image)
# 1. Fourier Transform (Magnitude Spectrum)
dft = np.fft.fft2(gray_array)
dft_shift = np.fft.fftshift(dft)
magnitude_spectrum = np.log(np.abs(dft_shift) + 1) # Log scale for visibility
# 2. Vector Representation (Edge Detection for a simplified contour-like output)
edges = cv2.Canny(gray_array, 100, 200)
# 3. Fractal Compression (Using simple downscaling & upscaling to mimic self-similarity)
downscale_factor = 8
small = cv2.resize(gray_array, (gray_array.shape[1] // downscale_factor, gray_array.shape[0] // downscale_factor), interpolation=cv2.INTER_AREA)
fractal_approx = cv2.resize(small, (gray_array.shape[1], gray_array.shape[0]), interpolation=cv2.INTER_CUBIC)
# 4. Wavelet Compression (Single-Level DWT)
coeffs2 = pywt.dwt2(gray_array, 'haar')
cA, (cH, cV, cD) = coeffs2 # Approximation, Horizontal, Vertical, Diagonal components
wavelet_approx = cv2.resize(cA, (gray_array.shape[1], gray_array.shape[0]), interpolation=cv2.INTER_CUBIC)
# Plot the transformations
fig, ax = plt.subplots(2, 2, figsize=(12, 12))
ax[0, 0].imshow(magnitude_spectrum, cmap='gray')
ax[0, 0].set_title("Fourier Transform (Magnitude Spectrum)")
ax[0, 0].axis("off")
ax[0, 1].imshow(edges, cmap='gray')
ax[0, 1].set_title("Vector Representation (Edges)")
ax[0, 1].axis("off")
ax[1, 0].imshow(fractal_approx, cmap='gray')
ax[1, 0].set_title("Fractal Compression Approximation")
ax[1, 0].axis("off")
ax[1, 1].imshow(wavelet_approx, cmap='gray')
ax[1, 1].set_title("Wavelet Compression Approximation")
ax[1, 1].axis("off")
# Save the output visualization
output_transform_path = "/mnt/data/image_transformations.png"
plt.savefig(output_transform_path, bbox_inches="tight")
plt.show()
output_transform_path