From 1922be45e2bc5a6ea633a1a8ed24af71d4cb6001 Mon Sep 17 00:00:00 2001 From: Spagnolo Gasper Date: Sat, 26 Nov 2022 16:06:18 +0100 Subject: [PATCH] hvala kurcu za neumna navodila + 2 --- assignment3/solution.py | 6 ++-- assignment4/solution.py | 26 +++++++++++--- assignment4/uz_framework/image.py | 58 ++++++++++++++++++++++--------- requirements.txt | 19 ++++++++++ 4 files changed, 86 insertions(+), 23 deletions(-) create mode 100644 requirements.txt diff --git a/assignment3/solution.py b/assignment3/solution.py index 9284e37..bb971dc 100644 --- a/assignment3/solution.py +++ b/assignment3/solution.py @@ -13,7 +13,7 @@ def ex1(): one_a() one_b() one_c() - one_e() + one_d() def one_a() -> None: """ @@ -756,9 +756,9 @@ def three_g(): # ######## # def main(): - #ex1() # everything K + ex1() # everything K #ex2() # everything OK - ex3() + #ex3() if __name__ == '__main__': main() diff --git a/assignment4/solution.py b/assignment4/solution.py index 75b5d47..2259d11 100644 --- a/assignment4/solution.py +++ b/assignment4/solution.py @@ -12,14 +12,14 @@ import os def ex1(): one_a() + one_b() def one_a() -> None: img = uz_image.imread_gray("data/graf/graf_a.jpg", uz_image.ImageType.float64) - # Get the hessian points sigmas = [3, 6, 9, 12] - determinant, hessian_points = uz_image.hessian_points(img, 9, 0.004) # Plot the points fig, axs = plt.subplots(2, len(sigmas)) + fig.suptitle("Hessian corner detection") for i, sigma in enumerate(sigmas): determinant, hessian_points = uz_image.hessian_points(img, sigma, 0.004) @@ -29,11 +29,29 @@ def one_a() -> None: # Plot grayscale image axs[1, i].imshow(img, cmap="gray") # Plot scatter hessian points - axs[1, i].scatter(hessian_points[:, 1], hessian_points[:, 0], s=1, c="r", marker="x") + axs[1, i].scatter(hessian_points[:, 1], hessian_points[:, 0], s=20, c="r", marker="x") plt.show() - +def one_b() -> None: + img = uz_image.imread_gray("data/graf/graf_a.jpg", uz_image.ImageType.float64) + sigmas = [3, 6, 9] + # Plot the points + fig, axs = plt.subplots(2, len(sigmas)) + fig.suptitle("Harris corner detection") + + for i, sigma in enumerate(sigmas): + determinant, harris_points = uz_image.harris_detector(img, sigma, treshold=1e-6) + # Plot determinant + axs[0, i].imshow(determinant) + axs[0, i].set_title(f"Sigma: {sigma}") + # Plot grayscale image + axs[1, i].imshow(img, cmap="gray") + # Plot scatter hessian points + axs[1, i].scatter(harris_points[:, 1], harris_points[:, 0], s=20, c="r", marker="x") + + plt.show() + # ######## # # SOLUTION # # ######## # diff --git a/assignment4/uz_framework/image.py b/assignment4/uz_framework/image.py index c716218..7de1e7e 100644 --- a/assignment4/uz_framework/image.py +++ b/assignment4/uz_framework/image.py @@ -830,25 +830,15 @@ def nonmaxima_suppression_box(image: Union[npt.NDArray[np.float64], npt.NDArray[ Accepts: image with sinusoids in hough space Returns: image with sinusoids """ - image = image.copy() - - def get_neighbours() -> list[tuple[int, int]]: - neighbours = [] - for i in range(-1, 2): - for j in range(-1, 2): - if i != 0 or j != 0: - neighbours.append((i, j)) - return neighbours - - neighbours = get_neighbours() + nms_image = np.zeros_like(image) for y in range(1, image.shape[0]-1): for x in range(1, image.shape[1]-1): - for neighbour in neighbours: - if image[y, x] < image[y+neighbour[0], x+neighbour[1]]: - image[y, x] = 0 - break - return image + neighbours = image[y - 1 : y + 2, x - 1 : x + 2] + if image[y, x] >= np.max(neighbours): + nms_image[y, x] = image[y, x] + + return nms_image 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]]: @@ -942,3 +932,39 @@ def hessian_points(image: Union[npt.NDArray[np.float64], points = np.argwhere(points > treshold) return determinant.astype(np.float64), points.astype(np.float64) + +def harris_detector(image: Union[npt.NDArray[np.float64], + npt.NDArray[np.uint8]], sigma: float, treshold: float, + alpha = 0.06) -> tuple[npt.NDArray[np.float64], npt.NDArray[np.float64]]: + """ + Accepts: image, sigma, treshold + """ + image = image.copy() + + (ix, iy) = derive_image_first_order(image, sigma) + + ix2 = np.square(ix) + iy2 = np.square(iy) + ixy = np.multiply(ix, iy) + + # Apply subsequent smoothing with gaussian kernel + gauss = np.array([get_gaussian_kernel(sigma * 1.6)]) # 1.6 is empirically chosen + ix2 = cv2.filter2D(ix2, cv2.CV_64F, gauss) + iy2 = cv2.filter2D(iy2, cv2.CV_64F, gauss) + ixy = cv2.filter2D(ixy, cv2.CV_64F, gauss) + ix2 = cv2.filter2D(ix2, cv2.CV_64F, gauss.T) + iy2 = cv2.filter2D(iy2, cv2.CV_64F, gauss.T) + ixy = cv2.filter2D(ixy, cv2.CV_64F, gauss.T) + + + determinant = ix2 * iy2 - np.square(ixy) + trace = ix2 + iy2 + + # Calculate harris response + features = determinant - alpha * np.square(trace) + + nms_features = nonmaxima_suppression_box(features) + points = np.argwhere(nms_features > treshold) + + return features.astype(np.float64), points.astype(np.float64) + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..bbb2e5b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,19 @@ +black==22.10.0 +click==8.1.3 +contourpy==1.0.5 +cycler==0.11.0 +fonttools==4.37.4 +kiwisolver==1.4.4 +matplotlib==3.6.1 +mypy-extensions==0.4.3 +numpy==1.23.4 +opencv-python==4.6.0.66 +packaging==21.3 +pathspec==0.10.1 +Pillow==9.2.0 +platformdirs==2.5.2 +pyparsing==3.0.9 +python-dateutil==2.8.2 +six==1.16.0 +tomli==2.0.1 +typing-extensions==4.4.0