Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
uva
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
uva
Commits
fe971e12
Commit
fe971e12
authored
13 years ago
by
Taddeüs Kroes
Browse files
Options
Downloads
Patches
Plain Diff
improc ass4: Implemented Canny Edge Detector.
parent
92553830
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
improc/ass4/canny.py
+116
-12
116 additions, 12 deletions
improc/ass4/canny.py
improc/ass4/flags.png
+0
-0
0 additions, 0 deletions
improc/ass4/flags.png
improc/ass4/gauss.py
+1
-1
1 addition, 1 deletion
improc/ass4/gauss.py
with
117 additions
and
13 deletions
improc/ass4/canny.py
+
116
−
12
View file @
fe971e12
#!/usr/bin/env python
from
matplotlib.pyplot
import
imread
,
imshow
,
show
from
numpy
import
arctan
from
matplotlib.pyplot
import
imread
,
imshow
,
subplot
,
show
from
numpy
import
arctan2
,
zeros
,
append
,
pi
#, argmax
from
numpy.linalg
import
norm
from
gauss
import
gD
def
canny
(
F
,
s
):
# Noise reduction by a Gauss filter
G
=
gD
(
F
,
s
,
2
,
2
)
def
in_image
(
p
,
F
):
"""
Check if given pixel coordinates p are within the bound of image F.
"""
return
p
[
0
]
>=
0
and
p
[
1
]
>=
0
and
p
[
0
]
<
F
.
shape
[
0
]
and
p
[
1
]
<
F
.
shape
[
1
]
#def zero_crossing(a, b, F):
# """Cech if there is a zero crossing point between F[a] and F[b]."""
# return in_image(a, F) and in_image(b, F) \
# and ((F[a] < 0 and F[b] > 0) or (F[a] > 0 and F[b] < 0))
def
canny
(
F
,
s
,
Tl
=
None
,
Th
=
None
):
"""
Apply the Canny Edge Detection algorithm with Gauss scale s to an
image F. Optionally specify a low and high threshold (Tl and Th) for
hysteresis thresholding.
"""
# Noise reduction by a Gaussian filter
#F = gD(F, s, 0, 0)[1]
# Find intensity gradient
#F = gD(F, s, 2, 2)[1]
Gx
=
gD
(
F
,
s
,
1
,
0
)[
1
]
Gy
=
gD
(
F
,
s
,
0
,
1
)[
1
]
G
=
zeros
(
F
.
shape
)
A
=
zeros
(
F
.
shape
,
dtype
=
int
)
for
x
in
xrange
(
F
.
shape
[
0
]):
for
y
in
xrange
(
F
.
shape
[
1
]):
p
=
(
x
,
y
)
# Gradient norm and angle
G
[
p
]
=
norm
(
append
(
Gx
[
p
],
Gy
[
p
]))
A
[
p
]
=
int
(
round
(
arctan2
(
Gy
[
p
],
Gx
[
p
])
*
4
/
pi
+
1
))
%
4
#p = (x, y)
#compare = [(x, y - 1), (x, y + 1), (x + 1, y - 1), \
# (x - 1, y + 1), (x - 1, y), (x + 1, y), \
# (x - 1, y - 1), (x + 1, y + 1)]
#norms = zeros(8)
#for i, c in enumerate(compare):
# if zero_crossing(p, c, F):
# norms[i] = abs(F[p]) + abs(F[c])
return
G
#m = argmax(norms)
#G[p] = norms[m]
#A[p] = m >> 1
# Non-maximum suppression
E
=
zeros
(
F
.
shape
)
for
x
in
xrange
(
F
.
shape
[
0
]):
for
y
in
xrange
(
F
.
shape
[
1
]):
g
=
G
[
x
,
y
]
a
=
A
[
x
,
y
]
compare
=
[((
x
,
y
-
1
),
(
x
,
y
+
1
)),
((
x
-
1
,
y
-
1
),
\
(
x
+
1
,
y
+
1
)),
((
x
-
1
,
y
),
(
x
+
1
,
y
)),
\
((
x
+
1
,
y
-
1
),
(
x
-
1
,
y
+
1
))]
na
,
nb
=
compare
[
a
]
if
(
not
in_image
(
na
,
G
)
or
g
>
G
[
na
])
\
and
(
not
in_image
(
nb
,
G
)
or
g
>
G
[
nb
]):
E
[
x
,
y
]
=
g
# Only execute hysteresis thresholding if the thresholds are specified
if
Tl
is
None
or
Th
is
None
:
return
E
# Hysteresis thresholding
Tl
*=
(
E
.
max
()
-
E
.
min
())
/
255
Th
*=
(
E
.
max
()
-
E
.
min
())
/
255
T
=
zeros
(
F
.
shape
,
dtype
=
bool
)
# Clear image borders
for
x
in
xrange
(
F
.
shape
[
0
]):
E
[
x
,
0
]
=
E
[
x
,
F
.
shape
[
1
]
-
1
]
=
0
for
y
in
xrange
(
1
,
F
.
shape
[
1
]
-
1
):
E
[
0
,
y
]
=
E
[
F
.
shape
[
0
]
-
1
,
y
]
=
0
# Tracing edges
def
follow_nb
(
x
,
y
):
"""
Follow the neighbouring pixels of an edge pixel in E recursively.
"""
if
T
[
x
,
y
]:
return
T
[
x
,
y
]
=
True
for
nx
in
xrange
(
-
1
,
2
):
for
ny
in
xrange
(
-
1
,
2
):
if
(
nx
!=
0
or
ny
!=
0
)
and
E
[
nx
,
ny
]
>
Tl
:
follow_nb
(
nx
,
ny
)
# Follow edges that have a starting value above Th
for
x
in
xrange
(
F
.
shape
[
0
]):
for
y
in
xrange
(
F
.
shape
[
1
]):
if
E
[
x
,
y
]
>
Th
:
follow_nb
(
x
,
y
)
return
E
,
T
if
__name__
==
'
__main__
'
:
from
sys
import
argv
,
exit
if
len
(
argv
)
<
2
:
print
'
Usage: python %s SCALE
'
%
argv
[
0
]
if
len
(
argv
)
<
2
or
len
(
argv
)
==
3
:
print
'
Usage: python %s SCALE
[ TL TH ]
'
%
argv
[
0
]
exit
(
1
)
s
=
float
(
argv
[
1
])
F
=
imread
(
'
cameraman.png
'
)
E
=
canny
(
F
,
s
)
imshow
(
E
,
cmap
=
'
gray
'
)
#F = imread('flags.png')
s
=
float
(
argv
[
1
])
if
len
(
argv
)
>
3
:
# Execute with tracing edges
E
,
T
=
canny
(
F
,
s
,
float
(
argv
[
2
]),
float
(
argv
[
3
]))
subplot
(
131
)
imshow
(
F
,
cmap
=
'
gray
'
)
subplot
(
132
)
imshow
(
E
,
cmap
=
'
gray
'
)
subplot
(
133
)
imshow
(
T
,
cmap
=
'
gray
'
)
else
:
# Execute until nn-maximum suppression
E
=
canny
(
F
,
s
)
subplot
(
121
)
imshow
(
F
,
cmap
=
'
gray
'
)
subplot
(
122
)
imshow
(
E
,
cmap
=
'
gray
'
)
show
()
This diff is collapsed.
Click to expand it.
improc/ass4/flags.png
0 → 100644
+
0
−
0
View file @
fe971e12
29.2 KiB
This diff is collapsed.
Click to expand it.
improc/ass4/gauss.py
+
1
−
1
View file @
fe971e12
...
...
@@ -57,7 +57,7 @@ def gD(F, s, iorder, jorder):
funcs
=
[
lambda
x
:
e
**
-
(
x
**
2
/
(
2
*
s
**
2
))
/
(
2
*
pi
*
s
**
2
),
\
lambda
x
:
-
x
*
e
**
-
(
x
**
2
/
(
2
*
s
**
2
))
\
/
(
2
*
pi
*
s
**
4
),
\
lambda
x
:
-
(
x
**
2
-
s
**
2
)
*
e
**
-
(
x
**
2
/
(
2
*
s
**
2
))
\
lambda
x
:
(
x
**
2
-
s
**
2
)
*
e
**
-
(
x
**
2
/
(
2
*
s
**
2
))
\
/
(
2
*
pi
*
s
**
6
)]
size
=
int
(
ceil
(
3
*
s
))
r
=
2
*
size
+
1
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment