Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
labo
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ISC2
visnum
labo
Commits
ca31516e
Verified
Commit
ca31516e
authored
1 year ago
by
iliya.saroukha
Browse files
Options
Downloads
Patches
Plain Diff
feat: fft done lab5
parent
f5c272cb
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
labo5.py
+130
-0
130 additions, 0 deletions
labo5.py
with
130 additions
and
0 deletions
labo5.py
0 → 100644
+
130
−
0
View file @
ca31516e
import
numpy
as
np
import
random
import
numpy.typing
as
npt
import
matplotlib.pyplot
as
plt
from
PIL
import
Image
from
enum
import
Enum
Img
=
npt
.
NDArray
[
np
.
uint8
]
Fft
=
npt
.
NDArray
[
np
.
complex128
]
class
Isotropy
(
Enum
):
ISO_90
=
1
ISO_45
=
2
class
Orientation
(
Enum
):
HORIZONTAL
=
1
VERTICAL
=
2
X_Y
=
3
def
load_img
(
path
:
str
)
->
Img
:
img
=
np
.
array
(
Image
.
open
(
path
))
return
img
def
show_image
(
img
:
Img
)
->
None
:
if
len
(
img
.
shape
)
==
2
:
plt
.
imshow
(
img
,
cmap
=
"
gray
"
,
vmin
=
0
,
vmax
=
255
)
else
:
plt
.
imshow
(
img
)
plt
.
show
()
def
thresholding
(
img
:
Img
,
value
:
int
)
->
Img
:
new_img
=
img
.
copy
()
for
y
in
range
(
img
.
shape
[
0
]):
for
x
in
range
(
img
.
shape
[
1
]):
if
new_img
[
y
,
x
]
>
value
:
new_img
[
y
,
x
]
=
255
else
:
new_img
[
y
,
x
]
=
0
return
new_img
def
xcorr
(
img
:
Img
,
kernel
:
Img
)
->
Img
:
res
=
np
.
zeros
(
img
.
shape
)
it
=
np
.
nditer
(
res
,
flags
=
[
'
multi_index
'
],
op_flags
=
[
'
writeonly
'
])
h
=
kernel
.
shape
[
0
]
//
2
for
elem
in
it
:
row
,
col
=
it
.
multi_index
roi
=
img
[
row
-
h
:
row
+
h
+
1
,
col
-
h
:
col
+
h
+
1
]
if
roi
.
shape
==
kernel
.
shape
:
mul
=
np
.
multiply
(
kernel
,
roi
)
elem
[...]
=
np
.
sum
(
mul
)
return
res
def
normalize
(
img
:
Img
,
new_range
:
tuple
[
int
,
int
]
=
(
0
,
255
))
->
Img
:
new_img
=
img
.
copy
()
min
,
max
=
(
np
.
amin
(
img
),
np
.
amax
(
img
))
for
y
in
range
(
img
.
shape
[
0
]):
for
x
in
range
(
img
.
shape
[
1
]):
intensity
=
new_img
[
y
,
x
]
new_img
[
y
,
x
]
=
(
intensity
-
min
)
*
((
new_range
[
1
]
-
new_range
[
0
])
/
(
max
-
min
))
\
+
new_range
[
0
]
return
new_img
def
get_fft
(
img
:
Img
)
->
Fft
:
return
np
.
fft
.
fft2
(
img
)
def
get_fft_spectrum
(
fft
:
Fft
)
->
Img
:
# Shift the zero-frequency component to the center of the spectrum
fft_shifted
=
np
.
fft
.
fftshift
(
fft
)
# Compute magnitude spectrum (absolute value of the shifted FFT)
magnitude_spectrum
=
np
.
abs
(
fft_shifted
)
return
np
.
log1p
(
magnitude_spectrum
)
def
get_img
(
fft
:
Fft
)
->
Img
:
reconstructed_img
=
np
.
fft
.
ifft2
(
fft
).
real
return
normalize
(
reconstructed_img
)
if
__name__
==
"
__main__
"
:
img
=
load_img
(
"
resources/testpattern1024.png
"
)
fft
=
get_fft
(
img
)
spectrum
=
get_fft_spectrum
(
fft
)
plt
.
figure
(
figsize
=
(
12
,
6
))
# Plot FFT magnitude spectrum
plt
.
subplot
(
1
,
3
,
1
)
plt
.
imshow
(
img
,
cmap
=
"
gray
"
,
vmin
=
0
,
vmax
=
255
)
plt
.
title
(
'
Original Image
'
)
plt
.
axis
(
'
off
'
)
plt
.
subplot
(
1
,
3
,
2
)
plt
.
imshow
(
spectrum
,
cmap
=
"
gray
"
)
plt
.
title
(
'
FFT spectrum
'
)
plt
.
axis
(
'
off
'
)
plt
.
subplot
(
1
,
3
,
3
)
plt
.
imshow
(
get_img
(
fft
),
cmap
=
"
gray
"
,
vmin
=
0
,
vmax
=
255
)
plt
.
title
(
'
Inverse FFT
'
)
plt
.
axis
(
'
off
'
)
plt
.
tight_layout
()
plt
.
show
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment