-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutilities.py
209 lines (146 loc) · 4.85 KB
/
utilities.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
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
'''
Utility script
This script contains all functions necessary to run mainloop.py and simulation_OffAxis.py
Python Version: 3.8.18
numpy version: 1.24.3
Author: Maria Paula Rey*, Raul Castañeda**
Applied Sciences and Engineering School, EAFIT University (Applied Optics Group)
Email: *[email protected] , **[email protected]
Date last modified: 17/10/2024
'''
# import libraries
import numpy as np
from PIL import Image, ImageOps
from matplotlib import pyplot as plt
from numpy.fft import fft2, ifft2, fftshift, ifftshift
import imageio.v2 as imageio
import cv2 as cv
# Function to binarize an image
def binarize(im):
"""
Convert an image to binary using a specified threshold.
Parameters:
im : The input image to binarize.
Returns:
im : The binarized image
"""
im2=im.convert("L")
threshold = 100
im = im2.point(lambda p: p > threshold and 255)
return im
# Function to read an image
def imageRead(namefile, size):
"""
Read an image from a file, convert it to grayscale, and resize it.
Parameters:
namefile : Path to the image file (str).
size : The new size for the image (width and height) (int).
Returns:
loadImage : The resized grayscale image.
"""
# inputs:
# namefile - direction image to read
imagen = Image.open(namefile)
loadImage = ImageOps.grayscale(imagen)
loadImage = loadImage.resize((size, size))
return loadImage
# Function to display an Image
def imageShow(image, title):
"""
Display an image with a specified title.
Parameters:
image : The image to display.
title : The title for the displayed image (str).
"""
plt.imshow(image, cmap='gray'), plt.title(title)
plt.show()
return
# Function to compute the Intensity of a given complex field
def intensity(complexField, log):
"""
Compute the intensity of a given complex field.
Parameters:
- complexField : The input complex field.
- log : If True, compute the log representation of the intensity (bool).
Returns:
- out : The computed intensity (and log if specified).
"""
out = np.abs(complexField)
out = out * out
if log == True:
out = 20 * np.log(out)
out[out == np.inf] = 0
out[out == -np.inf] = 0
return out
# Function to compute the Fourier Transform
def ft(field):
"""
Compute the Fourier Transform of a field.
Parameters:
- field : The input field for the Fourier Transform.
Returns:
- ft : The computed Fourier Transform.
"""
ft = np.fft.fft2(field)
ft = np.fft.fftshift(ft)
return ft
# Function to compute the inverse Fourier Transform
def ift(field):
"""
Compute the Inverse Fourier Transform of a field.
Parameters:
- field : The input field for the Inverse Fourier Transform.
Returns:
- ift : The computed Inverse Fourier Transform. numpy.ndarray
"""
ift = np.fft.ifft2(field)
ift = np.fft.fftshift(ift)
ift = np.fft.fftshift(ift)
return ift
# Function to get image information
def imgInfo(img):
"""
Get the dimensions of an image.
Parameters:
- img : The input image.
Returns:
- width, height : The dimensions of the image (width, height) (tuple)
"""
width, height = img.size
#print(f"Image size: {width} x {height} pixels")
return width, height
# Function to create a circular mask
def circularMask(width, height, radius, centX, centY, visualize):
"""
Create a circular mask.
Parameters:
- width : size image Y.
- height : size image X.
- radius : Radius of the circular mask.
- centX : coordinate Y center
- centY : coordinate X center
- visualize : If True, display the mask (bool).
Returns:
- mask : The created circular mask.
"""
X, Y = np.ogrid[:width, :height]
mask = np.zeros((width, height))
circle = np.sqrt((X - centX) ** 2 + (Y - centY) ** 2) <= radius
mask[circle] = 1
if visualize:
imageShow(mask, 'mask')
return mask
# Function to save an Image
def saveImg(sample, name):
"""
Save an image to a file after normalization.
Parameters:
- sample : The input image data.
- name : The filename to save the image (str)
"""
sample = np.abs(sample)
image_data = ((sample - np.min(sample)) / (np.max(sample) - np.min(sample)) * 255)
image = Image.fromarray(image_data.astype(np.uint8))
imageio.imwrite(name, image)
#image.save('name.png', format="png")
return