Initial commit with project files

This commit is contained in:
2025-06-27 14:34:11 +02:00
commit 7ea3207e63
310 changed files with 9331 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
# Übung 10: Objekt Detektion
Sie haben folgendes Bild gegeben, in dem Sie eine Flasche detektieren wollen:
![](../../data/flasche_rechteckig.png)
Weiterhin haben Sie bereits ein Kantenbild und ein Template erhalten:
| Kantenbild | Template |
| ---------- | -------- |
| ![](data/edges.png) | ![](data/template.png) |
## a) Template Matching
Sie sollen ein Template Matching durchführen, um die Position zu finden,
an welcher eine Flasche mit der größten Wahrscheinlichkeit steht. Nutzen Sie dafür das gegebene Template einer Flasche.
Um die Aufgabe zu lösen, implementieren Sie die folgenden Schritte:
1. Laden Sie das Template und das Kantenbild
2. Legen Sie das Template auf jeden möglichen Bildausschnitts des Kantenbildes und erstellen Sie einen Matching-Score.
1. Schneiden Sie einen Bildausschnitt in der Größe des Templates aus
2. Berechnen Sie Fläche des Querschnitt des Templates und des Bildausschnitts. Dies ist der Matching-Score dieser Position.
3. Speichern Sie den Matching Score für die Position
3. Finden Sie die Position mit dem größten Matching-Score und visualisieren Sie ihn in dem originalen Bild.
**Hinweise:**
- In dieser Übung sollen nur Bildausschnitte betrachtet werden, auf die das gesamte Template passt
- Das Template soll **NICHT** skaliert werden (Größen-Variant)
Bitte führen Sie für die Bearbeitung der Aufgabe das Skript [a.py](a.py) fort.
Die Lösung befindet sich in Datei [l_a.py](l_a.py).

31
5_Bildanalyse/ü10/a.py Normal file
View File

@@ -0,0 +1,31 @@
import numpy as np
import cv2
# Load images and template
original_img = cv2.imread("../../data/flasche_rechteckig.png")
original_img = cv2.resize(original_img, (int(original_img.shape[1]/ 2), int(original_img.shape[0] / 2)))
cv2.imshow("original_img", original_img)
# The original canny edge code wascode was:
# img = cv2.cvtColor(original_img, cv2.COLOR_BGR2GRAY)
# edges = cv2.Canny(img, 10, 100)
edges = cv2.imread("data/edges.png")[:, :, 0] / 255
cv2.imshow("canny-edges", edges.astype(np.float32) * 255)
template = cv2.imread("data/template.png")[:, :, 0] / 255
cv2.imshow("template", template.astype(np.float32) * 255)
# Sliding window over the edge image
h_edge, w_edge = edges.shape
h_template, w_template = template.shape
offset_h, offset_w = int(h_template / 2), int(w_template / 2)
print("Shape of edge image:", edges.shape)
print("Shape of template:", template.shape)
print("Offset of template:", offset_h, offset_w)
# YOUR CODE
# ...
cv2.waitKey(0)

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

44
5_Bildanalyse/ü10/l_a.py Normal file
View File

@@ -0,0 +1,44 @@
import numpy as np
import cv2
# Load images and template
original_img = cv2.imread("../../data/flasche_rechteckig.png")
original_img = cv2.resize(original_img, (int(original_img.shape[1]/ 2), int(original_img.shape[0] / 2)))
cv2.imshow("original_img", original_img)
# The original canny edge code wascode was:
# img = cv2.cvtColor(original_img, cv2.COLOR_BGR2GRAY)
# edges = cv2.Canny(img, 10, 100)
edges = cv2.imread("data/edges.png")[:, :, 0] / 255
cv2.imshow("canny-edges", edges.astype(np.float32) * 255)
template = cv2.imread("data/template.png")[:, :, 0] / 255
cv2.imshow("template", template.astype(np.float32) * 255)
# Sliding window over the edge image
h_edge, w_edge = edges.shape
h_template, w_template = template.shape
offset_h, offset_w = int(h_template / 2), int(w_template / 2)
print("Shape of edge image:", edges.shape)
print("Shape of template:", template.shape)
print("Offset of template:", offset_h, offset_w)
heatmap = np.zeros_like(edges).astype(np.float32)
for x in range(0, w_edge - w_template):
for y in range(0, h_edge - h_template):
overlapping_pixel = edges[y:y+h_template, x:x+w_template] * template
num_overlapping_pixel = np.sum(overlapping_pixel)
heatmap[y + offset_h, x + offset_w] = num_overlapping_pixel
heatmap = heatmap / np.max(heatmap)
cv2.imshow("heatmap", heatmap)
# Find maximum and print it so the original image
max_pos = np.unravel_index(heatmap.argmax(), heatmap.shape)
print("The maximal position is:", max_pos)
red = [0, 0, 255]
original_img[max_pos[0]-5:max_pos[0]+5, max_pos[1]-5:max_pos[1]+5] = red
cv2.imshow("best_match", original_img)
cv2.waitKey(0)