-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspec2img.py
49 lines (39 loc) · 1.65 KB
/
spec2img.py
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
import numpy as np
import os
from PIL import Image
from pyts.image import GramianAngularField, MarkovTransitionField
def Normalize_img(normal_gasd_arr):
norm_im = normal_gasd_arr[0, :, :]
norm_im = ((norm_im + 1) / 2 * 255).astype(np.uint8)
return norm_im
def spec2img(spec, output_path='img.jpeg', image_size=224):
"""
Converts a 1D spectrum to a 2D image using MTF, GAFS, and GAMD techniques.
Parameters:
spec (array_like): The input 1D spectrum.
output_path (str): The path where the output image will be saved. Default is 'img.jpeg'.
image_size (int): The size of the output image. Default is 224.
Returns:
None: The function saves the 2D image at the specified output path.
"""
try:
spec = np.array(spec)
# MTF
mtf = MarkovTransitionField(image_size=image_size)
X_mtf = mtf.fit_transform(spec.reshape(1, -1))
# GAFS
gafs = GramianAngularField(image_size=image_size, method='summation')
X_gafs = gafs.fit_transform(spec.reshape(1, -1))
# GAMD
gafd = GramianAngularField(image_size=image_size, method='difference')
X_gafd = gafd.fit_transform(spec.reshape(1, -1))
im0 = Normalize_img(X_gafd)
im1 = Normalize_img(X_gafs)
im2 = Normalize_img(X_mtf)
im = np.concatenate([im0[:, :, np.newaxis], im1[:, :, np.newaxis], im2[:, :, np.newaxis]], axis=2)
im = Image.fromarray(im)
im.save(output_path)
except Exception as e:
print(f"An error occurred: {e}")
# Example usage:
# spec2img(my_spectrum, output_path='output_image.jpeg', image_size=224)