-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhough_peaks.py
More file actions
63 lines (54 loc) · 1.95 KB
/
hough_peaks.py
File metadata and controls
63 lines (54 loc) · 1.95 KB
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
import numpy as np
import cv2
import matplotlib.pyplot as plt
def hough_peaks(H, Npeaks, border = 5):
peaks = []
Htemp = np.copy(H)
# loop through number of peaks
for i in range(Npeaks):
# find max indices in full arr then get (x,y) location
# get mem location of maxes, first
# then, get coordinates in H
# add coordinates to index arr
# these are indexes of
idx = np.argmax(Htemp)
Hidx = np.unravel_index(idx, Htemp.shape)
peaks.append(Hidx)
# check if too close to the edges of the image
# get x, y
iy, ix = Hidx # first separate x, y indexes from argmax(H)
# if idx_x is too close to the edges choose appropriate values
v = border/2
if (ix - v) < 0:
xmin = 0
else:
xmin = ix - v
xmin = int(round(xmin))
if ((ix + v + 1) > H.shape[1]):
xmax = H.shape[1]
else:
xmax = ix + v + 1
xmax = int(round(xmax))
# if idx_y is too close to the edges choose appropriate values
if (iy - v) < 0:
ymin = 0
else:
ymin = iy - v
ymin = int(round(ymin))
if ((iy + v+ 1) > H.shape[0]):
ymax = H.shape[0]
else:
ymax = iy + v + 1
ymax = int(round(ymax))
# bound each index by the neighborhood size and set all values to 0
for x in range(xmin, xmax):
for y in range(ymin, ymax):
# remove neighborhoods in H1
Htemp[y, x] = 0
# highlight peaks in original H
if (x == xmin or x == (xmax - 1)):
H[y, x] = 255
if (y == ymin or y == (ymax - 1)):
H[y, x] = 255
# return the indicies and the original Hough space with selected points
return peaks, H