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
b7e9b760
Commit
b7e9b760
authored
13 years ago
by
Taddeüs Kroes
Browse files
Options
Downloads
Patches
Plain Diff
ImProc ass2: implemented perspective transform.
parent
2393aae2
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/ass2/affine.py
+41
-9
41 additions, 9 deletions
improc/ass2/affine.py
improc/ass2/flyeronground.png
+0
-0
0 additions, 0 deletions
improc/ass2/flyeronground.png
improc/ass2/interpolation.py
+2
-4
2 additions, 4 deletions
improc/ass2/interpolation.py
with
43 additions
and
13 deletions
improc/ass2/affine.py
+
41
−
9
View file @
b7e9b760
from
interpolation
import
pv
from
pylab
import
array
,
matrix
,
zeros
,
show
from
numpy.linalg
import
lstsq
,
inv
from
pylab
import
array
,
matrix
,
zeros
,
show
,
dot
,
lstsq
,
inv
,
svd
# url: http://www.leptonica.com/affine.html
...
...
@@ -35,26 +34,59 @@ def affine_transform(image, p1, p2, p3, width, height):
# Construct the transformed image
result
=
zeros
((
width
,
height
))
for
y
in
xrange
(
height
):
for
x
in
xrange
(
width
):
for
x
in
xrange
(
width
):
for
y
in
xrange
(
height
):
orig_pos
=
(
A
*
array
([[
x
,
y
,
1
]]).
T
).
T
.
tolist
()[
0
]
result
[
x
][
y
]
=
pv
(
image
,
orig_pos
[
0
],
orig_pos
[
1
],
'
linear
'
)
return
result
def
perspective_transform
(
image
,
p1
,
p2
,
p3
,
p4
,
width
,
height
):
"""
Warps the parallelogram defined by four corner points onto a new image
of size MxN.
"""
x1
,
y1
=
p1
x2
,
y2
=
p2
x3
,
y3
=
p3
x4
,
y4
=
p4
M
=
array
([[
x1
,
y1
,
1
,
0
,
0
,
0
,
0
,
0
,
0
],
[
0
,
0
,
0
,
x1
,
y1
,
1
,
0
,
0
,
0
],
[
x2
,
y2
,
1
,
0
,
0
,
0
,
-
width
*
x2
,
-
width
*
y2
,
-
width
],
[
0
,
0
,
0
,
x2
,
y2
,
1
,
0
,
0
,
0
],
[
x3
,
y3
,
1
,
0
,
0
,
0
,
0
,
0
,
0
],
[
0
,
0
,
0
,
x3
,
y3
,
1
,
-
height
*
x3
,
-
height
*
y3
,
-
height
],
[
x4
,
y4
,
1
,
0
,
0
,
0
,
-
width
*
x4
,
-
width
*
y4
,
-
width
],
[
0
,
0
,
0
,
x4
,
y4
,
1
,
-
height
*
x4
,
-
height
*
y4
,
-
height
]])
# Calculate transform matrix
A
=
inv
(
svd
(
M
)[
2
].
T
[:,
-
1
].
reshape
(
3
,
3
))
# Construct the transformed image
result
=
zeros
((
width
,
height
,
4
))
for
x
in
xrange
(
width
):
for
y
in
xrange
(
height
):
p
=
dot
(
A
,
array
([[
x
],
[
y
],
[
1
]]))
result
[
x
][
y
]
=
pv
(
image
,
p
[
0
]
/
p
[
2
],
p
[
1
]
/
p
[
2
],
'
linear
'
)
return
result
if
__name__
==
'
__main__
'
:
from
pylab
import
imread
,
imshow
image
=
imread
(
'
cameraman.png
'
)
#image = imread('cameraman.png')
image
=
imread
(
'
flyeronground.png
'
)
half_width
=
image
.
shape
[
0
]
/
2
half_height
=
image
.
shape
[
1
]
/
2
M
,
N
=
image
.
shape
M
,
N
=
image
.
shape
[:
2
]
# TODO: validate MxN -> width x height
result
=
affine_transform
(
image
,
(
0.
,
half_height
),
(
half_width
,
0.
),
(
image
.
shape
[
0
],
half_height
),
128
,
128
)
#result = affine_transform(image, (0., half_height), (half_width, 0.),
# (image.shape[0], half_height), 128, 128)
result
=
perspective_transform
(
image
,
(
149
,
590
),
(
101
,
376
),
(
301
,
209
),
(
393
,
392
),
64
,
64
)
imshow
(
result
,
cmap
=
'
gray
'
)
show
()
This diff is collapsed.
Click to expand it.
improc/ass2/flyeronground.png
0 → 100644
+
0
−
0
View file @
b7e9b760
728 KiB
This diff is collapsed.
Click to expand it.
improc/ass2/interpolation.py
+
2
−
4
View file @
b7e9b760
...
...
@@ -12,10 +12,8 @@ def pv(image, x, y, method):
return
image
[
round
(
x
)][
round
(
y
)]
if
in_image
(
image
,
x
,
y
)
else
bgcolor
if
method
==
'
linear
'
:
x1
=
floor
(
x
)
x2
=
floor
(
x
+
1
)
y1
=
floor
(
y
)
y2
=
floor
(
y
+
1
)
x1
,
y1
=
floor
(
x
),
floor
(
y
)
x2
,
y2
=
floor
(
x
+
1
),
floor
(
y
+
1
)
if
not
in_image
(
image
,
x1
,
y1
)
or
not
in_image
(
image
,
x2
,
y2
):
return
bgcolor
...
...
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