{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os \n", "from glob import glob \n", "import pandas as pd\n", "import numpy as np\n", "\n", "from PIL import Image, ImageColor\n", "import matplotlib.pyplot as plt\n", "\n", "import torch\n", "\n", "from backend.disentangle_concepts import *\n", "import dnnlib \n", "import legacy\n", "from backend.color_annotations import *\n", "\n", "\n", "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "images_textiles = glob('/Users/ludovicaschaerf/Desktop/TextAIles/TextileGAN/Original Textiles/*')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "images = []\n", "\n", "for step in [10, 20]:\n", " imh = np.zeros((200, 200))\n", " imv = np.zeros((200, 200))\n", " imb = np.ones((200, 200))\n", " imb2 = np.ones((200, 200))\n", " \n", " for x,y in zip(range(0,200, step*2),range(step, 200, step*2)):\n", " imh[x:y, :] = 255\n", " imv[:, x:y] = 255\n", " imb[x:y, :] = 0\n", " imb[:, x:y] = 0\n", " imb2[x:y, :] = 0\n", " imb2[:, x*2:y*2] = 0\n", " \n", " images.append(imh) \n", " images.append(imb) \n", " images.append(imb2) \n", " images.append(imv) \n", "\n", "for im in images:\n", " plt.imshow(im, cmap='gray')\n", " plt.title('Original Image')\n", " plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_freqs(image):\n", " \n", " # Load the image\n", " # image = cv2.imread(image, cv2.IMREAD_GRAYSCALE)\n", " # Center crop the image to remove black borders\n", " height, width = image.shape\n", " short_side = min(height, width)\n", " crop_size = min(200, short_side)\n", " center_x = width // 2\n", " center_y = height // 2\n", " crop_half_size = crop_size // 2\n", " image = image[center_y - crop_half_size:center_y + crop_half_size,\n", " center_x - crop_half_size:center_x + crop_half_size]\n", "\n", " # kernel = np.ones((5,5), np.float32) / 25\n", " # image = cv2.filter2D( image, -1, kernel)\n", " # image = cv2.GaussianBlur(image,(5,5),0)\n", " # image = image - cv2.GaussianBlur(image, (21, 21), 1) + 127\n", "\n", " # print(np.unique(image))\n", " # Perform 1D Fourier Transforms\n", " horizontal_freq = np.fft.fftshift(np.fft.fft(image, axis=1))\n", " vertical_freq = np.fft.fftshift(np.fft.fft(image, axis=0))\n", " twod = np.fft.fftshift(np.fft.fft2(image))\n", " \n", " # Calculate corresponding frequencies\n", " num_cols = image.shape[1]\n", " num_rows = image.shape[0]\n", " # horizontal_freqs = np.fft.fftshift(np.fft.fftfreq(num_cols))\n", " # vertical_freqs = np.fft.fftshift(np.fft.fftfreq(num_rows))\n", " \n", " horizontal_freqs = np.fft.fftfreq(num_cols)\n", " vertical_freqs = np.fft.fftfreq(num_rows)\n", " \n", " # # Sum power along the second axis\n", " # twod = twod.real*twod.real + twod.imag*twod.imag\n", " # twod = twod.sum(axis=1)/twod.shape[1]\n", "\n", " # # Round up the size along this axis to an even number\n", " # n = int(math.ceil(image.shape[0] / 2.) * 2 )\n", "\n", " # # Generate a list of frequencies\n", " # f = np.fft.fftfreq(n)\n", "\n", " # # Graph it\n", " # plt.plot(f[1:],a[1:], label = 'sum of amplitudes over y vs f_x')\n", "\n", " \n", "\n", " # Calculate magnitude spectra\n", " horizontal_magnitudes = np.abs(horizontal_freq)[0]\n", " vertical_magnitudes = np.abs(vertical_freq)[0]\n", " \n", " # # Find peaks in the magnitudes\n", " # horizontal_peaks, _ = find_peaks(horizontal_magnitudes, height=100) # Adjust height threshold as needed\n", " # vertical_peaks, _ = find_peaks(vertical_magnitudes, height=10) # Adjust height threshold as needed\n", "\n", " print('Median horizontal frequency', np.median(horizontal_magnitudes), ', median vertical frequency', np.median(vertical_magnitudes))\n", " # Plot frequency analysis with peaks\n", " plt.figure(figsize=(25, 5))\n", "\n", " # Plot frequency analysis\n", " plt.subplot(1, 6, 1)\n", " plt.imshow(image, cmap='gray')\n", " plt.title('Original Image')\n", "\n", " plt.subplot(1, 6, 2)\n", " plt.imshow(np.log(1 + np.abs(horizontal_freq)), cmap='gray')\n", " plt.title('Horizontal Frequency Analysis')\n", "\n", " plt.subplot(1, 6, 3)\n", " plt.imshow(np.log(1 + np.abs(vertical_freq)), cmap='gray')\n", " plt.title('Vertical Frequency Analysis')\n", "\n", " plt.subplot(1, 6, 4)\n", " plt.imshow(np.log(1 + np.abs(twod)), cmap='gray')\n", " plt.title('2D Frequency Analysis')\n", "\n", " plt.subplot(1, 6, 5)\n", " plt.scatter(horizontal_freqs[1:], horizontal_magnitudes[1:], s=5)\n", " # plt.plot(horizontal_freqs[horizontal_peaks], horizontal_magnitudes[horizontal_peaks], 'ro', markersize=5)\n", " plt.xlabel('Horizontal Frequency')\n", " plt.ylabel('Magnitude')\n", " plt.title('Horizontal Frequency Analysis with Peaks')\n", "\n", " plt.subplot(1, 6, 6)\n", " plt.scatter(vertical_freqs[1:], vertical_magnitudes[1:], s=5)\n", " # plt.plot(vertical_freqs[vertical_peaks], vertical_magnitudes[vertical_peaks], 'ro', markersize=5)\n", " plt.xlabel('Vertical Frequency')\n", " plt.ylabel('Magnitude')\n", " plt.title('Vertical Frequency Analysis with Peaks')\n", "\n", "\n", " plt.show()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When the absolute value of the frequency is high in the context of Fourier analysis, it indicates the presence of a rapidly changing or repeating pattern in the image. The frequency component's absolute value reflects how many cycles of the pattern occur within a fixed interval.\n", "\n", "In Fourier analysis:\n", "\n", "Higher Frequency Components: Higher absolute frequency values correspond to faster changes or repetitions in the image data. This means that the pattern or feature represented by that frequency component oscillates more rapidly across the image.\n", "\n", "Spatial Frequency: In image analysis, frequency is often associated with how quickly the intensity or color changes as you move across the image. High absolute frequency values indicate rapid changes or transitions in the image content.\n", "\n", "Fine Details and Textures: Rapidly oscillating frequency components are associated with fine details and textures in the image. For example, in textiles, high-frequency components might capture the intricate weave patterns or small-scale textures.\n", "\n", "Small-Scale Features: Patterns that repeat at small scales, such as fine lines or tiny structures, tend to result in high-frequency components with high absolute values.\n", "\n", "Edges and Transitions: Edges and sharp transitions between different colors or intensities are also associated with high-frequency components. These transitions involve rapid changes in intensity, leading to higher absolute frequency values." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import cv2\n", "from scipy.signal import find_peaks\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "\n", "for image in images:\n", " get_freqs(image)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import cv2\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from scipy.signal import find_peaks\n", "from collections import Counter\n", "import seaborn as sns\n", "# Load the image\n", "image = cv2.imread(images_textiles[0], cv2.IMREAD_GRAYSCALE)\n", "\n", "# Perform 1D Fourier Transforms\n", "horizontal_freq = np.fft.fftshift(np.fft.fft(image, axis=1))\n", "vertical_freq = np.fft.fftshift(np.fft.fft(image, axis=0))\n", "\n", "# Calculate magnitude spectra\n", "horizontal_magnitudes = np.abs(horizontal_freq)[0]\n", "vertical_magnitudes = np.abs(vertical_freq)[0]\n", "\n", "# Create histograms of magnitude recurrence\n", "horizontal_magnitude_counter = Counter(np.round(horizontal_magnitudes).astype(int))\n", "vertical_magnitude_counter = Counter(np.round(vertical_magnitudes).astype(int))\n", "\n", "# Plot magnitude recurrence histograms\n", "plt.figure(figsize=(12, 5))\n", "\n", "plt.subplot(1, 2, 1)\n", "sns.histplot(list(horizontal_magnitude_counter.elements()), kde=True, color='blue')\n", "plt.xlim(0, 1000) # Limit x-axis range\n", "plt.xlabel('Magnitude')\n", "plt.ylabel('Recurrence')\n", "plt.title('Horizontal Magnitude Recurrence')\n", "\n", "plt.subplot(1, 2, 2)\n", "sns.histplot(list(vertical_magnitude_counter.elements()), kde=True, color='green')\n", "plt.xlim(0, 1000) # Limit x-axis range\n", "plt.xlabel('Magnitude')\n", "plt.ylabel('Recurrence')\n", "plt.title('Vertical Magnitude Recurrence')\n", "\n", "plt.tight_layout()\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import cv2\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from skimage.feature import hog\n", "from skimage import exposure\n", "\n", "# Load the image\n", "image = cv2.imread(images_textiles[4], cv2.IMREAD_GRAYSCALE)\n", "\n", "# Center crop the image to remove black borders\n", "height, width = image.shape\n", "crop_size = 200\n", "center_x = width // 2\n", "center_y = height // 2\n", "crop_half_size = crop_size // 2\n", "cropped_image = image[center_y - crop_half_size:center_y + crop_half_size,\n", " center_x - crop_half_size:center_x + crop_half_size]\n", "\n", "# Compute HOG features\n", "orientations = 9\n", "pixels_per_cell = (5, 5)\n", "cells_per_block = (1,1)\n", "hog_features, hog_image = hog(cropped_image, orientations=orientations,\n", " pixels_per_cell=pixels_per_cell,\n", " cells_per_block=cells_per_block,\n", " block_norm='L2-Hys',\n", " visualize=True)\n", "\n", "# Plot the HOG image\n", "plt.figure(figsize=(8, 4))\n", "plt.subplot(1, 2, 1)\n", "plt.imshow(cropped_image, cmap='gray')\n", "plt.title('Original Image')\n", "\n", "plt.subplot(1, 2, 2)\n", "plt.imshow(hog_image, cmap='gray')\n", "plt.title('HOG Image')\n", "plt.axis('off')\n", "\n", "plt.tight_layout()\n", "plt.show()\n", "\n", "# Plot the orientation distribution\n", "hog_histogram, _ = np.histogram(hog_features, bins=orientations)\n", "angles_deg = np.arange(0, 180, 180/orientations)\n", "plt.figure(figsize=(6, 4))\n", "plt.bar(angles_deg, hog_histogram, width=180/orientations)\n", "plt.xlabel('Orientation (degrees)')\n", "plt.ylabel('Frequency')\n", "plt.title('Orientation Distribution')\n", "plt.xticks(angles_deg)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import cv2\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import pywt\n", "\n", "# Load the image\n", "image = cv2.imread(images_textiles[0], cv2.IMREAD_GRAYSCALE)\n", "\n", "# Perform Continuous Wavelet Transform (CWT) on each row of the image\n", "wavelet = 'morl' # You can choose a different wavelet basis\n", "scales = np.arange(1, 20) # Scales to analyze\n", "\n", "cwt_coeffs = []\n", "for row in image:\n", " coeffs, _ = pywt.cwt(row, scales, wavelet)\n", " cwt_coeffs.append(coeffs)\n", "\n", "cwt_coeffs = np.array(cwt_coeffs)\n", "\n", "# Plot scaleograms\n", "plt.figure(figsize=(10, 6))\n", "plt.imshow(np.abs(cwt_coeffs[:, 0, :]), extent=[0, image.shape[1], scales[-1], scales[0]], cmap='viridis', aspect='auto')\n", "plt.colorbar(label='Magnitude')\n", "plt.title('Wavelet Scaleogram')\n", "plt.xlabel('Pixel')\n", "plt.ylabel('Scale')\n", "plt.show()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "art-reco_x86", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.16" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }