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
2393aae2
Commit
2393aae2
authored
13 years ago
by
Taddeüs Kroes
Browse files
Options
Downloads
Patches
Plain Diff
ImProc ass2: Implemented affine transformation.
parent
d082e2e6
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
improc/ass2/affine.py
+17
-33
17 additions, 33 deletions
improc/ass2/affine.py
improc/ass2/interpolation.py
+14
-10
14 additions, 10 deletions
improc/ass2/interpolation.py
with
31 additions
and
43 deletions
improc/ass2/affine.py
+
17
−
33
View file @
2393aae2
from
interpolation
import
pv
from
numpy
import
array
,
zeros
from
pylab
import
array
,
matrix
,
zeros
,
show
from
numpy.linalg
import
lstsq
,
inv
# url: http://www.leptonica.com/affine.html
...
...
@@ -19,43 +19,26 @@ def affine_transform(image, p1, p2, p3, width, height):
[
x3
,
y3
,
1
,
0
,
0
,
0
],
[
0
,
0
,
0
,
x3
,
y3
,
1
]])
q
=
array
([[
x1
],
[
y1
],
[
x2
],
[
y2
],
[
x3
],
[
y3
]])
q
=
array
([[
0
],
[
0
],
[
width
],
[
0
],
[
width
],
[
height
]])
# Calculate transform matrix
p
=
lstsq
(
M
,
q
)[
0
]
T
=
p
.
reshape
(
2
,
3
).
T
T
=
inv
(
T
)
a
,
b
,
c
,
d
,
e
,
f
=
lstsq
(
M
,
q
)[
0
].
T
.
tolist
()[
0
]
A
=
matrix
([[
a
,
b
,
c
],
[
d
,
e
,
f
],
[
0
,
0
,
1
]]).
I
# Construct the transformed image
result
=
zeros
((
width
,
height
))
for
y
in
xrange
(
height
):
for
x
in
xrange
(
width
):
# TODO: multiplication using '*' or 'dot()' ?
# XXX: where is |0 0 1| in T, since p.reshape(2, 3) ?
# |x'| |a b c| |x|
# |y'| = |d e f| |y|
# |1 | |0 0 1| |1|
orig_pos
=
array
([
x
,
y
,
1
]).
reshape
(
3
,
1
)
*
T
#print x,y, tmp
print
orig_pos
,
len
(
orig_pos
)
# XXX Why is orig_pos in the format listed below?
#
# |g h|
# |i j|
# |k l|
#
result
[
y
][
x
]
=
pv
(
image
,
orig_pos
[
0
],
orig_pos
[
1
],
'
linear
'
)
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
...
...
@@ -70,7 +53,8 @@ if __name__ == '__main__':
M
,
N
=
image
.
shape
# TODO: validate MxN -> width x height
result
=
affine_transform
(
image
,
(
0.
,
0.
),
(
0.
,
half_height
),
(
half_width
,
half_height
),
M
,
N
)
result
=
affine_transform
(
image
,
(
0.
,
half_height
),
(
half_width
,
0.
),
(
image
.
shape
[
0
]
,
half_height
),
128
,
128
)
imshow
(
result
)
imshow
(
result
,
cmap
=
'
gray
'
)
show
()
This diff is collapsed.
Click to expand it.
improc/ass2/interpolation.py
+
14
−
10
View file @
2393aae2
...
...
@@ -6,22 +6,26 @@ def in_image(image, x, y):
return
x
>=
0
and
y
>=
0
and
x
<
image
.
shape
[
0
]
and
y
<
image
.
shape
[
1
]
def
pv
(
image
,
x
,
y
,
method
):
if
not
in_image
(
image
,
x
,
y
):
return
bgcolor
if
method
==
'
nearest
'
:
return
image
[
round
(
x
)][
round
(
y
)]
x
,
y
=
round
(
x
),
round
(
y
)
return
image
[
round
(
x
)][
round
(
y
)]
if
in_image
(
image
,
x
,
y
)
else
bgcolor
if
method
==
'
linear
'
:
x1
=
floor
(
x
)
x2
=
ceil
(
x
)
x2
=
floor
(
x
+
1
)
y1
=
floor
(
y
)
y2
=
ceil
(
y
)
y2
=
floor
(
y
+
1
)
if
not
in_image
(
image
,
x1
,
y1
)
or
not
in_image
(
image
,
x2
,
y2
):
return
bgcolor
diff
=
(
x2
-
x1
)
*
(
y2
-
y1
)
return
image
[
x1
][
y1
]
*
(
x2
-
x
)
*
(
y2
-
y
)
\
+
image
[
x2
][
y1
]
*
(
x
-
x1
)
*
(
y2
-
y
)
\
+
image
[
x1
][
y2
]
*
(
x2
-
x
)
*
(
y
-
y1
)
\
+
image
[
x2
][
y2
]
*
(
x
-
x1
)
*
(
y
-
y1
)
return
image
[
x1
][
y1
]
/
diff
*
(
x2
-
x
)
*
(
y2
-
y
)
\
+
image
[
x2
][
y1
]
/
diff
*
(
x
-
x1
)
*
(
y2
-
y
)
\
+
image
[
x1
][
y2
]
/
diff
*
(
x2
-
x
)
*
(
y
-
y1
)
\
+
image
[
x2
][
y2
]
/
diff
*
(
x
-
x1
)
*
(
y
-
y1
)
raise
ValueError
,
'
Interpolation method
"
%s
"
is not supported
'
%
method
...
...
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