-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfurior_analysis.py
245 lines (208 loc) · 8.26 KB
/
furior_analysis.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
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
from math import pi
import cv2
import numpy as np
import time
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
def update(value):
thresholded_magnitude = gradient_magnitude.copy()
thresholded_magnitude, final_edges = canny_edge_detection(image, gradient_magnitude, gradient_direction, 150)
ax4.clear()
ax4.imshow(thresholded_magnitude, cmap='gray')
ax4.set_title('Thresholded Gradient Magnitude')
ax4.axis('off')
ax5.clear()
ax5.imshow(final_edges, cmap='gray')
ax5.set_title('Traced Edges')
ax5.axis('off')
plt.draw()
def canny_edge_detection(image,gradient_magnitude,gradient_direction,value):
gradient_direction = np.arctan2(gradient_y, gradient_x)
# Convert gradient direction to degrees
gradient_direction = np.degrees(gradient_direction)
# Quantize gradient direction into 4 directions: 0, 45, 90, 135 degrees
quantized_direction = np.round(gradient_direction / 45) % 4
# Apply non-maximum suppression
suppressed = np.zeros_like(gradient_magnitude)
for i in range(1, gradient_magnitude.shape[0] - 1):
for j in range(1, gradient_magnitude.shape[1] - 1):
if quantized_direction[i, j] == 0:
neighbors = [gradient_magnitude[i, j - 1], gradient_magnitude[i, j + 1]]
elif quantized_direction[i, j] == 1:
neighbors = [gradient_magnitude[i - 1, j + 1], gradient_magnitude[i + 1, j - 1]]
elif quantized_direction[i, j] == 2:
neighbors = [gradient_magnitude[i - 1, j], gradient_magnitude[i + 1, j]]
else:
neighbors = [gradient_magnitude[i - 1, j - 1], gradient_magnitude[i + 1, j + 1]]
if gradient_magnitude[i, j] >= max(neighbors):
suppressed[i, j] = gradient_magnitude[i, j]
# Thresholding and edge tracking by hysteresis
strong_pixel = 255
weak_pixel = 50
high_threshold = value
low_threshold = 30
strong_edges = suppressed > high_threshold
weak_edges = (suppressed >= low_threshold) & (suppressed <= high_threshold)
final_edges = np.zeros_like(suppressed)
f2 = np.zeros_like(suppressed)
f2[strong_edges] = 255
f2[weak_edges] = 50
final_edges[strong_edges] = 255
final_edges[weak_edges] = 50
rows, cols = final_edges.shape
for row in range(1, rows - 1):
for col in range(1, cols - 1):
if final_edges[row,col] == 50 or final_edges[row,col]==255:
# Check if any neighboring pixel is a strong edge
print(1)
if np.any(final_edges[row-2:row+3, col-2:col+3] == strong_pixel):
final_edges[row, col] = strong_pixel
else:
final_edges[row, col] = 0
dfs(final_edges)
return final_edges,f2
def trace_edges(rows, cols,row,col,final_edges):
if 0 <= row < rows and 0 <= col < cols and final_edges[row, col] == 50:
final_edges[row, col] = 255
for r in range(row - 2, row + 3):
for c in range(col - 2, col + 3):
if (r != row or c != col) and 0 <= r < rows and 0 <= c < cols:
print(1)
trace_edges(final_edges.shape[0],final_edges.shape[1],r,c,final_edges)
def canny():
image_path = "d://computer vision//1234.jpg" # replace with the actual path of your image
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) # convert to grayscale
edges = cv2.canny(image, 30, 150)
# # display the original image and the edge-detected image
cv2.imshow('original image', image)
cv2.imshow('edges', edges)
blurred = cv2.gaussianblur(image,(3,3),1.7 )
# calculate gradients
gradient_x = cv2.sobel(blurred, cv2.cv_64f, 1, 0, ksize=3)
gradient_y = cv2.sobel(blurred, cv2.cv_64f, 0, 1, ksize=3)
# calculate gradient magnitude and direction
gradient_magnitude = cv2.magnitude(gradient_x, gradient_y)
gradient_direction = cv2.phase(gradient_x, gradient_y, angleindegrees=true)
# create a figure and subplots
fig, ((ax1, ax2), (ax3, ax4), (ax6, ax5)) = plt.subplots(3, 2, figsize=(10, 12))
ax1.imshow(image, cmap='gray')
ax1.set_title('original image')
ax1.axis('off')
ax2.imshow(gradient_x, cmap='gray')
ax2.set_title('gradient in x direction')
ax2.axis('off')
ax3.imshow(gradient_y, cmap='gray')
ax3.set_title('gradient in y direction')
ax3.axis('off')
ax4.imshow(gradient_magnitude, cmap='gray')
ax4.set_title('gradient magnitude')
ax4.axis('off')
# add slider
ax_slider = plt.axes([0.2, 0.02, 0.65, 0.03])
slider = slider(ax_slider, 'threshold', valmin=0, valmax=np.max(gradient_magnitude), valinit=0)
update(100)
plt.tight_layout()
plt.show()
def dfs(final_edges):
for row in range(final_edges.shape[0]):
for col in range(final_edges.shape[1]):
if final_edges[row, col] == 255:
print(1)
trace_edges(final_edges.shape[0],final_edges.shape[1],row,col,final_edges)
def furior_transform(y,x,image):
_F = 0
N,M = image.shape
startu = int(-(image.shape[0]/2))
startv = int(-(image.shape[1]/2))
xcount = -1
for u in range(startu,image.shape[0]+startu-1):
xcount += 1
ycount = -1
for v in range(startv,image.shape[1]+startv-1):
ycount+=1
term = x*u/float(M) + y*v/float(N)
_F += image[xcount,ycount]*np.exp(-2*np.pi*1j*term)
#print(_F)
_F = _F/np.prod(image.shape)
return(_F)
def plot_nonzero_Fourier_coefficient_line(_y,_x,_N,_M):
## horizontal and vertical lines along nonzero Fourier coefficient
if _x < _M/2: ## x is always positive
plt.axvline(_x)
plt.axvline(-_x)
else: ## x is not between - M/2,..., M/2-1
plt.axvline(_M - _x)
plt.axvline(-_M + _x)
if _y < _N/2:
plt.axhline(_y)
plt.axhline(-_y)
else: ## y is not between - N/2,..., N/2-1
plt.axhline(_N - _y)
plt.axhline(-_N + _y)
def test(image):
orig_img = image
_N,_M = image.shape
ySample, xSample = _N, _M
_x,_y = _N,_M
fs = np.zeros((ySample,xSample),dtype=complex)
startx = int(-orig_img.shape[1]/2)
starty = int(-orig_img.shape[0]/2)
countx = -1
xs = range(startx,startx + orig_img.shape[1])
ys = range(starty,starty + orig_img.shape[0])
for x in xs:
countx += 1
county = -1
for y in ys:
county += 1
fs[countx,county] = furior_transform(y,x,orig_img)
if np.abs(fs[countx,county]) > 0.0001:
print("x={:2.0f}, y={:2.0f}, f={:+5.3f}".format(x,y,fs[county,countx]))
ploat(fs)
def ploat(fs):
magnitude_fs = np.abs(fs) # Compute the magnitude of complex values
plt.imshow(magnitude_fs, cmap="gray")
plt.colorbar(label="Magnitude")
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Fourier Transform Magnitude")
plt.show()
time.sleep(100)
def bfft(image):
fft = np.fft.fft2(image)
fft_shift = np.fft.fftshift(fft)
ploat(fft_shift)
def furior(angle,magnitude):
width, height = 500,500
image_real = np.zeros((height, width), dtype=np.float32)
image_imag = np.zeros((height, width), dtype=np.float32)
# Center of the image
center_x, center_y = width // 2, height // 2
angle_deg=angle
freq_factor = magnitude
angle_rad = np.radians(angle_deg)
k_x = freq_factor * np.sin(angle_rad)
k_y = freq_factor * np.cos(angle_rad)
# Loop through frequencies and orientations
# Try different frequency values
# Generate sinusoidal pattern
for y in range(height):
for x in range(width):
print("furior")
r = np.sqrt((x - center_x)**2 + (y - center_y)**2)
value_real = np.sin(k_x * x + k_y * y)
value_imag = np.cos(k_x * x + k_y * y)
image_real[y, x] += value_real
image_imag[y, x] += value_imag
# Display the image
plt.imshow(image_real, cmap='gray')
plt.axis('off')
plt.show()
return image_real
# Load the image8
if __name__ == "__main__":
#image_path = "D://computer vision//123.jpg" # replace with the actual path of your image
#image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
#test(image)
image = furior(45,1)
bfft(image)