hvala kurcu za neumna navodila + 2
parent
72a58075a3
commit
1922be45e2
|
@ -13,7 +13,7 @@ def ex1():
|
||||||
one_a()
|
one_a()
|
||||||
one_b()
|
one_b()
|
||||||
one_c()
|
one_c()
|
||||||
one_e()
|
one_d()
|
||||||
|
|
||||||
def one_a() -> None:
|
def one_a() -> None:
|
||||||
"""
|
"""
|
||||||
|
@ -756,9 +756,9 @@ def three_g():
|
||||||
# ######## #
|
# ######## #
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
#ex1() # everything K
|
ex1() # everything K
|
||||||
#ex2() # everything OK
|
#ex2() # everything OK
|
||||||
ex3()
|
#ex3()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -12,14 +12,14 @@ import os
|
||||||
|
|
||||||
def ex1():
|
def ex1():
|
||||||
one_a()
|
one_a()
|
||||||
|
one_b()
|
||||||
|
|
||||||
def one_a() -> None:
|
def one_a() -> None:
|
||||||
img = uz_image.imread_gray("data/graf/graf_a.jpg", uz_image.ImageType.float64)
|
img = uz_image.imread_gray("data/graf/graf_a.jpg", uz_image.ImageType.float64)
|
||||||
# Get the hessian points
|
|
||||||
sigmas = [3, 6, 9, 12]
|
sigmas = [3, 6, 9, 12]
|
||||||
determinant, hessian_points = uz_image.hessian_points(img, 9, 0.004)
|
|
||||||
# Plot the points
|
# Plot the points
|
||||||
fig, axs = plt.subplots(2, len(sigmas))
|
fig, axs = plt.subplots(2, len(sigmas))
|
||||||
|
fig.suptitle("Hessian corner detection")
|
||||||
|
|
||||||
for i, sigma in enumerate(sigmas):
|
for i, sigma in enumerate(sigmas):
|
||||||
determinant, hessian_points = uz_image.hessian_points(img, sigma, 0.004)
|
determinant, hessian_points = uz_image.hessian_points(img, sigma, 0.004)
|
||||||
|
@ -29,11 +29,29 @@ def one_a() -> None:
|
||||||
# Plot grayscale image
|
# Plot grayscale image
|
||||||
axs[1, i].imshow(img, cmap="gray")
|
axs[1, i].imshow(img, cmap="gray")
|
||||||
# Plot scatter hessian points
|
# 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()
|
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 #
|
# SOLUTION #
|
||||||
# ######## #
|
# ######## #
|
||||||
|
|
|
@ -830,25 +830,15 @@ def nonmaxima_suppression_box(image: Union[npt.NDArray[np.float64], npt.NDArray[
|
||||||
Accepts: image with sinusoids in hough space
|
Accepts: image with sinusoids in hough space
|
||||||
Returns: image with sinusoids
|
Returns: image with sinusoids
|
||||||
"""
|
"""
|
||||||
image = image.copy()
|
nms_image = np.zeros_like(image)
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
for y in range(1, image.shape[0]-1):
|
for y in range(1, image.shape[0]-1):
|
||||||
for x in range(1, image.shape[1]-1):
|
for x in range(1, image.shape[1]-1):
|
||||||
for neighbour in neighbours:
|
neighbours = image[y - 1 : y + 2, x - 1 : x + 2]
|
||||||
if image[y, x] < image[y+neighbour[0], x+neighbour[1]]:
|
if image[y, x] >= np.max(neighbours):
|
||||||
image[y, x] = 0
|
nms_image[y, x] = image[y, x]
|
||||||
break
|
|
||||||
return image
|
return nms_image
|
||||||
|
|
||||||
def retrieve_hough_pairs(original_image: npt.NDArray[np.float64], hough_image: npt.NDArray[np.uint64],
|
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]]:
|
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)
|
points = np.argwhere(points > treshold)
|
||||||
|
|
||||||
return determinant.astype(np.float64), points.astype(np.float64)
|
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)
|
||||||
|
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue