Hvala kurcu, mamo progress bar

main
Gasper Spagnolo 2022-11-16 12:55:08 +01:00
parent a2b30c7ed1
commit efff29e065
2 changed files with 141 additions and 21 deletions

View File

@ -229,7 +229,8 @@ def ex3():
#three_a()
#three_b()
#three_c()
three_d()
#three_d()
three_e()
def three_a():
"""
@ -361,6 +362,8 @@ def three_d():
THETA = 0.02
T_LOW = 0.04
T_HIGH = 0.16
N_BINS_RHO = 720
N_BINS_THETA = 720
synthetic_image = np.zeros((100, 100))
synthetic_image[10, 10] = 1
@ -368,29 +371,143 @@ def three_d():
oneline_image = uz_image.imread_gray('./images/oneline.png', uz_image.ImageType.float64)
rectangle_image = uz_image.imread_gray('./images/rectangle.png', uz_image.ImageType.float64)
print('[+] Images loaded')
oneline_image_edges = uz_image.find_edges_canny(oneline_image, SIGMA, THETA, T_LOW, T_HIGH)
rectangle_image_edges = uz_image.find_edges_canny(rectangle_image, SIGMA, THETA, T_LOW, T_HIGH)
print('[+] Edges detected')
synthetic_image_hough = uz_image.hough_find_lines(synthetic_image, 360, 360, 0.2)
oneline_image_hough = uz_image.hough_find_lines(oneline_image_edges, 360, 360, 0.2)
rectangle_image_hough = uz_image.hough_find_lines(rectangle_image_edges, 360, 360 , 0.2)
synthetic_image_hough = uz_image.hough_find_lines(synthetic_image, N_BINS_THETA, N_BINS_RHO, 0.2)
oneline_image_hough = uz_image.hough_find_lines(oneline_image_edges, N_BINS_THETA, N_BINS_RHO, 0.2)
rectangle_image_hough = uz_image.hough_find_lines(rectangle_image_edges, N_BINS_THETA, N_BINS_RHO , 0.2)
print('[+] Hugh lines drawn')
synthetic_image_hough_nonmax = uz_image.nonmaxima_suppression_box(synthetic_image_hough)
oneline_image_hough_nonmax = uz_image.nonmaxima_suppression_box(oneline_image_hough)
rectangle_image_hough_nonmax = uz_image.nonmaxima_suppression_box(rectangle_image_hough)
print('[+] Nonmaxima suppression applied')
fig, axs = plt.subplots(1, 3)
axs[0].imshow(oneline_image)
neighbour_pairs = uz_image.retrieve_hough_pairs(oneline_image_hough_nonmax, np.max(oneline_image_hough_nonmax)*0.3, 360, 360)
def select_best_pairs(image_line_params: npt.NDArray[np.float64], n =10):
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
# Plot synthetic image
axs[0].imshow(synthetic_image, cmap='gray')
neighbour_pairs = uz_image.retrieve_hough_pairs(synthetic_image, synthetic_image_hough_nonmax, np.max(synthetic_image_hough_nonmax)*0.99, N_BINS_THETA, N_BINS_RHO)
for neighbour in neighbour_pairs:
xs, ys = uz_image.get_line_to_plot(neighbour[0], neighbour[1], oneline_image_hough_nonmax.shape[0], oneline_image_hough_nonmax.shape[1])
xs, ys = uz_image.get_line_to_plot(neighbour[0], neighbour[1], synthetic_image.shape[0], synthetic_image.shape[1])
axs[0].plot(xs, ys, 'r', linewidth=0.7)
plt.show()
# Plot oneline image
axs[1].imshow(oneline_image, cmap='gray')
neighbour_pairs = uz_image.retrieve_hough_pairs(oneline_image, oneline_image_hough_nonmax, np.max(oneline_image_hough_nonmax)*0.5, N_BINS_THETA, N_BINS_RHO)
for neighbour in neighbour_pairs:
xs, ys = uz_image.get_line_to_plot(neighbour[0], neighbour[1], oneline_image.shape[0], oneline_image.shape[1])
axs[1].plot(xs, ys, 'r', linewidth=0.7)
# Plot rectangle image
axs[2].imshow(rectangle_image, cmap='gray')
neighbour_pairs = uz_image.retrieve_hough_pairs(rectangle_image, rectangle_image_hough_nonmax, np.max(rectangle_image_hough_nonmax)*0.35, N_BINS_THETA, N_BINS_RHO)
best_paris = select_best_pairs(neighbour_pairs)
for neighbour in best_paris:
xs, ys = uz_image.get_line_to_plot(neighbour[0], neighbour[1], rectangle_image.shape[0], rectangle_image.shape[1])
axs[2].plot(xs, ys, 'r', linewidth=0.7)
plt.show()
def three_e():
"""
Read the image from files bricks.jpg and pier.jpg. Change the image to grayscale
and detect edges. Then detect lines using your algorithm. As the results will likely
depend on the number of pixels that vote for specific cell and this depends on the
size of the image and the resolution of the accumulator, try sorting the pairs by
their corresponding cell values in descending order and only select the top n = 10
lines. Display the results and experiment with parameters of Hough algorithm as
well as the edge detection algorithm, e.g. try changing the number of cells in the
accumulator or σ parameter in edge detection to obtain results that are similar or
better to the ones shown on the image below
"""
SIGMA = 1
THETA = 0.02
T_LOW = 0.04
T_HIGH = 0.16
N_BINS_RHO = 360
N_BINS_THETA = 360
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')
bricks_image_edges = uz_image.find_edges_canny(bricks_image_gray, SIGMA, THETA, T_LOW, T_HIGH)
pier_image_edges = uz_image.find_edges_canny(pier_image_gray, SIGMA, THETA, T_LOW, T_HIGH)
print('[+] Edges detected')
bricks_image_hough = uz_image.hough_find_lines(bricks_image_edges, N_BINS_THETA, N_BINS_RHO, 0.2)
pier_image_hough = uz_image.hough_find_lines(pier_image_edges, N_BINS_THETA, N_BINS_RHO, 0.2)
print('[+] Hugh lines drawn')
bricks_image_hough_nonmax = uz_image.nonmaxima_suppression_box(bricks_image_hough)
pier_image_hough_nonmax = uz_image.nonmaxima_suppression_box(pier_image_hough)
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.8, 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.8, N_BINS_THETA, N_BINS_RHO)
print('[+] Hough pairs retrieved')
def select_best_pairs(image_line_params: npt.NDArray[np.float64], n =10):
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)
# Plot coloured images with lines drawn
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)
axs[4, 1].imshow(pier_image_coloured)
for param in pier_image_line_params:
xs, ys = uz_image.get_line_to_plot(param[0], param[1], pier_image_coloured.shape[0], pier_image_coloured.shape[1])
axs[4, 1].plot(xs, ys, 'r', linewidth=0.7)
plt.show()
# ######## #
# SOLUTION #
# ######## #

View File

@ -6,6 +6,7 @@ from PIL import Image
from typing import Union
import numpy.typing as npt
import enum
from tqdm import tqdm
class ImageType(enum.Enum):
uint8 = 0
@ -664,7 +665,8 @@ def hough_find_lines(image: Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]
image = image.copy()
image[image < treshold] = 0
theta_values = np.linspace(-np.pi/2, np.pi/2, n_bins_theta)
rho_values = np.linspace(-np.sqrt(image.shape[0]**2 + image.shape[1]**2), np.sqrt(image.shape[0]**2 + image.shape[1]**2), n_bins_rho)
D = np.sqrt(image.shape[0]**2 + image.shape[1]**2)
rho_values = np.linspace(-D, D, n_bins_rho)
accumulator = np.zeros((n_bins_rho, n_bins_theta), dtype=np.uint64)
cos_precalculated = np.cos(theta_values)
@ -673,15 +675,12 @@ def hough_find_lines(image: Union[npt.NDArray[np.float64], npt.NDArray[np.uint8]
y_s, x_s = np.nonzero(image)
# Loop through all nonzero pixels above treshold
for i in range(len(y_s)):
for i in tqdm(range(len(y_s)), desc='Hough transform'):
x = x_s[i]
y = y_s[i]
# Precalculate rhos
rhos = []
for theta in range(n_bins_theta):
rho = int(np.round(x * cos_precalculated[theta] + y * sin_precalculated[theta]))
rhos.append(rho)
rhos = np.round(x* cos_precalculated + y* sin_precalculated).astype(np.int64)
# Bin the rhos
binned = np.digitize(rhos, rho_values)
@ -707,27 +706,31 @@ def nonmaxima_suppression_box(image: npt.NDArray[np.uint64]) -> npt.NDArray[np.u
neighbours.append((i, j))
return neighbours
neighbours = get_neighbours()
for y in range(1, image.shape[0]-1):
for x in range(1, image.shape[1]-1):
for neighbour in get_neighbours():
for neighbour in neighbours:
if image[y, x] < image[y+neighbour[0], x+neighbour[1]]:
image[y, x] = 0
break
return image
def retrieve_hough_pairs(image: npt.NDArray[np.uint64], treshold: int, n_bins_theta: int, n_bins_rho) -> list[tuple[int, int]]:
def retrieve_hough_pairs(original_image: npt.NDArray[np.float64], hough_image: npt.NDArray[np.uint64], treshold: int, n_bins_theta: int, n_bins_rho: int) -> list[tuple[int, int]]:
"""
Accepts: image with sinusoids in hough space, treshold
Accepts: original image, image with sinusoids in hough space, treshold, n_bins theta in h space, n_bins rho in h space
Returns: list of pairs of sinusoids
"""
theta_values = np.linspace(-np.pi/2, np.pi/2, n_bins_theta)
rho_values = np.linspace(-np.sqrt(image.shape[0]**2 + image.shape[1]**2), np.sqrt(image.shape[0]**2 + image.shape[1]**2), n_bins_rho)
D = np.sqrt(original_image.shape[0]**2 + original_image.shape[1]**2)
rho_values = np.linspace(-D, D, n_bins_rho)
image = image.copy()
image[image < treshold] = 0
hough_image = hough_image.copy()
hough_image[hough_image < treshold] = 0
y_s, x_s = np.nonzero(hough_image)
y_s, x_s = np.nonzero(image)
pairs = []
for i in range(len(x_s)):
x = x_s[i] # theta values