Initial commit with project files
This commit is contained in:
22
2_Bildbearbeitung/ü8/README.md
Normal file
22
2_Bildbearbeitung/ü8/README.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# Übung 8: Geometrische Transformation
|
||||
|
||||
In dieser Übung soll die Transformationsforschrift
|
||||
|
||||
<p align="center">
|
||||
<img src="https://latex.codecogs.com/svg.image?\begin{pmatrix}&space;x'\\y'\end{pmatrix}&space;=\begin{pmatrix}&space;a&space;&&space;b\\c&space;&&space;d\end{pmatrix}\cdot\begin{pmatrix}&space;x\\y\end{pmatrix}+&space;\begin{pmatrix}&space;e\\f\end{pmatrix}.&space;" title="\begin{pmatrix} x'\\y'\end{pmatrix} =\begin{pmatrix} 0.5 & 0\\0 & 0.5\end{pmatrix}\cdot\begin{pmatrix} x\\y\end{pmatrix} ." />
|
||||
</p>
|
||||
|
||||
für die Transformation des Bildes I1 zu I2 hergeleitet werden.
|
||||
|
||||
|
||||
| I1 | I2 |
|
||||
| --- | --- |
|
||||
|  |  |
|
||||
|
||||
|
||||
Leiten Sie die Transformationsvorschrift her und testen Sie die Vorschrift, indem Sie ein Skript in die Datei [a.py](a.py)
|
||||
programmieren. Die Lösung ist in der Datei [l_a.py](l_a.py) zu finden!
|
||||
|
||||
|
||||
|
||||
|
||||
42
2_Bildbearbeitung/ü8/a.py
Normal file
42
2_Bildbearbeitung/ü8/a.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
img = cv2.imread("../../data/car.png")
|
||||
img = cv2.resize(img, (500, 500))
|
||||
cv2.imshow("Car", img)
|
||||
|
||||
print("Image Shape:", img.shape)
|
||||
|
||||
|
||||
def T(pos, rotation, translation):
|
||||
""" Transformation Matrix """
|
||||
new_pos = np.matmul(rotation, pos) + translation
|
||||
|
||||
return new_pos
|
||||
|
||||
|
||||
new_image = np.zeros_like(img)
|
||||
|
||||
rotation = [
|
||||
[..., ...],
|
||||
[..., ...]
|
||||
]
|
||||
translation = [
|
||||
...,
|
||||
...
|
||||
]
|
||||
|
||||
|
||||
for x in range(img.shape[1]):
|
||||
for y in range(img.shape[0]):
|
||||
old_pos = np.asarray([[x], [y]])
|
||||
new_pos = T(old_pos, rotation, translation)
|
||||
new_pos = new_pos.astype(int)
|
||||
if 0 <= new_pos[0] < img.shape[1] and 0 <= new_pos[1] < img.shape[0]:
|
||||
new_image[new_pos[1], new_pos[0]] = img[y, x]
|
||||
|
||||
print(new_image.shape)
|
||||
cv2.imshow("After Transformation", new_image)
|
||||
|
||||
cv2.waitKey(0)
|
||||
|
||||
BIN
2_Bildbearbeitung/ü8/data/new.jpg
Normal file
BIN
2_Bildbearbeitung/ü8/data/new.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 78 KiB |
BIN
2_Bildbearbeitung/ü8/data/original.jpg
Normal file
BIN
2_Bildbearbeitung/ü8/data/original.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 77 KiB |
88
2_Bildbearbeitung/ü8/l_a.py
Normal file
88
2_Bildbearbeitung/ü8/l_a.py
Normal file
@@ -0,0 +1,88 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
img = cv2.imread("../../data/car.png")
|
||||
img = cv2.resize(img, (500, 500))
|
||||
cv2.imshow("Car", img)
|
||||
|
||||
print("Image Shape:", img.shape)
|
||||
|
||||
|
||||
def T(pos, rotation, translation):
|
||||
""" Transformation Matrix """
|
||||
new_pos = np.matmul(rotation, pos) + translation
|
||||
|
||||
return new_pos
|
||||
|
||||
|
||||
new_image = np.zeros_like(img)
|
||||
|
||||
alpha = -0.5 * np.pi
|
||||
rotation = [
|
||||
[np.cos(alpha), -np.sin(alpha)],
|
||||
[np.sin(alpha), np.cos(alpha)]
|
||||
]
|
||||
translation = [
|
||||
[0],
|
||||
[img.shape[1]]
|
||||
]
|
||||
|
||||
|
||||
for x in range(img.shape[1]):
|
||||
for y in range(img.shape[0]):
|
||||
old_pos = np.asarray([[x], [y]])
|
||||
new_pos = T(old_pos, rotation, translation)
|
||||
new_pos = new_pos.astype(int)
|
||||
if 0 <= new_pos[0] < img.shape[1] and 0 <= new_pos[1] < img.shape[0]:
|
||||
new_image[new_pos[1], new_pos[0]] = img[y, x]
|
||||
|
||||
print(new_image.shape)
|
||||
cv2.imshow("After Transformation", new_image)
|
||||
|
||||
"""
|
||||
Naive Solution
|
||||
|
||||
1. Rotate the image with 90° -> alpha=90°
|
||||
2. Translate in y axis with image length -> [ [0], [height] ]
|
||||
|
||||
"""
|
||||
|
||||
"""
|
||||
Mathematical solution
|
||||
|
||||
1. Find correspondences
|
||||
|
||||
x,y --> x',y'
|
||||
|
||||
0,0 --> 0,1
|
||||
1,0 --> 0,0
|
||||
0,1 --> 1,1
|
||||
1,1 --> 1,0
|
||||
0.5,0.5 --> 0.5,0.5
|
||||
|
||||
|
||||
2. Solve system:
|
||||
[a, b] * [x, y]^T + [e, f]^T = [x', y']^T
|
||||
[c, d]
|
||||
|
||||
ax + by + e = x'
|
||||
cx + dy + f = y'
|
||||
|
||||
|
||||
Translation (0,0 --> 0,1):
|
||||
0 + 0 + e = 0 --> e = 0
|
||||
0 + 0 + f = 1 --> f = 1
|
||||
|
||||
Rotation (1,0 --> 0,0):
|
||||
a + 0 + 0 = 0 --> a = 0
|
||||
c + 0 + 1 = 0 --> c = -1
|
||||
|
||||
Rotation (0,1 --> 1,1):
|
||||
0 + b + 0 = 1 --> b = 1
|
||||
0 + d + 1 = 1 --> d = 0
|
||||
|
||||
"""
|
||||
|
||||
|
||||
cv2.waitKey(0)
|
||||
|
||||
Reference in New Issue
Block a user