main
Gasper Spagnolo 2022-11-20 22:32:36 +01:00
parent 647ff20f61
commit a138b0ffe5
2 changed files with 92 additions and 59 deletions

View File

@ -345,10 +345,10 @@ def ex3():
#three_a() #three_a()
#three_b() #three_b()
#three_c() #three_c()
three_d() #three_d()
three_e() #three_e()
three_f() #three_f()
#three_g() three_g()
def three_a(): def three_a():
""" """
@ -647,46 +647,76 @@ def three_f():
THETA = 0.02 THETA = 0.02
T_LOW = 0.04 T_LOW = 0.04
T_HIGH = 0.16 T_HIGH = 0.16
N_BINS_RHO = 360
N_BINS_THETA = 360
img = cv2.imread('images/rectangle.png') bricks_image_gray= uz_image.imread_gray('./images/bricks.jpg', uz_image.ImageType.float64)
pier_image_gray = uz_image.imread_gray('./images/pier.jpg', uz_image.ImageType.float64)
bricks_image_coloured = uz_image.imread('./images/bricks.jpg', uz_image.ImageType.float64)
pier_image_coloured = uz_image.imread('./images/pier.jpg', uz_image.ImageType.float64)
print('[+] Images loaded')
# Get gradient magntude and gradient angle bricks_image_edges = uz_image.find_edges_canny(bricks_image_gray, SIGMA, THETA+0.04, T_LOW, T_HIGH)
pier_image_edges = uz_image.find_edges_canny(pier_image_gray, SIGMA, THETA, T_LOW, T_HIGH)
print('[+] Edges detected')
t_lower = 70 gm_bricks, ga_bricks = uz_image.gradient_magnitude(bricks_image_gray, 1)
t_upper = 200 gm_pier, ga_pier = uz_image.gradient_magnitude(pier_image_gray, 1)
edge_detected_image = cv2.Canny(img, t_lower, t_upper)
gm, ga = uz_image.gradient_magnitude(img, 1)
print('Edge detected:', edge_detected_image.shape) bricks_image_hough = uz_image.hough_find_lines_i(bricks_image_edges, ga_bricks, gm_bricks, N_BINS_THETA, N_BINS_RHO, 0.2)
pier_image_hough = uz_image.hough_find_lines_i(pier_image_edges, ga_pier, gm_pier, N_BINS_THETA, N_BINS_RHO, 0.2)
print('[+] Hugh lines drawn')
# Transform image into hough space bricks_image_hough_nonmax = uz_image.nonmaxima_suppression_box(bricks_image_hough)
image_transformed_into_hough_space = uz_image.hough_find_lines_i(edge_detected_image, ga, gm, 360, 360, 0.2) pier_image_hough_nonmax = uz_image.nonmaxima_suppression_box(pier_image_hough)
print('Hough lines drawn:', image_transformed_into_hough_space.shape) print('[+] Nonmaxima suppression applied')
bricks_image_line_params = uz_image.retrieve_hough_pairs(bricks_image_gray, bricks_image_hough_nonmax, np.max(bricks_image_hough_nonmax)*0.3, N_BINS_THETA, N_BINS_RHO)
pier_image_line_params = uz_image.retrieve_hough_pairs(pier_image_gray, pier_image_hough_nonmax, np.max(pier_image_hough_nonmax)*0.3, N_BINS_THETA, N_BINS_RHO)
print('[+] Hough pairs retrieved')
def select_best_pairs(image_line_params: npt.NDArray[np.float64], n =20):
image_line_params = np.array(image_line_params)
# Sorts just kth element so every eleement before kth element is lower than kth element
# and every element after kth element is higher than kth element
partition = np.argpartition(image_line_params, kth=len(image_line_params) - n - 1, axis=0)[-n:]
image_line_params = image_line_params[partition.T[0]]
return image_line_params
bricks_image_line_params = select_best_pairs(bricks_image_line_params)
pier_image_line_params = select_best_pairs(pier_image_line_params)
print('[+] Best pairs selected')
fig, axs = plt.subplots(5, 2)
# Plot grayscale images
axs[0, 0].imshow(bricks_image_gray, cmap='gray')
axs[0, 0].set(title='bricks.jpg')
axs[0, 1].imshow(pier_image_gray, cmap='gray')
axs[0, 1].set(title='pier.jpg')
# Plot images with canny edges detected
axs[1, 0].imshow(bricks_image_edges, cmap='gray')
axs[1, 1].imshow(pier_image_edges, cmap='gray')
# Plot images in hough space
axs[2, 0].imshow(bricks_image_hough)
axs[2, 1].imshow(pier_image_hough)
# Plot images in hough space after nonmax suppression
axs[3, 0].imshow(bricks_image_hough_nonmax)
axs[3, 1].imshow(pier_image_hough_nonmax)
hugh_pairs = uz_image.retrieve_hough_pairs(img, image_transformed_into_hough_space, np.max(image_transformed_into_hough_space) * 0.2, 360, 360) # Plot coloured images with lines drawn
best_pairs = hugh_pairs axs[4, 0].imshow(bricks_image_coloured)
for param in bricks_image_line_params:
xs, ys = uz_image.get_line_to_plot(param[0], param[1], bricks_image_coloured.shape[0], bricks_image_coloured.shape[1])
axs[4, 0].plot(xs, ys, 'r', linewidth=0.7)
fig, axs = plt.subplots(2, 2) axs[4, 1].imshow(pier_image_coloured)
for param in pier_image_line_params:
axs[0, 0].imshow(edge_detected_image) xs, ys = uz_image.get_line_to_plot(param[0], param[1], pier_image_coloured.shape[0], pier_image_coloured.shape[1])
axs[0, 0].set(title='normal') axs[4, 1].plot(xs, ys, 'r', linewidth=0.7)
axs[0, 1].imshow(image_transformed_into_hough_space)
axs[0, 1].set(title='normal')
axs[1, 0].imshow(edge_detected_image, cmap='gray')
for param in best_pairs:
xs, ys = uz_image.get_line_to_plot(param[0], param[1], img.shape[0], img.shape[1])
axs[1, 0].plot(xs, ys, 'r', linewidth=0.7)
axs[1, 1].imshow(image_transformed_into_hough_space)
#axs[1,1].imshow(rectangle_image, cmap='gray')
#for param in best_pairs_i:
# xs, ys = uz_image.get_line_to_plot(param[0], param[1], rectangle_image.shape[0], rectangle_image.shape[1])
# axs[1, 1].plot(xs, ys, 'r', linewidth=0.7)
#plt.show()
plt.show() plt.show()
@ -697,14 +727,15 @@ def three_g():
between 45 and 50 pixels. between 45 and 50 pixels.
""" """
circle_image = uz_image.imread('images/eclipse.jpg', uz_image.ImageType.uint8) SIGMA = 1
THETA = 0.02
T_LOW = 0.04
T_HIGH = 0.16
#img = cv2.imread('images/rectangle.png') circle_image = uz_image.imread('images/eclipse.jpg', uz_image.ImageType.float64)
t_lower = 100 circle_image_gray = uz_image.imread_gray('images/eclipse.jpg', uz_image.ImageType.float64)
t_upper = 150 edge_detected_image = uz_image.find_edges_canny(circle_image_gray, SIGMA, THETA, T_LOW, T_HIGH)
edge_detected_image = cv2.Canny(circle_image, t_lower, t_upper) hugh_transformed_circle = uz_image.hough_transform_a_circle(circle_image_gray, edge_detected_image, 45, 50, 0.2)
hugh_transformed_circle = uz_image.hough_transform_a_circle(edge_detected_image, 45, 50, 0.2)
fig, axs = plt.subplots(1, 2) fig, axs = plt.subplots(1, 2)
@ -720,7 +751,6 @@ def three_g():
plt.imshow(hugh_transformed_circle[:, :, i]) plt.imshow(hugh_transformed_circle[:, :, i])
plt.show() plt.show()
# ######## # # ######## #
# SOLUTION # # SOLUTION #
# ######## # # ######## #

View File

@ -727,28 +727,30 @@ def hough_transform_a_point(x: int, y: int, n_bins: int) -> npt.NDArray[np.float
return accumlator return accumlator
def hough_transform_a_circle(edged_image: Union[npt.NDArray[np.float64] , npt.NDArray[np.uint8]], def hough_transform_a_circle(image: Union[npt.NDArray[np.float64] , npt.NDArray[np.uint8]],
edged_image: Union[npt.NDArray[np.float64] , npt.NDArray[np.uint8]],
r_start: int, r_end: int, treshold: float) -> npt.NDArray[np.float64]: r_start: int, r_end: int, treshold: float) -> npt.NDArray[np.float64]:
""" """
Accepts: image, r_start, r_end Accepts: image, r_start, r_end
Returns: hough space Returns: hough space
""" """
image = edged_image.copy()
image[image < treshold] = 0
accumlator = np.zeros((edged_image.shape[0], edged_image.shape[1], r_end - r_start))
indices = np.argwhere(image)
sine_value = np.sin(np.linspace(0, np.pi, 360))
cosine_value = np.cos(np.linspace(0, np.pi, 360))
edged_image = edged_image.copy()
edged_image[edged_image < treshold] = 0
accumlator = np.zeros((edged_image.shape[0], edged_image.shape[1], r_end - r_start))
indices = np.argwhere(edged_image)
gm, ga = gradient_magnitude(image, 1)
# Loop through all nonzero pixels above treshold # Loop through all nonzero pixels above treshold
for i in tqdm(range(len(indices)), desc='Hough transform'): for i in tqdm(range(len(indices)), desc='Hough transform'):
for r in range(0, r_end - r_start): for r in range(0, r_end - r_start):
x, y = indices[i] print(r)
for svcv in range(sine_value.shape[0]): y, x = indices[i]
a = x - r * cosine_value[svcv] a = x - r * np.cos(ga[y, x])
b = y - r * sine_value[svcv] b = y - r * np.sin(ga[y, x])
accumlator[int(a), int(b), r ] += 1 accumlator[int(a), int(b), r ] += 1
return accumlator return accumlator
@ -762,7 +764,8 @@ def hough_find_lines(image: Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]
image = image.copy() image = image.copy()
image[image < treshold] = 0 image[image < treshold] = 0
theta_values = np.linspace(-np.pi/2, np.pi/2, n_bins_theta) theta_values = np.linspace(-np.pi/2, np.pi/2, n_bins_theta)
D = np.sqrt(image.shape[0]**2 + image.shape[1]**2)
D = np.sqrt(image.shape[0]**2 + image.shape[1]**2)
rho_values = np.linspace(-D, D, n_bins_rho) rho_values = np.linspace(-D, D, n_bins_rho)
accumulator = np.zeros((n_bins_rho, n_bins_theta), dtype=np.uint64) accumulator = np.zeros((n_bins_rho, n_bins_theta), dtype=np.uint64)