File size: 13,798 Bytes
52bd88d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
{
 "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
}