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,35 @@
# Übung 1: BGR zu HSV
In dieser Übung wird der HSV Farbraum betrachtet. Die Information der Farbe wird
durch die drei Werte
- H: Helligkeit (Hue)
- S: Sättigung (Saturation)
- V: Value (Helligkeit)
repräsentiert.
In der folgenden Abbildung wird der Farbraum visuell dargestellt:
![alt text](https://upload.wikimedia.org/wikipedia/commons/f/f1/HSV_cone.jpg)
## Aufgabe a)
In der Datei *a.py* wird ein Bild geladen. Nach dem Laden befindet sich das Bild
im BGR-Farbraum. Konvertieren Sie das Bild manuell und
ohne Hilfe von OpenCV in den HSV Farbraum.
Sie können die Rechenvorschriften der Konvertierung von RGB zu HSV aus der [OpenCV-Dokumentation](https://docs.opencv.org/3.4/de/d25/imgproc_color_conversions.html)
nutzen. Beachten Sie, dass die Farbkanäle in OpenCV in BGR und nicht in RGB abgespeichert sind!
## Aufgabe b)
Konvertieren Sie das Bild mithilfe der OpenCV Funktion ```cv2.cvtColor()``` in den HSV
Farbraum. Vergleichen Sie das Ergebnis dann mit dem Ergebnis aus Aufgabenteil a).
Sind die Ergebnisse gleich? Wenn nicht, woran kann es liegen?

View File

@@ -0,0 +1,39 @@
import numpy as np
import cv2
# Einlesen des Bildes
filepath = "../../data/balls.png"
''' a) Manuelles Konvertieren '''
img_norm = img.astype(np.float32) / 255.0
height, width, channels = img.shape
hsv_img = np.zeros_like(img_norm)
for x in range(width):
for y in range(int(height)):
hsv_img = np.round(hsv_img * 255)
hsv_img = hsv_img.astype(np.uint8)
''' b) Konvertieren mit OpenCV '''
hsv_img2 =
''' Das Ergebnis überprüfen '''
img2 = cv2.cvtColor(hsv_img, cv2.COLOR_HSV2BGR)
img3 = cv2.cvtColor(hsv_img2, cv2.COLOR_HSV2BGR)
difference = np.sum(np.abs(img2 - img3))
print("Totale Differenz zwischen den Ergebnissen:", difference)
max_difference = np.max(np.abs(img2 - img3))
print("Maximale Abweichung pro Pixel/Kanal:", max_difference)
example_differences = img2[0:10, 0:10] - img3[0:10, 0:10]
print("Beispiel Differenzen:\n", example_differences)
cv2.imshow("ORIGINAL", img)
cv2.imshow("MANUELL", img2)
cv2.imshow("OPENCV", img3)
cv2.waitKey(0)

View File

@@ -0,0 +1,66 @@
import numpy as np
import cv2
# Einlesen des Bildes
filepath = "../../data/balls.png"
img = cv2.imread(filepath)
''' a) Manuelles Konvertieren '''
img_norm = img.astype(np.float32) / 255.0
height, width, channels = img.shape
hsv_img = np.zeros_like(img_norm)
for x in range(width):
for y in range(int(height)):
minimum = np.min(img_norm[y, x])
b, g, r = img_norm[y, x, 0], img_norm[y, x, 1], img_norm[y, x, 2]
# V Wert
v = np.max(img_norm[y, x].copy())
# S Wert
if v == 0:
s = 0
else:
s = (v - minimum) / v
# H Wert
max_channel_index = np.argmax(img_norm[y, x])
if (v - np.min(img_norm[y, x])) == 0:
h = np.zeros(1)
elif max_channel_index == 2: # Rot
h = 60 * (g - b) / (v - minimum)
elif max_channel_index == 1: # Grün
h = 120 + 60 * (b - r) / (v - minimum)
else: # Blau
h = 240 + 60 * (r - g) / (v - minimum)
if h < 0:
h += 360
h /= (2 * 255)
hsv_img[y, x, 0] = h
hsv_img[y, x, 1] = s
hsv_img[y, x, 2] = v
hsv_img = np.round(hsv_img * 255)
hsv_img = hsv_img.astype(np.uint8)
''' b) Konvertieren mit OpenCV '''
hsv_img2 = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
''' Das Ergebnis überprüfen '''
img2 = cv2.cvtColor(hsv_img, cv2.COLOR_HSV2BGR)
img3 = cv2.cvtColor(hsv_img2, cv2.COLOR_HSV2BGR)
difference = np.sum(np.abs(img2 - img3))
print("Totale Differenz zwischen den Ergebnissen:", difference)
max_difference = np.max(np.abs(img2 - img3))
print("Maximale Abweichung pro Pixel/Kanal:", max_difference)
example_differences = img2[0:3, 0:3] - img3[0:3, 0:3]
print("Beispiel Differenzen:\n", example_differences)
cv2.imshow("ORIGINAL", img)
cv2.imshow("MANUELL", img2)
cv2.imshow("OPENCV", img3)
cv2.waitKey(0)